Skip to content

Commit

Permalink
- Using reflection package
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Feb 14, 2020
1 parent 56e8c33 commit f03bc41
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

"doctrine/orm" : "~2.5",

"phpdocumentor/reflection-docblock": "^5.0",

"nette/di" : "~3.0",
"nette/utils" : "~3.0"
},
Expand Down
25 changes: 10 additions & 15 deletions src/IPub/DoctrineCrud/Mapping/EntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace IPub\DoctrineCrud\Mapping;

use phpDocumentor;

use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
Expand Down Expand Up @@ -222,7 +224,6 @@ public function fillEntity(Utils\ArrayHash $values, Entities\IEntity $entity, bo

} else {
$varAnnotation = $this->parseAnnotation($propertyReflection, 'var');
$varAnnotation = $varAnnotation ? (is_array($varAnnotation) ? (count($varAnnotation) ? end($varAnnotation) : NULL) : $varAnnotation) : NULL;

$className = NULL;

Expand Down Expand Up @@ -269,7 +270,6 @@ public function fillEntity(Utils\ArrayHash $values, Entities\IEntity $entity, bo

} else {
$varAnnotation = $this->parseAnnotation($propertyReflection, 'var');
$varAnnotation = $varAnnotation ? (is_array($varAnnotation) ? (count($varAnnotation) ? end($varAnnotation) : NULL) : $varAnnotation) : NULL;

$className = $varAnnotation;

Expand Down Expand Up @@ -412,24 +412,19 @@ private function findAttributeName(Entities\IEntity $entity, string $className)
* @param Reflector $ref
* @param string $name
*
* @return array|NULL
* @return string|NULL
*/
private function parseAnnotation(Reflector $ref, string $name) : ?array
private function parseAnnotation(Reflector $ref, string $name) : ?string
{
if (!preg_match_all('#[\s*]@' . preg_quote($name, '#') . '(?:\(\s*([^)]*)\s*\)|\s|$)#', (string) $ref->getDocComment(), $m)) {
return NULL;
}

static $tokens = ['true' => TRUE, 'false' => FALSE, 'null' => NULL];

$res = [];
$factory = phpDocumentor\Reflection\DocBlockFactory::createInstance();
$docblock = $factory->create($ref->getDocComment());

foreach ($m[1] as $s) {
foreach (preg_split('#\s*,\s*#', $s, -1, PREG_SPLIT_NO_EMPTY) ?: ['true'] as $item) {
$res[] = array_key_exists($tmp = strtolower($item), $tokens) ? $tokens[$tmp] : $item;
foreach ($docblock->getTags() as $tag) {
if ($tag->getName() === $name) {
return trim((string) $tag);
}
}

return $res;
return NULL;
}
}
7 changes: 5 additions & 2 deletions tests/php.ini-unix
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
extension = xdebug.so
extension = json.so
extension = pdo.so
extension = pdo_sqlite.so
extension = sqlite.so
extension = iconv.so
extension = tokenizer.so
extension = json.so
extension = ctype.so
extension = xdebug.so

[xdebug]
xdebug.enable = On
9 changes: 6 additions & 3 deletions tests/php.ini-win
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[PHP]
extension_dir = "./ext"
extension = php_xdebug.dll
extension = php_json.dll
extension = php_pdo.dll
extension = php_pdo_sqlite.dll
extension = php_sqlite..dll
extension = php_sqlite.dll
extension = iconv.dll
extension = tokenizer.dll
extension = json.dll
extension = ctype.dll
extension = xdebug.dll

0 comments on commit f03bc41

Please sign in to comment.