*/ private array $attributes; public function __construct(DiscoveredAttribute ...$attributes) { $this->attributes = $attributes; } public function add(DiscoveredAttribute $attribute): self { $newAttributes = [...$this->attributes, $attribute]; return new self(...$newAttributes); } /** * @return ArrayIterator */ public function getIterator(): ArrayIterator { return new ArrayIterator($this->attributes); } public function count(): int { return count($this->attributes); } public function isEmpty(): bool { return empty($this->attributes); } /** * Filter by attribute class */ public function filterByAttributeClass(string $attributeClass): self { $filtered = array_filter( $this->attributes, fn (DiscoveredAttribute $attr) => $attr->attributeClass === $attributeClass ); return new self(...$filtered); } /** * Filter by target type */ public function filterByTarget(AttributeTarget $target): self { $filtered = array_filter( $this->attributes, fn (DiscoveredAttribute $attr) => $attr->target === $target ); return new self(...$filtered); } /** * Get all class attributes */ public function getClassAttributes(): self { return $this->filterByTarget(AttributeTarget::TARGET_CLASS); } /** * Get all method attributes */ public function getMethodAttributes(): self { return $this->filterByTarget(AttributeTarget::METHOD); } /** * Convert to array for backwards compatibility * @deprecated Use object methods instead * @return array>> */ public function toLegacyArray(): array { $result = []; foreach ($this->attributes as $attribute) { $attributeClass = $attribute->attributeClass; if (! isset($result[$attributeClass])) { $result[$attributeClass] = []; } $result[$attributeClass][] = $attribute->toArray(); } return $result; } /** * @return array */ public function toArray(): array { return $this->attributes; } }