Commit 878b6095 authored by Johnny's avatar Johnny

Merge branch 'feature/remove-nette-dependency'

parents e9c054c2 8293962a
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
} }
], ],
"require": { "require": {
"nette/nette": "~2",
"tharos/leanmapper": "2.2.0" "tharos/leanmapper": "2.2.0"
}, },
"require-dev": { "require-dev": {
......
...@@ -10,8 +10,11 @@ abstract class AbstractRestriction ...@@ -10,8 +10,11 @@ abstract class AbstractRestriction
/** @var array */ /** @var \LeanMapper\Reflection\EntityReflection */
protected $properties = []; protected $reflection;
/** @var \LeanMapper\Reflection\Property[] */
protected $properties;
/** @var array */ /** @var array */
private $values = []; private $values = [];
...@@ -26,27 +29,13 @@ abstract class AbstractRestriction ...@@ -26,27 +29,13 @@ abstract class AbstractRestriction
*/ */
public function __construct(\LeanMapper\IMapper $mapper) public function __construct(\LeanMapper\IMapper $mapper)
{ {
$this->mapper = $mapper; $this->reflection = new \LeanMapper\Reflection\EntityReflection($this);
$element = new \Nette\Reflection\ClassType(get_class($this)); $this->properties = array_reduce($this->reflection->getEntityProperties(), function($result, $item) {
return $result + [$item->getName() => $item];
do { }, array());
$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'];
}
}
}
if ($element instanceof \Nette\Reflection\ClassType) { $this->mapper = $mapper;
$element = $element->getParentClass();
} else {
$element = NULL;
}
} while ($element !== NULL);
$this->initDefaults(); $this->initDefaults();
} }
...@@ -59,23 +48,6 @@ abstract class AbstractRestriction ...@@ -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 * @param string $key
* *
...@@ -112,29 +84,32 @@ abstract class AbstractRestriction ...@@ -112,29 +84,32 @@ abstract class AbstractRestriction
throw new \InvalidArgumentException("'{$key}' is not defined."); throw new \InvalidArgumentException("'{$key}' is not defined.");
} }
$types = $this->properties[$key]; $property = $this->properties[$key];
if (is_object($value)) { if ($property->isNullable() && $value == NULL) {
$found = FALSE; } elseif ($property->isBasicType()) {
foreach ($types as $class) { switch ($property->getType()) {
if (class_exists($class) && $value instanceof $class) { case 'boolean':
$found = TRUE; $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;
} }
} else {
if (!$found) { $type = $property->getType();
if (!($result = $result instanceof $type)) {
throw new \InvalidArgumentException('Unexpected class.'); 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; $this->values[$key] = $value;
......
...@@ -6,7 +6,6 @@ use LeanMapper\Relationship\HasMany; ...@@ -6,7 +6,6 @@ use LeanMapper\Relationship\HasMany;
use LeanMapper\Filtering; use LeanMapper\Filtering;
use LeanMapper\Fluent; use LeanMapper\Fluent;
use InvalidArgumentException; use InvalidArgumentException;
use Nette\Utils\Strings;
abstract class Entity extends \LeanMapper\Entity abstract class Entity extends \LeanMapper\Entity
...@@ -25,7 +24,7 @@ abstract class Entity extends \LeanMapper\Entity ...@@ -25,7 +24,7 @@ abstract class Entity extends \LeanMapper\Entity
throw new InvalidArgumentException("Method '$name' in entity " . get_called_class() . " expects at least one argument with target column name."); throw new InvalidArgumentException("Method '$name' in entity " . get_called_class() . " expects at least one argument with target column name.");
} }
return $this->__call($name . Strings::firstUpper(array_shift($arguments)), $arguments); return $this->__call($name . ucfirst(array_shift($arguments)), $arguments);
} elseif (substr($name, 0, 10) === 'getCountOf') { } elseif (substr($name, 0, 10) === 'getCountOf') {
$property = $this->getCurrentReflection()->getEntityProperty(lcfirst(substr($name, 10))); $property = $this->getCurrentReflection()->getEntityProperty(lcfirst(substr($name, 10)));
......
...@@ -37,7 +37,7 @@ class Repository extends AbstractRepository ...@@ -37,7 +37,7 @@ class Repository extends AbstractRepository
$row = $statement->fetch(); $row = $statement->fetch();
if ($row === FALSE) { if ($row === FALSE) {
if (is_callable($notFoundCallback)) { if (is_callable($notFoundCallback)) {
\Nette\Utils\Callback::invokeArgs($notFoundCallback, [$id]); $notFoundCallback($id);
} else { } else {
throw new InvalidArgumentException('Not found: ' . var_export($id, TRUE)); throw new InvalidArgumentException('Not found: ' . var_export($id, TRUE));
} }
......
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
namespace LeanMapperWorkflow\Traits; namespace LeanMapperWorkflow\Traits;
use LeanMapperWorkflow\Repository; use LeanMapperWorkflow\Repository;
use Nette\Application\IPresenter;
/**
* Trait is prepared for using in Nette presenters only
*/
trait FindEntity trait FindEntity
{ {
...@@ -19,7 +21,7 @@ trait FindEntity ...@@ -19,7 +21,7 @@ trait FindEntity
*/ */
protected final function findEntity(Repository $repository, $id, $showMessage = FALSE, $redirectLink = NULL) protected final function findEntity(Repository $repository, $id, $showMessage = FALSE, $redirectLink = NULL)
{ {
if ($this instanceof IPresenter && ($showMessage !== FALSE || $redirectLink !== NULL)) { if ($showMessage !== FALSE || $redirectLink !== NULL) {
$notFoundCallback = function() use ($showMessage, $redirectLink) { $notFoundCallback = function() use ($showMessage, $redirectLink) {
if ($showMessage) { if ($showMessage) {
$this->flashMessage('@todo', 'error'); $this->flashMessage('@todo', 'error');
......
...@@ -5,12 +5,11 @@ use Tester\Assert; ...@@ -5,12 +5,11 @@ use Tester\Assert;
$container = require __DIR__ . '/../bootstrap.php'; $container = require __DIR__ . '/../bootstrap.php';
/** /**
* @property string $string * @property string $string
* @property int $int * @property int $int
* @property bool $bool * @property bool $bool
* @property \DateTime $dateTime * @property \DateTime $dateTime
* @property string|null $stringOrNull * @property string|null $stringOrNull
* @property \DateTime|\stdClass $dateTimeOrStdClass
*/ */
class MyRestriction extends \LeanMapperWorkflow\AbstractRestriction class MyRestriction extends \LeanMapperWorkflow\AbstractRestriction
{ {
...@@ -22,7 +21,6 @@ class MyRestriction extends \LeanMapperWorkflow\AbstractRestriction ...@@ -22,7 +21,6 @@ class MyRestriction extends \LeanMapperWorkflow\AbstractRestriction
} }
/** /**
* @property int $string
* @property string $extended * @property string $extended
*/ */
class ExtendedRestriction extends MyRestriction class ExtendedRestriction extends MyRestriction
...@@ -101,9 +99,6 @@ Assert::exception(function() use ($restriction) { ...@@ -101,9 +99,6 @@ Assert::exception(function() use ($restriction) {
$restriction = new ExtendedRestriction($mapper); $restriction = new ExtendedRestriction($mapper);
$restriction->string = '123';
Assert::same($restriction->string, 123); // $string je v ExtendedRestriction typu int
$restriction->int = '123'; $restriction->int = '123';
Assert::same($restriction->int, 123); // $int je v ExtendedRestriction zdeden od MyRestriction 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