|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Setono\SyliusMeilisearchPlugin\Engine; |
| 6 | + |
| 7 | +use Webmozart\Assert\Assert; |
| 8 | + |
| 9 | +/** |
| 10 | + * @implements \ArrayAccess<string, int> |
| 11 | + * @implements \IteratorAggregate<string, int> |
| 12 | + */ |
| 13 | +final class FacetValues implements \Countable, \IteratorAggregate, \ArrayAccess |
| 14 | +{ |
| 15 | + /** |
| 16 | + * This holds the facet values for the respective facet and search results. Examples for $values include: |
| 17 | + * |
| 18 | + * [ |
| 19 | + * "Celsius Small" => 3 |
| 20 | + * "Date & Banana" => 2 |
| 21 | + * "Modern Wear" => 6 |
| 22 | + * "You are breathtaking" => 10 |
| 23 | + * ] |
| 24 | + * |
| 25 | + * [ |
| 26 | + * "false" => 16 |
| 27 | + * "true" => 5 |
| 28 | + * ] |
| 29 | + * |
| 30 | + * [ |
| 31 | + * "1.74" => 1 |
| 32 | + * "12.21" => 1 |
| 33 | + * "16.52" => 1 |
| 34 | + * "21.06" => 1 |
| 35 | + * "22.49" => 1 |
| 36 | + * "24.19" => 1 |
| 37 | + * ] |
| 38 | + * |
| 39 | + * @var array<string, int> |
| 40 | + */ |
| 41 | + private array $values = []; |
| 42 | + |
| 43 | + public function __construct( |
| 44 | + public readonly string $name, |
| 45 | + array $values, |
| 46 | + ) { |
| 47 | + foreach ($values as $key => $value) { |
| 48 | + if (!is_string($key) || !is_int($value)) { |
| 49 | + throw new \InvalidArgumentException(sprintf( |
| 50 | + 'The values of the facet must be an array of strings and integers. Input array was: %s', |
| 51 | + json_encode($values, \JSON_THROW_ON_ERROR), |
| 52 | + )); |
| 53 | + } |
| 54 | + |
| 55 | + $this->values[$key] = $value; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return list<string> |
| 61 | + */ |
| 62 | + public function getValues(): array |
| 63 | + { |
| 64 | + return array_keys($this->values); |
| 65 | + } |
| 66 | + |
| 67 | + public function getValueCount(string $value): int |
| 68 | + { |
| 69 | + if (!$this->has($value)) { |
| 70 | + throw new \InvalidArgumentException(sprintf('Facet value "%s" does not exist', $value)); |
| 71 | + } |
| 72 | + |
| 73 | + return $this->values[$value]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @psalm-assert-if-true int $this->values[$value] |
| 78 | + */ |
| 79 | + public function has(string $value): bool |
| 80 | + { |
| 81 | + return isset($this->values[$value]); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return \ArrayIterator<string, int> |
| 86 | + */ |
| 87 | + public function getIterator(): \ArrayIterator |
| 88 | + { |
| 89 | + return new \ArrayIterator($this->values); |
| 90 | + } |
| 91 | + |
| 92 | + public function offsetExists(mixed $offset): bool |
| 93 | + { |
| 94 | + return $this->has($offset); |
| 95 | + } |
| 96 | + |
| 97 | + public function offsetGet(mixed $offset): int |
| 98 | + { |
| 99 | + return $this->values[$offset]; |
| 100 | + } |
| 101 | + |
| 102 | + public function offsetSet(mixed $offset, mixed $value): void |
| 103 | + { |
| 104 | + throw new \LogicException('You cannot set an offset'); |
| 105 | + } |
| 106 | + |
| 107 | + public function offsetUnset(mixed $offset): void |
| 108 | + { |
| 109 | + throw new \LogicException('You cannot unset an offset'); |
| 110 | + } |
| 111 | + |
| 112 | + public function count(): int |
| 113 | + { |
| 114 | + return count($this->values); |
| 115 | + } |
| 116 | + |
| 117 | + public function isEmpty(): bool |
| 118 | + { |
| 119 | + return [] === $this->values; |
| 120 | + } |
| 121 | +} |
0 commit comments