Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
40 / 40 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| MetadataFieldHashmap | |
100.00% |
40 / 40 |
|
100.00% |
4 / 4 |
12 | |
100.00% |
1 / 1 |
| offsetGet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| filterOnContext | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
7 | |||
| sort | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| getPrioFromKey | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Context; |
| 3 | |
| 4 | use Apie\Core\Lists\ItemHashmap; |
| 5 | use Apie\Core\Metadata\Fields\FieldInterface; |
| 6 | use Apie\Core\Metadata\GetterInterface; |
| 7 | use Apie\Core\Metadata\SetterInterface; |
| 8 | |
| 9 | /** |
| 10 | * Contains a list of methods and/or properties. |
| 11 | */ |
| 12 | final class MetadataFieldHashmap extends ItemHashmap |
| 13 | { |
| 14 | public function offsetGet(mixed $offset): FieldInterface |
| 15 | { |
| 16 | return parent::offsetGet($offset); |
| 17 | } |
| 18 | |
| 19 | public function filterOnContext(ApieContext $apieContext, ?bool $getters = null, ?bool $setters = null): self |
| 20 | { |
| 21 | $list = array_filter( |
| 22 | $this->internalArray, |
| 23 | function (FieldInterface $field) use ($apieContext, $getters, $setters) { |
| 24 | if ($getters !== null) { |
| 25 | $fieldIsGetter = $field instanceof GetterInterface && 'never' !== (string) $field->getTypehint(); |
| 26 | if ($getters xor $fieldIsGetter) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | } |
| 31 | if ($setters !== null) { |
| 32 | $fieldIsSetter = $field instanceof SetterInterface && 'never' !== (string) $field->getTypehint(); |
| 33 | if ($setters xor $fieldIsSetter) { |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | return $field->appliesToContext($apieContext); |
| 38 | } |
| 39 | ); |
| 40 | |
| 41 | return new self($list); |
| 42 | } |
| 43 | |
| 44 | public function sort(): self |
| 45 | { |
| 46 | $arrayCopy = array_map( |
| 47 | function (FieldInterface $value, string $key) { |
| 48 | $prioFromType = $value->getFieldPriority(); |
| 49 | return [$value, $prioFromType ?? $this->getPrioFromKey($key), $key]; |
| 50 | }, |
| 51 | $this->internalArray, |
| 52 | array_keys($this->internalArray) |
| 53 | ); |
| 54 | usort($arrayCopy, function (array $input1, array $input2) { |
| 55 | return $input1[1] <=> $input2[1]; |
| 56 | }); |
| 57 | $newArray = []; |
| 58 | foreach ($arrayCopy as $input) { |
| 59 | $newArray[$input[2]] = $input[0]; |
| 60 | } |
| 61 | return new self($newArray); |
| 62 | } |
| 63 | |
| 64 | private function getPrioFromKey(string $input): int |
| 65 | { |
| 66 | if (stripos($input, 'status') !== false) { |
| 67 | return -150; |
| 68 | } |
| 69 | $ratings = [ |
| 70 | 'id' => -300, |
| 71 | 'name' => -250, |
| 72 | 'email' => -200, |
| 73 | 'description' => 100, |
| 74 | ]; |
| 75 | return $ratings[$input] ?? 0; |
| 76 | } |
| 77 | } |