|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Validator\DataSet; |
| 6 | + |
| 7 | +use ReflectionAttribute; |
| 8 | +use ReflectionObject; |
| 9 | +use ReflectionProperty; |
| 10 | +use Yiisoft\Validator\DataSetInterface; |
| 11 | +use Yiisoft\Validator\RuleInterface; |
| 12 | +use Yiisoft\Validator\RulesProviderInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * This data set makes use of attributes introduced in PHP 8. It simplifies rules configuration process, especially for |
| 16 | + * nested data and relations. Please refer to the guide for example. |
| 17 | + * |
| 18 | + * @link https://www.php.net/manual/en/language.attributes.overview.php |
| 19 | + */ |
| 20 | +final class ObjectDataSet implements RulesProviderInterface, DataSetInterface |
| 21 | +{ |
| 22 | + private object $object; |
| 23 | + |
| 24 | + private bool $dataSetProvided; |
| 25 | + |
| 26 | + private int $propertyVisibility; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ReflectionProperty[] |
| 30 | + */ |
| 31 | + private array $reflectionProperties = []; |
| 32 | + |
| 33 | + private iterable $rules; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + object $object, |
| 37 | + int $propertyVisibility = ReflectionProperty::IS_PRIVATE|ReflectionProperty::IS_PROTECTED|ReflectionProperty::IS_PUBLIC |
| 38 | + ) { |
| 39 | + $this->object = $object; |
| 40 | + $this->propertyVisibility = $propertyVisibility; |
| 41 | + $this->parseObject(); |
| 42 | + } |
| 43 | + |
| 44 | + public function getRules(): iterable |
| 45 | + { |
| 46 | + return $this->rules; |
| 47 | + } |
| 48 | + |
| 49 | + public function getAttributeValue(string $attribute): mixed |
| 50 | + { |
| 51 | + if ($this->dataSetProvided) { |
| 52 | + return $this->object->getAttributeValue($attribute); |
| 53 | + } |
| 54 | + |
| 55 | + return isset($this->reflectionProperties[$attribute]) |
| 56 | + ? $this->reflectionProperties[$attribute]->getValue($this->object) |
| 57 | + : null; |
| 58 | + } |
| 59 | + |
| 60 | + public function getData(): array |
| 61 | + { |
| 62 | + if ($this->dataSetProvided) { |
| 63 | + return $this->object->getData(); |
| 64 | + } |
| 65 | + |
| 66 | + $data = []; |
| 67 | + foreach ($this->reflectionProperties as $name => $property) { |
| 68 | + $data[$name] = $property->getValue($this->object); |
| 69 | + } |
| 70 | + return $data; |
| 71 | + } |
| 72 | + |
| 73 | + // TODO: use Generator to collect attributes |
| 74 | + private function parseObject(): void |
| 75 | + { |
| 76 | + $objectProvidedRules = $this->object instanceof RulesProviderInterface; |
| 77 | + $this->dataSetProvided = $this->object instanceof DataSetInterface; |
| 78 | + |
| 79 | + $this->rules = $objectProvidedRules ? $this->object->getRules() : []; |
| 80 | + |
| 81 | + if ($this->dataSetProvided) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $reflection = new ReflectionObject($this->object); |
| 86 | + foreach ($reflection->getProperties() as $property) { |
| 87 | + if (!$this->isUseProperty($property)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + if (PHP_VERSION_ID < 80100) { |
| 92 | + $property->setAccessible(true); |
| 93 | + } |
| 94 | + $this->reflectionProperties[$property->getName()] = $property; |
| 95 | + |
| 96 | + if (!$objectProvidedRules) { |
| 97 | + $attributes = $property->getAttributes(RuleInterface::class, ReflectionAttribute::IS_INSTANCEOF); |
| 98 | + foreach ($attributes as $attribute) { |
| 99 | + $this->rules[$property->getName()][] = $attribute->newInstance(); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private function isUseProperty(ReflectionProperty $property): bool |
| 106 | + { |
| 107 | + return ($property->isPublic() && ($this->propertyVisibility&ReflectionProperty::IS_PUBLIC)) |
| 108 | + || ($property->isPrivate() && ($this->propertyVisibility&ReflectionProperty::IS_PRIVATE)) |
| 109 | + || ($property->isProtected() && ($this->propertyVisibility&ReflectionProperty::IS_PROTECTED)); |
| 110 | + } |
| 111 | +} |
0 commit comments