forked from mirzaev/baza
1
0
Fork 0
baza/mirzaev/baza/system/record.php

133 lines
2.6 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace mirzaev\baza;
// Built-in libraries
use DomainException as exception_domain;
/**
* Record
*
* @package mirzaev\baza
*
* @var array $values The record values
*
* @method void __construct(string|int|float $values) Constructor
* @method array values() Read all values of the record
* @method void __set(string $name, mixed $value) Write the record value
* @method mixed __get(string $name) Read the record value
* @method void __unset(string $name) Delete the record value
* @method bool __isset(string $name) Check for initializing of the record value
*
* @license http://www.wtfpl.net Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
class record
{
/**
* Values
*
* @var array $values The record values
*/
protected array $values = [];
/**
* Constructor
*
* @param string[]|int[]|float[] $values Values of the record
*
* @return void
*/
public function __construct(string|int|float ...$values)
{
// Initializing values
if (!empty($values)) $this->values = $values;
}
/**
* Values
*
* Read all values of the record
*
* @return array All values of the record
*/
public function values(): array
{
return $this->values ?? [];
}
/**
* Write
*
* Write the value
*
* @param string $name Name of the parameter
* @param mixed $value Content of the parameter
*
* @throws exception_domain if not found the parameter
*
* @return void
*/
public function __set(string $name, mixed $value = null): void
{
if (isset($this->values[$name])) {
// Initialized the parameter
// Writing the value and exit
$this->values[$name] = $value;
} else {
// Not initialized the parameter
// Exit (fail)
throw new exception_domain("Not found the parameter: $name");
}
}
/**
* Read
*
* Read the value
*
* @param string $name Name of the value
*
* @return mixed Content of the value
*/
public function __get(string $name): mixed
{
// Reading the value and exit (success)
return $this->values[$name] ?? null;
}
/**
* Delete
*
* Delete the value
*
* @param string $name Name of the value
*
* @return void
*/
public function __unset(string $name): void
{
// Deleting the value
unset($this->values[$name]);
}
/**
* Check for initializing
*
* Check for initializing the value
*
* @param string $name Name of the value
*
* @return bool Is the value initialized?
*/
public function __isset(string $name): bool
{
// Checking for initializing the value and exit (success)
return isset($this->values[$name]);
}
}