for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace WebComplete\core\entity;
use function WebComplete\core\utils\typecast\cast;
WebComplete\core\utils\typecast\cast
abstract class AbstractEntity
{
protected $id;
private $data = [];
/**
* @return array
*/
abstract public static function fields(): array;
* @return int|string|null
public function getId()
return $this->id;
}
* @param int|string $id
public function setId($id)
$this->id = $id;
* @param array $data
* @param bool $update
public function mapFromArray(array $data, bool $update = false)
if (!$update) {
$this->data = [];
foreach ($data as $field => $value) {
$this->setField($field, $value);
public function mapToArray(): array
$result = [
'id' => $this->getId()
];
$fields = \array_keys(static::fields());
foreach ($fields as $field) {
$result[$field] = $this->getField($field);
return $result;
* @param $name
* @param $value
protected function setField($name, $value)
$fields = static::fields();
if (isset($fields[$name])) {
$this->data[$name] = cast($value, $fields[$name]);
* @param null $default
$default
null
*
* @return mixed|null
protected function getField($name, $default = null)
return $this->data[$name] ?? $default;