name = $name; } /** * Get name * * @return string Name of the persistence */ public function getName() { if (empty($this->name)) { // @TODO the property is initialized! Either initialization or this should be removed. $this->setName(get_class($this)); } return $this->name; } /** * Set isLoaded * * @param bool $isLoaded Is loaded state */ public function setIsLoaded($isLoaded) { $this->isLoaded = (bool)$isLoaded; } /** * Get isLoaded * * @return bool Is loaded state */ public function isLoaded() { return (bool)$this->isLoaded; } /** * Set saveImmediately * * @param bool $saveImmediately Save immediately */ public function setSaveImmediately($saveImmediately) { $this->saveImmediately = (bool)$saveImmediately; } /** * Get saveImmediately * * @return bool Save immediately state */ public function saveImmediately() { return (bool)$this->saveImmediately; } /** * Set value * * @param string $key Name of the value * @param mixed $value Value content * @throws \Exception */ public function set($key, $value) { if (empty($key)) { throw new \Exception('Empty keys are not allowed'); } 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 * * @param array $values Key <-> value pairs */ 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 * @return bool TRUE if exists */ 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]; } return null; } /** * 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(); } $this->content = []; if ($this->saveImmediately()) { $this->save(); } } }