larapie /
data-transfer-object
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Larapie\DataTransferObject\Resolvers; |
||
| 4 | |||
| 5 | use Throwable; |
||
| 6 | use ReflectionProperty; |
||
| 7 | use Symfony\Component\Validator\Constraint; |
||
| 8 | use Larapie\DataTransferObject\Annotations\Inherit; |
||
| 9 | use Larapie\DataTransferObject\Exceptions\ConstraintInheritanceException; |
||
| 10 | |||
| 11 | class ConstraintsResolver |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var ReflectionProperty |
||
| 15 | */ |
||
| 16 | protected $reflection; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $annotations; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * TypeResolver constructor. |
||
| 25 | * @param ReflectionProperty $reflection |
||
| 26 | * @param array $annotations |
||
| 27 | */ |
||
| 28 | 36 | final public function __construct(ReflectionProperty $reflection, array $annotations) |
|
| 29 | { |
||
| 30 | 36 | $this->reflection = $reflection; |
|
| 31 | 36 | $this->annotations = $annotations; |
|
| 32 | 36 | } |
|
| 33 | |||
| 34 | 36 | public function resolve(): array |
|
| 35 | { |
||
| 36 | 36 | $constraints = []; |
|
| 37 | 36 | foreach ($this->annotations as $annotation) { |
|
| 38 | 7 | if ($annotation instanceof Inherit) { |
|
| 39 | 1 | $constraints = array_merge($constraints, $this->getParentConstraints()); |
|
| 40 | 7 | } elseif ($annotation instanceof Constraint) { |
|
| 41 | 7 | $constraints[] = $annotation; |
|
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | 36 | return $constraints; |
|
| 46 | } |
||
| 47 | |||
| 48 | 1 | protected function getParentConstraints() |
|
| 49 | { |
||
| 50 | try { |
||
| 51 | 1 | if ($parentClass = $this->reflection->getDeclaringClass()->getParentClass()) { |
|
| 52 | 1 | $parentProperty = $parentClass->getProperty($this->reflection->getName()); |
|
| 53 | } |
||
| 54 | } catch (Throwable $exception) { |
||
| 55 | throw new ConstraintInheritanceException('There is no parent property to inherit from'); |
||
| 56 | } |
||
| 57 | |||
| 58 | 1 | return (new static($parentProperty, (new AnnotationResolver($parentProperty))->resolve()))->resolve(); |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 59 | } |
||
| 60 | } |
||
| 61 |