Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| AbstractCustomFields | |
0.00% |
0 / 28 |
|
0.00% |
0 / 12 |
240 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| set | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| unset | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| merge | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| replace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| remove | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRawData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setRawData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getEncoder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setEncoder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Satag\AmicronEntityBundle\Util\CustomFields; |
| 6 | |
| 7 | abstract class AbstractCustomFields |
| 8 | { |
| 9 | protected string $getter; |
| 10 | |
| 11 | protected string $setter; |
| 12 | |
| 13 | protected object $entity; |
| 14 | |
| 15 | protected string $dataIdentifier; |
| 16 | |
| 17 | protected AbstractCustomFieldsEncoder $encoder; |
| 18 | |
| 19 | /** |
| 20 | * @throws CustomFieldsException |
| 21 | */ |
| 22 | public function __construct(object $entity, string $fieldName, string $dataIdentifier) |
| 23 | { |
| 24 | $this->getter = 'get' . ucfirst($fieldName); |
| 25 | $this->setter = 'set' . ucfirst($fieldName); |
| 26 | if (!method_exists($entity, $this->getter)) { |
| 27 | throw new CustomFieldsException(\sprintf('Entity %s hat keine %s-Methode', $entity::class, $this->getter)); |
| 28 | } |
| 29 | if (!method_exists($entity, $this->setter)) { |
| 30 | throw new CustomFieldsException(\sprintf('Entity %s hat keine %s-Methode', $entity::class, $this->getter)); |
| 31 | } |
| 32 | if (preg_match('/^[a-zA-Z0-9ÄÖÜäöüß\-]+$/', $dataIdentifier) !== 1) { |
| 33 | throw new CustomFieldsException(\sprintf('Der DataIdentifier "%s" ist ungültig. Verwenden Sie nur Buchstaben, Zahlen und Bindestrich (-) sowie den Unterstrich (_). ', $dataIdentifier)); |
| 34 | } |
| 35 | |
| 36 | $this->entity = $entity; |
| 37 | $this->dataIdentifier = $dataIdentifier; |
| 38 | } |
| 39 | |
| 40 | public function set(string $key, mixed $value): void |
| 41 | { |
| 42 | $array = $this->getEncoder()->decode($this->getRawData()); |
| 43 | $array[$key] = $value; |
| 44 | $this->setRawData($this->getEncoder()->encode($this->getRawData(), $array)); |
| 45 | } |
| 46 | |
| 47 | public function unset(string $key): void |
| 48 | { |
| 49 | $array = $this->getEncoder()->decode($this->getRawData()); |
| 50 | unset($array[$key]); |
| 51 | $this->setRawData($this->getEncoder()->encode($this->getRawData(), $array)); |
| 52 | } |
| 53 | |
| 54 | public function get(string $key): mixed |
| 55 | { |
| 56 | return $this->getArray()[$key] ?? null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param array<string, mixed> $arrayToMerge |
| 61 | * |
| 62 | * @return array<string, mixed> |
| 63 | */ |
| 64 | public function merge(array $arrayToMerge): array |
| 65 | { |
| 66 | $currentArray = $this->getEncoder()->decode($this->getRawData()); |
| 67 | $result = array_replace_recursive($currentArray, $arrayToMerge); |
| 68 | $this->setRawData($this->getEncoder()->encode($this->getRawData(), $result)); |
| 69 | |
| 70 | return $result; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param array<string, mixed> $replacement |
| 75 | */ |
| 76 | public function replace(array $replacement): void |
| 77 | { |
| 78 | $this->setRawData($this->getEncoder()->encode($this->getRawData(), $replacement)); |
| 79 | } |
| 80 | |
| 81 | public function remove(): void |
| 82 | { |
| 83 | $this->setRawData($this->getEncoder()->removeBlock($this->getRawData())); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return array<string, mixed> |
| 88 | */ |
| 89 | public function getArray(): array |
| 90 | { |
| 91 | return $this->getEncoder()->decode($this->getRawData()); |
| 92 | } |
| 93 | |
| 94 | protected function getRawData(): string |
| 95 | { |
| 96 | return (string) $this->entity->{$this->getter}(); |
| 97 | } |
| 98 | |
| 99 | protected function setRawData(string $data): void |
| 100 | { |
| 101 | $this->entity->{$this->setter}($data); |
| 102 | } |
| 103 | |
| 104 | protected function getEncoder(): AbstractCustomFieldsEncoder |
| 105 | { |
| 106 | return $this->encoder; |
| 107 | } |
| 108 | |
| 109 | protected function setEncoder(AbstractCustomFieldsEncoder $encoder): void |
| 110 | { |
| 111 | $this->encoder = $encoder; |
| 112 | } |
| 113 | } |