Add Db AttrSet

This commit is contained in:
Benjamin Renard 2024-02-18 19:39:50 +01:00
parent 07b6fa1305
commit dfbb4d6f9c
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

47
src/Db/AttrSet.php Normal file
View file

@ -0,0 +1,47 @@
<?php
namespace EesyPHP\Db;
class AttrSet extends Attr {
/**
* Possible values
* @var array<mixed>
*/
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;
}
}