From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- library/Icinga/User/Preferences.php | 169 ++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 library/Icinga/User/Preferences.php (limited to 'library/Icinga/User/Preferences.php') diff --git a/library/Icinga/User/Preferences.php b/library/Icinga/User/Preferences.php new file mode 100644 index 0000000..b09462b --- /dev/null +++ b/library/Icinga/User/Preferences.php @@ -0,0 +1,169 @@ + + * 'value')); // Start with initial preferences + * + * $preferences->aNewPreference = 'value'; // Set a preference + * + * unset($preferences->aPreference); // Unset a preference + * + * // Retrieve a preference and return a default value if the preference does not exist + * $anotherPreference = $preferences->get('anotherPreference', 'defaultValue'); + */ +class Preferences implements Countable +{ + /** + * Preferences key-value array + * + * @var array + */ + protected $preferences = array(); + + /** + * Constructor + * + * @param array $preferences Preferences key-value array + */ + public function __construct(array $preferences = array()) + { + $this->preferences = $preferences; + } + + /** + * Count all preferences + * + * @return int The number of preferences + */ + public function count(): int + { + return count($this->preferences); + } + + /** + * Determine whether a preference exists + * + * @param string $name + * + * @return bool + */ + public function has($name) + { + return array_key_exists($name, $this->preferences); + } + + /** + * Write data to a preference + * + * @param string $name + * @param mixed $value + */ + public function __set($name, $value) + { + $this->preferences[$name] = $value; + } + + /** + * Retrieve a preference section + * + * @param string $name + * + * @return array|null + */ + public function get($name) + { + if (array_key_exists($name, $this->preferences)) { + return $this->preferences[$name]; + } + + return null; + } + + /** + * Retrieve a value from a specific section + * + * @param string $section + * @param string $name + * @param null $default + * + * @return array|null + */ + public function getValue($section, $name, $default = null) + { + if (array_key_exists($section, $this->preferences) + && array_key_exists($name, $this->preferences[$section]) + ) { + return $this->preferences[$section][$name]; + } + + return $default; + } + + /** + * Magic method so that $obj->value will work. + * + * @param string $name + * + * @return mixed + */ + public function __get($name) + { + return $this->get($name); + } + + /** + * Remove a given preference + * + * @param string $name Preference name + */ + public function remove($name) + { + unset($this->preferences[$name]); + } + + /** + * Determine if a preference is set and is not NULL + * + * @param string $name Preference name + * + * @return bool + */ + public function __isset($name) + { + return isset($this->preferences[$name]); + } + + /** + * Unset a given preference + * + * @param string $name Preference name + */ + public function __unset($name) + { + $this->remove($name); + } + + /** + * Get preferences as array + * + * @return array + */ + public function toArray() + { + return $this->preferences; + } +} -- cgit v1.2.3