<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
trait LazyGetSet
{
private $useFluentSetter = true;
/**
* Change fluent setter.
*/
public function setFluentSetter($flag)
{
$this->useFluentSetter = $flag;
}
public function __call($method, $args)
{
if ((0 === strpos($method, 'get')) && ($ptyName = $this->getLazyPropertyName($method))) {
return $this->lazyGet($ptyName, $args);
}
if ((0 === strpos($method, 'set')) && ($ptyName = $this->getLazyPropertyName($method))) {
return $this->lazySet($ptyName, $args);
}
}
/**
* Detect property name from setter/getter methods.
*/
private function getLazyPropertyName($method)
{
$ptyName = lcfirst(substr($method, 3));
if (property_exists($this, $ptyName)) {
return $ptyName;
}
}
/**
* Get property value
*/
private function lazyGet($ptyName)
{
return $this->{$ptyName};
}
/**
* Inject property value.
*
* @todo Validate data type
*/
private function lazySet($ptyName, $args)
{
if($this->validate($ptyName, $args[0])) {
$this->{$ptyName} = $args[0];
}
if ($this->useFluentSetter) {
return $this;
}
}
private function validate($variable_name, $variable_value) {
$commentDocReader = new commentDocReader();
$commentDocReader->setClass(__CLASS__);
$hints = $commentDocReader->getPropertyHintType($variable_name);
if(isset($hints['@var'])) {
$validateCallback = array(
'string' => 'is_string',
'integer' => 'is_integer',
'array' => 'is_array',
'float' => 'is_float',
'double' => 'is_double',
'boolean' => 'is_bool',
'object' => 'is_object',
'resource' => 'is_resource',
);
if(isset($validateCallback[$hints['@var']])) {
return call_user_func($validateCallback[$hints['@var']], $variable_value);
} else {
return var_dump($variable_value instanceof $hints['@var']);
}
}
}
public function getPropertyHintType($className, $propertyName) {
$class = new ReflectionClass($className)
$match = array();
$docs = array();
foreach(explode("\n",(str_replace(array('*', '/'),'', $class->getProperty($propertyName)->getDocComment()))) as $doc) {
$docs[] = trim($doc);
}
$return = array();
foreach(array_filter($docs) as $doc) {
$match = array();
preg_match('/^([@].*)\ (.*)$/', $doc, $match);
if($match){
$return[$match[1]] = trim($match[2]);
}
}
return $return;
}
}
class MyClass
{
use LazyGetSet;
/**
* @var integer
*/
private $name;
/**
* @var MyClass
*/
private $instant;
}
//echo (new MyClass())->setName(123)->getName();
$MyClass = new MyClass();
echo $MyClass->setInstant($MyClass)->getInstant();
- Output for 5.4.0 - 5.4.32
- Parse error: syntax error, unexpected '$match' (T_VARIABLE) in /in/bf2bU on line 90
Process exited with code 255. - Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected T_STRING in /in/bf2bU on line 6
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected T_STRING in /in/bf2bU on line 6
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/bf2bU on line 6
Process exited with code 255.
preferences:
233.14 ms | 1386 KiB | 123 Q