for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\DataSet;
use ReflectionAttribute;
use ReflectionClass;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\RulesProviderInterface;
/**
* This data set makes use of attributes introduced in PHP 8. It simplifies rules configuration process, especially for
* nested data and relations. Please refer to the guide for example.
*
* @link https://www.php.net/manual/en/language.attributes.overview.php
*/
final class AttributeDataSet implements RulesProviderInterface
{
use ArrayDataTrait;
private object $baseAnnotatedObject;
public function __construct(object $baseAnnotatedObject, array $data = [])
$this->baseAnnotatedObject = $baseAnnotatedObject;
$this->data = $data;
}
public function getRules(): iterable
$classMeta = new ReflectionClass($this->baseAnnotatedObject);
return $this->collectAttributes($classMeta);
// TODO: use Generator to collect attributes
private function collectAttributes(ReflectionClass $classMeta): array
$rules = [];
foreach ($classMeta->getProperties() as $property) {
$attributes = $property->getAttributes(RuleInterface::class, ReflectionAttribute::IS_INSTANCEOF);
foreach ($attributes as $attribute) {
/** @psalm-suppress UndefinedMethod */
$rules[$property->getName()][] = $attribute->newInstance();
return $rules;