File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace hiqdev \php \billing \product \price ;
6+
7+ use Countable ;
8+ use IteratorAggregate ;
9+ use Traversable ;
10+
11+ class PriceTypeCollection implements IteratorAggregate, Countable
12+ {
13+ /**
14+ * @var string[] - flipped types for fast search
15+ */
16+ private array $ flippedTypes ;
17+
18+ public function __construct (private readonly array $ types = [])
19+ {
20+ $ this ->flippedTypes = array_flip (array_map (fn (PriceTypeInterface $ t ) => $ t ->name (), $ types ));
21+ }
22+
23+ /**
24+ * @return Traversable<int, PriceTypeInterface>
25+ */
26+ public function getIterator (): Traversable
27+ {
28+ return new \ArrayIterator ($ this ->types );
29+ }
30+
31+ public function has (string $ priceType ): bool
32+ {
33+ return array_key_exists ($ priceType , $ this ->flippedTypes );
34+ }
35+
36+ public function count (): int
37+ {
38+ return count ($ this ->types );
39+ }
40+
41+ public function hasItems (): bool
42+ {
43+ return $ this ->count () > 0 ;
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments