|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Model\Response\Metadata; |
| 6 | + |
| 7 | +/** |
| 8 | + * @implements \IteratorAggregate<string, mixed> |
| 9 | + * @implements \ArrayAccess<string, mixed> |
| 10 | + */ |
| 11 | +class Metadata implements \JsonSerializable, \Countable, \IteratorAggregate, \ArrayAccess |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var array<string, mixed> |
| 15 | + */ |
| 16 | + private array $metadata = []; |
| 17 | + |
| 18 | + /** |
| 19 | + * @param array<string, mixed> $metadata |
| 20 | + */ |
| 21 | + public function __construct(array $metadata = []) |
| 22 | + { |
| 23 | + $this->set($metadata); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @return array<string, mixed> |
| 28 | + */ |
| 29 | + public function all(): array |
| 30 | + { |
| 31 | + return $this->metadata; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param array<string, mixed> $metadata |
| 36 | + */ |
| 37 | + public function set(array $metadata): void |
| 38 | + { |
| 39 | + $this->metadata = $metadata; |
| 40 | + } |
| 41 | + |
| 42 | + public function add(string $key, mixed $value): void |
| 43 | + { |
| 44 | + $this->metadata[$key] = $value; |
| 45 | + } |
| 46 | + |
| 47 | + public function has(string $key): bool |
| 48 | + { |
| 49 | + return \array_key_exists($key, $this->metadata); |
| 50 | + } |
| 51 | + |
| 52 | + public function get(string $key, mixed $default = null): mixed |
| 53 | + { |
| 54 | + return $this->metadata[$key] ?? $default; |
| 55 | + } |
| 56 | + |
| 57 | + public function remove(string $key): void |
| 58 | + { |
| 59 | + unset($this->metadata[$key]); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return array<string, mixed> |
| 64 | + */ |
| 65 | + public function jsonSerialize(): array |
| 66 | + { |
| 67 | + return $this->all(); |
| 68 | + } |
| 69 | + |
| 70 | + public function count(): int |
| 71 | + { |
| 72 | + return \count($this->metadata); |
| 73 | + } |
| 74 | + |
| 75 | + public function getIterator(): \Traversable |
| 76 | + { |
| 77 | + return new \ArrayIterator($this->metadata); |
| 78 | + } |
| 79 | + |
| 80 | + public function offsetExists(mixed $offset): bool |
| 81 | + { |
| 82 | + return $this->has((string) $offset); |
| 83 | + } |
| 84 | + |
| 85 | + public function offsetGet(mixed $offset): mixed |
| 86 | + { |
| 87 | + return $this->get((string) $offset); |
| 88 | + } |
| 89 | + |
| 90 | + public function offsetSet(mixed $offset, mixed $value): void |
| 91 | + { |
| 92 | + $this->add((string) $offset, $value); |
| 93 | + } |
| 94 | + |
| 95 | + public function offsetUnset(mixed $offset): void |
| 96 | + { |
| 97 | + $this->remove((string) $offset); |
| 98 | + } |
| 99 | +} |
0 commit comments