Skip to content

Commit 29be10d

Browse files
HP-2557: Created PriceTypeCollection for simplified work with PriceTypeInterface
1 parent 439554f commit 29be10d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)