99 lines
3.4 KiB
PHP
Executable File
99 lines
3.4 KiB
PHP
Executable File
<?php declare(strict_types=1); namespace IR\Core;
|
|
if (!defined('IR_START')) exit('<pre>No direct script access allowed</pre>');
|
|
|
|
use IR\Utils\Meta\Inspector as Inspector;
|
|
use IR\Utils\Types\Strings as Strings;
|
|
use IR\Exceptions\Types\SystemException as SystemException;
|
|
use IR\Exceptions\Types\MethodNotFoundException as MethodNotFoundException;
|
|
|
|
class Base
|
|
{
|
|
public function __construct(array $options = [])
|
|
{
|
|
foreach ($options as $key => $value)
|
|
{
|
|
if(property_exists($this,"_{$key}"))
|
|
{
|
|
$key = str_replace(' ','',preg_replace( "/\r|\n/","",ucwords(str_replace('_',' ',$key))));
|
|
$method = "set{$key}";
|
|
$this->$method($value);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function __call(string $method, array $arguments)
|
|
{
|
|
$class = get_called_class();
|
|
|
|
# getters case
|
|
$getMatches = Strings::getInstance()->match($method, '^get([a-zA-Z0-9_]+)$');
|
|
if (is_array($getMatches) && count($getMatches) > 0)
|
|
{
|
|
$normalized = lcfirst($getMatches[0]);
|
|
$property = "_{$normalized}";
|
|
$found = false;
|
|
|
|
if (property_exists($class, $property)) {
|
|
$found = true;
|
|
} else {
|
|
$pieces = preg_split('/(?=[A-Z])/', ucwords($normalized));
|
|
array_shift($pieces);
|
|
$property = strtolower('_' . implode('_', $pieces));
|
|
|
|
if (property_exists($class, $property)) {
|
|
$found = true;
|
|
}
|
|
}
|
|
|
|
if ($found)
|
|
{
|
|
$meta = Inspector::getInstance()->propertiesMeta($class, $property);
|
|
|
|
if (empty($meta['@readwrite']) && empty($meta['@read']))
|
|
{
|
|
throw new SystemException('You cannot get this property "' . $property . '" it\'s not readable in ' . $class);
|
|
}
|
|
|
|
return $this->$property ?? null;
|
|
}
|
|
}
|
|
|
|
# setters case
|
|
$setMatches = Strings::getInstance()->match($method, '^set([a-zA-Z0-9_]+)$');
|
|
if (is_array($setMatches) && count($setMatches) > 0)
|
|
{
|
|
$normalized = lcfirst($setMatches[0]);
|
|
$property = "_{$normalized}";
|
|
$found = false;
|
|
|
|
if (property_exists($class, $property)) {
|
|
$found = true;
|
|
} else {
|
|
$pieces = preg_split('/(?=[A-Z])/', ucwords($normalized));
|
|
array_shift($pieces);
|
|
$property = strtolower('_' . implode('_', $pieces));
|
|
|
|
if (property_exists($class, $property)) {
|
|
$found = true;
|
|
}
|
|
}
|
|
|
|
if ($found)
|
|
{
|
|
$meta = Inspector::getInstance()->propertiesMeta($class, $property);
|
|
|
|
if (empty($meta['@readwrite']) && empty($meta['@write']))
|
|
{
|
|
throw new SystemException('You cannot write on this property "' . $property . '" it\'s readonly in ' . $class);
|
|
}
|
|
|
|
$this->$property = $arguments[0];
|
|
return true;
|
|
}
|
|
}
|
|
|
|
$class = (get_called_class() != '' && get_called_class() != false) ? 'in ' . get_called_class() : '';
|
|
throw new MethodNotFoundException($method . ' : Method is not implemented ' . $class . ' !', 500);
|
|
}
|
|
}
|