AbstractPersistence.php 4.34 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Persistence;

4 5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7 8 9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
16 17 18 19

/**
 * Abstract persistence handler
 */
20
abstract class AbstractPersistence implements \T3o\TerFe2\Persistence\PersistenceInterface
21 22 23 24 25 26 27 28
{

    /**
     * @var string
     */
    protected $name = 'Tx_TerFe2';

    /**
29
     * @var bool
30
     */
Markus Sommer's avatar
Markus Sommer committed
31
    protected $isLoaded = false;
32 33

    /**
34
     * @var bool
35
     */
Markus Sommer's avatar
Markus Sommer committed
36
    protected $saveImmediately = true;
37 38 39 40

    /**
     * @var array
     */
41
    protected $content = [];
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    /**
     * Set name
     *
     * @param string $name Name to set
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string Name of the persistence
     */
    public function getName()
    {
        if (empty($this->name)) {
61
            // @TODO the property is initialized! Either initialization or this should be removed.
62 63 64 65 66 67 68 69
            $this->setName(get_class($this));
        }
        return $this->name;
    }

    /**
     * Set isLoaded
     *
70
     * @param bool $isLoaded Is loaded state
71 72 73 74 75 76 77 78 79
     */
    public function setIsLoaded($isLoaded)
    {
        $this->isLoaded = (bool)$isLoaded;
    }

    /**
     * Get isLoaded
     *
80
     * @return bool Is loaded state
81 82 83 84 85 86 87 88 89
     */
    public function isLoaded()
    {
        return (bool)$this->isLoaded;
    }

    /**
     * Set saveImmediately
     *
90
     * @param bool $saveImmediately Save immediately
91 92 93 94 95 96 97 98 99
     */
    public function setSaveImmediately($saveImmediately)
    {
        $this->saveImmediately = (bool)$saveImmediately;
    }

    /**
     * Get saveImmediately
     *
100
     * @return bool Save immediately state
101 102 103 104 105 106 107 108 109 110 111
     */
    public function saveImmediately()
    {
        return (bool)$this->saveImmediately;
    }

    /**
     * Set value
     *
     * @param string $key Name of the value
     * @param mixed $value Value content
112
     * @throws \Exception
113 114 115 116
     */
    public function set($key, $value)
    {
        if (empty($key)) {
117
            throw new \Exception('Empty keys are not allowed');
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        }
        if (!$this->isLoaded()) {
            $this->load();
        }
        $this->content[$key] = $value;
        if ($this->saveImmediately()) {
            $this->save();
        }
    }

    /**
     * Add value
     *
     * @param string $key Name of the value
     * @param mixed $value Value content
     */
    public function add($key, $value)
    {
        $this->set($key, $value);
    }

    /**
     * Add multiple values
     *
142
     * @param array $values Key <-> value pairs
143 144 145 146 147 148 149 150 151 152 153 154
     */
    public function addMultiple(array $values)
    {
        foreach ($values as $key => $value) {
            $this->add($key, $value);
        }
    }

    /**
     * Check if content contains given key
     *
     * @param string $key Name of the value
155
     * @return bool TRUE if exists
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
     */
    public function has($key)
    {
        if (!$this->isLoaded()) {
            $this->load();
        }
        return isset($this->content[$key]);
    }

    /**
     * Get value
     *
     * @param string $key Name of the value
     * @return mixed Value content
     */
    public function get($key)
    {
        if ($this->has($key)) {
            return $this->content[$key];
        }
Markus Sommer's avatar
Markus Sommer committed
176
        return null;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }

    /**
     * Get all values
     *
     * @return array Key <-> value pairs
     */
    public function getAll()
    {
        if (!$this->isLoaded()) {
            $this->load();
        }
        return $this->content;
    }

    /**
     * Remove a value
     *
     * @param string $key Name of the value
     */
    public function remove($key)
    {
        if ($this->has($key)) {
            unset($this->content[$key]);
        }
        if ($this->saveImmediately()) {
            $this->save();
        }
    }

    /**
     * Remove all values
     */
    public function removeAll()
    {
        if (!$this->isLoaded()) {
            $this->load();
        }
215
        $this->content = [];
216 217 218 219 220
        if ($this->saveImmediately()) {
            $this->save();
        }
    }
}