Files
michaelschiemer/src/Framework/Discovery/ValueObjects/AttributeCollection.php
Michael Schiemer 55a330b223 Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug
- Add DISCOVERY_SHOW_PROGRESS=true
- Temporary changes for debugging InitializerProcessor fixes on production
2025-08-11 20:13:26 +02:00

121 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Discovery\ValueObjects;
use ArrayIterator;
use Countable;
use IteratorAggregate;
/**
* Type-safe collection of discovered attributes
*/
final readonly class AttributeCollection implements IteratorAggregate, Countable
{
/** @var array<DiscoveredAttribute> */
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<int, DiscoveredAttribute>
*/
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<string, array<array<string, mixed>>>
*/
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<DiscoveredAttribute>
*/
public function toArray(): array
{
return $this->attributes;
}
}