|
1 |
| -<?php namespace Sikessem; |
| 1 | +<?php |
2 | 2 |
|
3 |
| -class Map { |
| 3 | +namespace Sikessem; |
4 | 4 |
|
5 |
| - public function __construct(string $keys_type, string $values_type) { |
| 5 | +class Map |
| 6 | +{ |
| 7 | + public function __construct(string $keys_type, string $values_type) |
| 8 | + { |
6 | 9 |
|
7 |
| - $keys_type = Filter::sanitize($keys_type); |
8 |
| - if(!Filter::validate($keys_type)) throw new Error("Unknown type $keys_type", Error::INVALID_TYPE); |
9 |
| - $this->keys_type = $keys_type; |
| 10 | + $keys_type = Filter::sanitize($keys_type); |
| 11 | + if (! Filter::validate($keys_type)) { |
| 12 | + throw new Error("Unknown type $keys_type", Error::INVALID_TYPE); |
| 13 | + } |
| 14 | + $this->keys_type = $keys_type; |
10 | 15 |
|
11 |
| - $values_type = Filter::sanitize($values_type); |
12 |
| - if(!Filter::validate($values_type)) throw new Error("Unknown type $values_type", Error::INVALID_TYPE); |
13 |
| - $this->values_type = $values_type; |
14 |
| - } |
| 16 | + $values_type = Filter::sanitize($values_type); |
| 17 | + if (! Filter::validate($values_type)) { |
| 18 | + throw new Error("Unknown type $values_type", Error::INVALID_TYPE); |
| 19 | + } |
| 20 | + $this->values_type = $values_type; |
| 21 | + } |
15 | 22 |
|
16 |
| - protected int $count = 0; |
| 23 | + protected int $count = 0; |
17 | 24 |
|
18 |
| - protected string $keys_type; |
19 |
| - protected array $keys_list = []; |
| 25 | + protected string $keys_type; |
20 | 26 |
|
21 |
| - protected string $values_type; |
22 |
| - protected array $values_list = []; |
| 27 | + protected array $keys_list = []; |
23 | 28 |
|
24 |
| - public function set(mixed $key, mixed $value): static { |
| 29 | + protected string $values_type; |
25 | 30 |
|
26 |
| - $key_type = Filter::sanitize(gettype($key)); |
27 |
| - if($key_type !== 'mixed' && $key_type !== $this->keys_type) throw new Error("Invalid key type ($key_type) given", Error::INVALID_KEY); |
| 31 | + protected array $values_list = []; |
28 | 32 |
|
29 |
| - $value_type = Filter::sanitize(gettype($value)); |
30 |
| - if($value_type !== 'mixed' && $value_type !== $this->values_type) throw new Error("Invalid value type ($value_type) given", Error::INVALID_VALUE); |
| 33 | + public function set(mixed $key, mixed $value): static |
| 34 | + { |
31 | 35 |
|
32 |
| - if(false === ($index = array_search($key, $this->keys_list, true))) $index = $this->count++; |
| 36 | + $key_type = Filter::sanitize(gettype($key)); |
| 37 | + if ($key_type !== 'mixed' && $key_type !== $this->keys_type) { |
| 38 | + throw new Error("Invalid key type ($key_type) given", Error::INVALID_KEY); |
| 39 | + } |
33 | 40 |
|
34 |
| - $this->keys_list[$index] = $key; |
35 |
| - $this->values_list[$index] = $value; |
| 41 | + $value_type = Filter::sanitize(gettype($value)); |
| 42 | + if ($value_type !== 'mixed' && $value_type !== $this->values_type) { |
| 43 | + throw new Error("Invalid value type ($value_type) given", Error::INVALID_VALUE); |
| 44 | + } |
36 | 45 |
|
37 |
| - return $this; |
38 |
| - } |
| 46 | + if (false === ($index = array_search($key, $this->keys_list, true))) { |
| 47 | + $index = $this->count++; |
| 48 | + } |
39 | 49 |
|
40 |
| - public function get(mixed $key): mixed { |
| 50 | + $this->keys_list[$index] = $key; |
| 51 | + $this->values_list[$index] = $value; |
41 | 52 |
|
42 |
| - return false === ($index = array_search($key, $this->keys_list, true))? null: $this->values_list[$index]; |
43 |
| - } |
| 53 | + return $this; |
| 54 | + } |
44 | 55 |
|
45 |
| - public function has(mixed $key): bool { |
| 56 | + public function get(mixed $key): mixed |
| 57 | + { |
46 | 58 |
|
47 |
| - return in_array($key, $this->keys_list, true); |
48 |
| - } |
| 59 | + return false === ($index = array_search($key, $this->keys_list, true)) ? null : $this->values_list[$index]; |
| 60 | + } |
| 61 | + |
| 62 | + public function has(mixed $key): bool |
| 63 | + { |
| 64 | + |
| 65 | + return in_array($key, $this->keys_list, true); |
| 66 | + } |
49 | 67 | }
|
0 commit comments