From dfbb4d6f9c3d348b15a7910bccc0b7c372bf7622 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 18 Feb 2024 19:39:50 +0100 Subject: [PATCH] Add Db AttrSet --- src/Db/AttrSet.php | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/Db/AttrSet.php diff --git a/src/Db/AttrSet.php b/src/Db/AttrSet.php new file mode 100644 index 0000000..33cefc7 --- /dev/null +++ b/src/Db/AttrSet.php @@ -0,0 +1,47 @@ + + */ + protected $possible_values = []; + + /** + * Compute attribute value from DB + * @param string|null $value The value as retrieved from debug + * @return string|null The attribute value + */ + public function from_db($value) { + $value = parent::from_db($value); + if (is_null($value)) + return null; + if (!in_array($value, $this -> possible_values)) + throw new DbException( + "Unexpected value '%s' retrieved from database. Should be one of the following values: %s", + $value, implode(', ', $this -> possible_values) + ); + return $value; + } + + /** + * Compute attribute value for DB + * @param string|null $value The value as handled in PHP + * @return string|null The attribute value as stored in DB + */ + public function to_db($value) { + $value = parent::from_db($value); + if (is_null($value)) + return null; + if (!in_array($value, $this -> possible_values)) + throw new DbException( + "Unexpected value '%s'. Should be one of the following values: %s", + $value, implode(', ', $this -> possible_values) + ); + return $value; + } + +}