Commit 85867541 authored by Tomas Lang's avatar Tomas Lang

Restriction: change \Nette\Reflection\ClassType to...

Restriction: change \Nette\Reflection\ClassType to \LeanMapper\Reflection\EntityReflection (BC break)
parent e9c054c2
......@@ -10,8 +10,11 @@ abstract class AbstractRestriction
/** @var array */
protected $properties = [];
/** @var \LeanMapper\Reflection\EntityReflection */
protected $reflection;
/** @var \LeanMapper\Reflection\Property[] */
protected $properties;
/** @var array */
private $values = [];
......@@ -26,27 +29,13 @@ abstract class AbstractRestriction
*/
public function __construct(\LeanMapper\IMapper $mapper)
{
$this->mapper = $mapper;
$this->reflection = new \LeanMapper\Reflection\EntityReflection($this);
$element = new \Nette\Reflection\ClassType(get_class($this));
do {
$annotations = $element->getAnnotations('property');
if (isset($annotations['property'])) {
foreach ($annotations['property'] as $property) {
if (($parsedProperty = $this->parseProperty($property)) !== NULL && !isset($this->properties[$parsedProperty['variable']])) {
$this->properties[$parsedProperty['variable']] = $parsedProperty['type'];
}
}
}
$this->properties = array_reduce($this->reflection->getEntityProperties(), function($result, $item) {
return $result + [$item->getName() => $item];
}, array());
if ($element instanceof \Nette\Reflection\ClassType) {
$element = $element->getParentClass();
} else {
$element = NULL;
}
} while ($element !== NULL);
$this->mapper = $mapper;
$this->initDefaults();
}
......@@ -59,23 +48,6 @@ abstract class AbstractRestriction
/**
* @param string $property
*
* @return array|null
*/
protected function parseProperty($property)
{
$res = \Nette\Utils\Strings::match($property, '#^\s*(?P<type>[a-z\\\|]+)\s+\$(?P<variable>[a-z0-9_-]+)\s*$#i');
return $res === NULL ? $res : array(
'type' => explode('|', $res['type']),
'variable' => $res['variable']
);
}
/**
* @param string $key
*
......@@ -112,29 +84,32 @@ abstract class AbstractRestriction
throw new \InvalidArgumentException("'{$key}' is not defined.");
}
$types = $this->properties[$key];
if (is_object($value)) {
$found = FALSE;
foreach ($types as $class) {
if (class_exists($class) && $value instanceof $class) {
$found = TRUE;
}
$property = $this->properties[$key];
if ($property->isNullable() && $value == NULL) {
} elseif ($property->isBasicType()) {
switch ($property->getType()) {
case 'boolean':
$value = (bool) $value;
break;
case 'integer':
$value = (int) $value;
break;
case 'float':
$value = (float) $value;
break;
case 'string':
$value = (string) $value;
break;
case 'array':
$value = (array) $value;
break;
}
if (!$found) {
} else {
$type = $property->getType();
if (!($result = $result instanceof $type)) {
throw new \InvalidArgumentException('Unexpected class.');
}
} elseif (is_array($value) && in_array('array', $types)) {
} elseif ($value === NULL && in_array('null', $types)) {
} elseif (in_array('int', $types) && is_numeric($value)) {
$value = (int) $value;
} elseif (in_array('bool', $types) || in_array('boolean', $types)) {
$value = (bool) $value;
} elseif (in_array('string', $types)) {
$value = (string) $value;
} else {
throw new \InvalidArgumentException('Unexpected value.');
}
$this->values[$key] = $value;
......
......@@ -5,12 +5,11 @@ use Tester\Assert;
$container = require __DIR__ . '/../bootstrap.php';
/**
* @property string $string
* @property int $int
* @property bool $bool
* @property \DateTime $dateTime
* @property string|null $stringOrNull
* @property \DateTime|\stdClass $dateTimeOrStdClass
* @property string $string
* @property int $int
* @property bool $bool
* @property \DateTime $dateTime
* @property string|null $stringOrNull
*/
class MyRestriction extends \LeanMapperWorkflow\AbstractRestriction
{
......@@ -22,7 +21,6 @@ class MyRestriction extends \LeanMapperWorkflow\AbstractRestriction
}
/**
* @property int $string
* @property string $extended
*/
class ExtendedRestriction extends MyRestriction
......@@ -101,9 +99,6 @@ Assert::exception(function() use ($restriction) {
$restriction = new ExtendedRestriction($mapper);
$restriction->string = '123';
Assert::same($restriction->string, 123); // $string je v ExtendedRestriction typu int
$restriction->int = '123';
Assert::same($restriction->int, 123); // $int je v ExtendedRestriction zdeden od MyRestriction
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment