|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository |
| 5 | + * @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md |
| 6 | + * @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace OpenCodeModeling\CodeAst\Builder; |
| 12 | + |
| 13 | +use Countable; |
| 14 | +use Iterator; |
| 15 | + |
| 16 | +final class FileCollection implements Iterator, Countable |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var array<string, File> |
| 20 | + */ |
| 21 | + private $items; |
| 22 | + |
| 23 | + public static function fromItems(File ...$files): self |
| 24 | + { |
| 25 | + return new self(...$files); |
| 26 | + } |
| 27 | + |
| 28 | + public static function emptyList(): self |
| 29 | + { |
| 30 | + return new self(); |
| 31 | + } |
| 32 | + |
| 33 | + private function __construct(File ...$files) |
| 34 | + { |
| 35 | + foreach ($files as $file) { |
| 36 | + $this->items[$this->identifier($file)] = $file; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public function add(File $file): self |
| 41 | + { |
| 42 | + $this->items[$this->identifier($file)] = $file; |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + public function remove(File $file): self |
| 48 | + { |
| 49 | + unset($this->items[$this->identifier($file)]); |
| 50 | + |
| 51 | + return $this; |
| 52 | + } |
| 53 | + |
| 54 | + public function contains(File $file): bool |
| 55 | + { |
| 56 | + return isset($this->items[$this->identifier($file)]); |
| 57 | + } |
| 58 | + |
| 59 | + public function filter(callable $filter): self |
| 60 | + { |
| 61 | + return new self(...\array_values( |
| 62 | + \array_filter( |
| 63 | + $this->items, |
| 64 | + static function (File $file) use ($filter) { |
| 65 | + return $filter($file); |
| 66 | + } |
| 67 | + ) |
| 68 | + ) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return array<string, File> |
| 74 | + */ |
| 75 | + public function items(): array |
| 76 | + { |
| 77 | + return $this->items; |
| 78 | + } |
| 79 | + |
| 80 | + public function rewind(): void |
| 81 | + { |
| 82 | + \reset($this->items); |
| 83 | + } |
| 84 | + |
| 85 | + public function current(): File |
| 86 | + { |
| 87 | + return \current($this->items); |
| 88 | + } |
| 89 | + |
| 90 | + public function key(): string |
| 91 | + { |
| 92 | + return \key($this->items); |
| 93 | + } |
| 94 | + |
| 95 | + public function next(): void |
| 96 | + { |
| 97 | + \next($this->items); |
| 98 | + } |
| 99 | + |
| 100 | + public function valid(): bool |
| 101 | + { |
| 102 | + return \key($this->items) !== null; |
| 103 | + } |
| 104 | + |
| 105 | + public function count(): int |
| 106 | + { |
| 107 | + return \count($this->items); |
| 108 | + } |
| 109 | + |
| 110 | + private function identifier(File $file): string |
| 111 | + { |
| 112 | + $namespace = $file->getNamespace() !== null ? ('\\' . $file->getNamespace()) : ''; |
| 113 | + $name = $file->getName() !== null ? ('\\' . $file->getName()) : ''; |
| 114 | + |
| 115 | + $identifier = $namespace . $name; |
| 116 | + |
| 117 | + if ($identifier === '') { |
| 118 | + return \spl_object_hash($file); |
| 119 | + } |
| 120 | + |
| 121 | + return $identifier; |
| 122 | + } |
| 123 | +} |
0 commit comments