Lines
89.34%
109 / 122
Methods
79.41%
27 / 34
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| exchangeArray | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| offsetSet | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getUnflattenedArray | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| getDefaults | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getValidator | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | ||
| isValid | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| testValid | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| createExample | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| getExample | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| readJsonSchema | 66.66% | 6 / 9 | 0.00% | 0 / 1 | 3.33 | ||
| getEntityName | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| setJsonCompressLevel | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| jsonSerialize | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 2 | ||
| __toString | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| __get | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| __isset | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| __set | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| __unset | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| __clone | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | ||
| addData | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 6 | ||
| withData | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | ||
| hasId | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| getId | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 3 | ||
| toProperty | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| hasProperty | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getProperty | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| withProperty | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | ||
| withLessData | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| withCleanedUpFormData | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| getResolveLevel | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| setResolveLevel | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| withResolveLevel | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 5 | ||
| withReference | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| 16 | class Entity extends \ArrayObject implements \JsonSerializable | |
| 17 | { | |
| 18 | /** | |
| 19 | * primary id for entity | |
| 20 | * | |
| 21 | * @var string | |
| 22 | */ | |
| 23 | public const string PRIMARY = 'id'; | |
| 24 | ||
| 25 | /** | |
| 26 | * @var string|null $schema Filename of JSON-Schema file | |
| 27 | */ | |
| 28 | public static $schema = null; | |
| 29 | ||
| 30 | /** | |
| 31 | * @var String $schema Filename of JSON-Schema file | |
| 32 | */ | |
| 33 | public static $schemaRefPrefix = ''; | |
| 34 | ||
| 35 | /** | |
| 36 | * @var \ArrayObject|null $jsonSchema JSON-Schema definition to validate data | |
| 37 | */ | |
| 38 | protected $jsonSchema = null; | |
| 39 | ||
| 40 | /** | |
| 41 | * @var Int $jsonCompressLevel | |
| 42 | */ | |
| 43 | protected $jsonCompressLevel = 0; | |
| 44 | ||
| 45 | /** | |
| 46 | * @var Array $schemaCache for not loading and interpreting a schema twice | |
| 47 | * | |
| 48 | */ | |
| 49 | protected static $schemaCache = []; | |
| 50 | ||
| 51 | /** | |
| 52 | * @var int|null $resolveLevel indicator on data integrity | |
| 53 | */ | |
| 54 | protected $resolveLevel = null; | |
| 55 | ||
| 56 | /** | |
| 57 | * Read the json schema and let array act like an object | |
| 58 | */ | |
| 59 | public function __construct(mixed $input = null, int $flags = \ArrayObject::ARRAY_AS_PROPS, string $iterator_class = "ArrayIterator") | |
| 60 | { | |
| 61 | /** @var class-string<\ArrayIterator> $iterator_class */ | |
| 62 | parent::__construct($this->getDefaults(), $flags, $iterator_class); | |
| 63 | if ($input) { | |
| 64 | $input = $this->getUnflattenedArray($input); | |
| 65 | $this->addData($input); | |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | #[\Override] | |
| 70 | public function exchangeArray(object|array $input): array | |
| 71 | { | |
| 72 | parent::exchangeArray($this->getDefaults()); | |
| 73 | $input = $this->getUnflattenedArray($input); | |
| 74 | $this->addData($input); | |
| 75 | return $this->getArrayCopy(); | |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * @param array-key|null $key | |
| 80 | */ | |
| 81 | #[\Override] | |
| 82 | public function offsetSet(mixed $key, mixed $value): void | |
| 83 | { | |
| 84 | /** @psalm-suppress PossiblyNullArgument */ | |
| 85 | parent::offsetSet($key, $value); | |
| 86 | } | |
| 87 | ||
| 88 | public function getUnflattenedArray(mixed $input): mixed | |
| 89 | { | |
| 90 | if (!$input instanceof UnflattedArray) { | |
| 91 | $input = new UnflattedArray($input); | |
| 92 | $input->getUnflattenedArray(); | |
| 93 | } | |
| 94 | $input = $input->getValue(); | |
| 95 | return $input; | |
| 96 | } | |
| 97 | /** | |
| 98 | * Set Default values | |
| 99 | * | |
| 100 | * @return array | |
| 101 | * | |
| 102 | */ | |
| 103 | public function getDefaults() | |
| 104 | { | |
| 105 | return []; | |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * This method is private, because the used library should not be used outside of this class! | |
| 110 | * | |
| 111 | */ | |
| 112 | public function getValidator(string $locale = 'de_DE', int $resolveLevel = 0): Validator | |
| 113 | { | |
| 114 | $jsonSchema = self::readJsonSchema()->withResolvedReferences($resolveLevel); | |
| 115 | $data = (new Schema($this))->withoutRefs(); | |
| 116 | if (Property::__keyExists('$schema', $data)) { | |
| 117 | unset($data['$schema']); | |
| 118 | } | |
| 119 | $validator = new Validator($data->toJsonObject(true), $jsonSchema, $locale); | |
| 120 | return $validator; | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Check if the given data validates against the given jsonSchema | |
| 125 | * | |
| 126 | * @return bool | |
| 127 | */ | |
| 128 | public function isValid(int $resolveLevel = 0): bool | |
| 129 | { | |
| 130 | $validator = $this->getValidator('de_DE', $resolveLevel = 0); | |
| 131 | return $validator->isValid(); | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Check if the given data validates against the given jsonSchema | |
| 136 | * | |
| 137 | * @throws \BO\Zmsentities\Exception\SchemaValidation | |
| 138 | * @return bool | |
| 139 | */ | |
| 140 | public function testValid(string $locale = 'de_DE', int $resolveLevel = 0): bool | |
| 141 | { | |
| 142 | $validator = $this->getValidator($locale, $resolveLevel); | |
| 143 | if (!$validator->isValid()) { | |
| 144 | $exception = new \BO\Zmsentities\Exception\SchemaValidation(); | |
| 145 | $exception->setSchemaName($this->getEntityName()); | |
| 146 | $exception->setValidationError($validator->getErrors()); | |
| 147 | throw $exception; | |
| 148 | } | |
| 149 | return true; | |
| 150 | } | |
| 151 | ||
| 152 | /** | |
| 153 | * create an example for testing | |
| 154 | * | |
| 155 | * @return self | |
| 156 | */ | |
| 157 | public static function createExample() | |
| 158 | { | |
| 159 | $object = new static(); | |
| 160 | return $object->getExample(); | |
| 161 | } | |
| 162 | ||
| 163 | /** | |
| 164 | * return a new object as example | |
| 165 | * | |
| 166 | * @return static | |
| 167 | */ | |
| 168 | public static function getExample() | |
| 169 | { | |
| 170 | $class = get_called_class(); | |
| 171 | $jsonSchema = self::readJsonSchema(); | |
| 172 | if ($jsonSchema->offsetExists('example')) { | |
| 173 | return new $class($jsonSchema['example']); | |
| 174 | } | |
| 175 | return new $class(); | |
| 176 | } | |
| 177 | ||
| 178 | protected static function readJsonSchema(): mixed | |
| 179 | { | |
| 180 | $class = get_called_class(); | |
| 181 | if (!array_key_exists($class, self::$schemaCache)) { | |
| 182 | $schemaFile = $class::$schema; | |
| 183 | if (!is_string($schemaFile)) { | |
| 184 | throw new \BO\Zmsentities\Exception\SchemaMissingJsonFile( | |
| 185 | 'Missing JSON-Schema file for ' . $class | |
| 186 | ); | |
| 187 | } | |
| 188 | self::$schemaCache[$class] = Loader::asArray($schemaFile); | |
| 189 | } | |
| 190 | return self::$schemaCache[$class]; | |
| 191 | } | |
| 192 | ||
| 193 | public function getEntityName(): string | |
| 194 | { | |
| 195 | $entity = get_class($this); | |
| 196 | $entity = preg_replace('#.*[\\\]#', '', $entity) ?? ''; | |
| 197 | $entity = strtolower($entity); | |
| 198 | return $entity; | |
| 199 | } | |
| 200 | ||
| 201 | public function setJsonCompressLevel(mixed $jsonCompressLevel): static | |
| 202 | { | |
| 203 | $this->jsonCompressLevel = $jsonCompressLevel; | |
| 204 | return $this; | |
| 205 | } | |
| 206 | ||
| 207 | #[\Override] | |
| 208 | public function jsonSerialize(): mixed | |
| 209 | { | |
| 210 | $schema = array( | |
| 211 | '$schema' => 'https://schema.berlin.de/queuemanagement/' . $this->getEntityName() . '.json' | |
| 212 | ); | |
| 213 | $schema = array_merge($schema, $this->getArrayCopy()); | |
| 214 | if ($this instanceof \BO\Zmsentities\Helper\NoSanitize) { | |
| 215 | $serialize = $schema; | |
| 216 | } else { | |
| 217 | $schema = new Schema($schema); | |
| 218 | $schema->setDefaults($this->getDefaults()); | |
| 219 | $schema->setJsonCompressLevel($this->jsonCompressLevel); | |
| 220 | $serialize = $schema->toJsonObject(); | |
| 221 | } | |
| 222 | return $serialize; | |
| 223 | } | |
| 224 | ||
| 225 | public function __toString() | |
| 226 | { | |
| 227 | $encoded = json_encode($this->jsonSerialize(), JSON_HEX_QUOT); | |
| 228 | return $encoded !== false ? $encoded : ''; | |
| 229 | } | |
| 230 | ||
| 231 | /** | |
| 232 | * Declared so Psalm treats ARRAY_AS_PROPS reads as magic. PHP uses ArrayObject | |
| 233 | * property handlers at runtime, so these methods are not invoked. | |
| 234 | */ | |
| 235 | public function __get(string $name): mixed | |
| 236 | { | |
| 237 | return $this->offsetGet($name); | |
| 238 | } | |
| 239 | ||
| 240 | public function __isset(string $name): bool | |
| 241 | { | |
| 242 | return $this->offsetExists($name); | |
| 243 | } | |
| 244 | ||
| 245 | public function __set(string $name, mixed $value): void | |
| 246 | { | |
| 247 | $this->offsetSet($name, $value); | |
| 248 | } | |
| 249 | ||
| 250 | public function __unset(string $name): void | |
| 251 | { | |
| 252 | $this->offsetUnset($name); | |
| 253 | } | |
| 254 | ||
| 255 | public function __clone() | |
| 256 | { | |
| 257 | foreach ($this as $key => $property) { | |
| 258 | if (is_object($property)) { | |
| 259 | $this[$key] = clone $property; | |
| 260 | } | |
| 261 | } | |
| 262 | } | |
| 263 | ||
| 264 | /** | |
| 265 | * Performs a merge with an iterable | |
| 266 | * Sub-entities are preserved | |
| 267 | */ | |
| 268 | public function addData(array|object $mergeData): static | |
| 269 | { | |
| 270 | foreach ($mergeData as $key => $item) { | |
| 271 | if (isset($this[$key])) { | |
| 272 | if ($this[$key] instanceof Entity) { | |
| 273 | $this[$key]->setResolveLevel(($this->getResolveLevel() ?? 0) - 1); | |
| 274 | $this[$key]->addData($item); | |
| 275 | } elseif ($this[$key] instanceof \BO\Zmsentities\Collection\Base) { | |
| 276 | $this[$key]->exchangeArray([]); | |
| 277 | $this[$key]->setResolveLevel(($this->getResolveLevel() ?? 0) - 1); | |
| 278 | $this[$key]->addData($item); | |
| 279 | } elseif (is_array($this[$key])) { | |
| 280 | $this[$key] = array_replace_recursive($this[$key], $item); | |
| 281 | } else { | |
| 282 | $this[$key] = $item; | |
| 283 | } | |
| 284 | } else { | |
| 285 | $this[$key] = $item; | |
| 286 | } | |
| 287 | } | |
| 288 | return $this; | |
| 289 | } | |
| 290 | ||
| 291 | /** | |
| 292 | * Performs addData on a cloned entity | |
| 293 | */ | |
| 294 | public function withData(mixed $mergeData): static | |
| 295 | { | |
| 296 | $entity = clone $this; | |
| 297 | $entity->addData($mergeData); | |
| 298 | return $entity; | |
| 299 | } | |
| 300 | ||
| 301 | public function hasId(): bool | |
| 302 | { | |
| 303 | return (false !== $this->getId()) ? true : false; | |
| 304 | } | |
| 305 | ||
| 306 | public function getId(): mixed | |
| 307 | { | |
| 308 | $idName = $this::PRIMARY; | |
| 309 | return ($this->offsetExists($idName) && $this[$idName]) ? $this[$idName] : false; | |
| 310 | } | |
| 311 | ||
| 312 | /** | |
| 313 | * Allow accessing properties without checking if it exists first | |
| 314 | * | |
| 315 | * @return \BO\Zmsentities\Helper\Property | |
| 316 | */ | |
| 317 | public function toProperty() | |
| 318 | { | |
| 319 | return new \BO\Zmsentities\Helper\Property($this); | |
| 320 | } | |
| 321 | ||
| 322 | public function hasProperty(mixed $propertyName): mixed | |
| 323 | { | |
| 324 | return $this->toProperty()->{$propertyName}->isAvailable(); | |
| 325 | } | |
| 326 | ||
| 327 | public function getProperty(string $propertyName, mixed $default = ''): mixed | |
| 328 | { | |
| 329 | return $this->toProperty()->{$propertyName}->get($default); | |
| 330 | } | |
| 331 | ||
| 332 | /** | |
| 333 | * Change property without changing original | |
| 334 | */ | |
| 335 | public function withProperty(mixed $propertyName, mixed $newValue): static | |
| 336 | { | |
| 337 | $entity = clone $this; | |
| 338 | $entity[$propertyName] = $newValue; | |
| 339 | return $entity; | |
| 340 | } | |
| 341 | ||
| 342 | /** | |
| 343 | * Reduce data of dereferenced entities to a required minimum | |
| 344 | * | |
| 345 | * @return static | |
| 346 | */ | |
| 347 | public function withLessData() | |
| 348 | { | |
| 349 | return clone $this; | |
| 350 | } | |
| 351 | ||
| 352 | /** | |
| 353 | * Reduce data of dereferenced entities to a required minimum | |
| 354 | * | |
| 355 | * @return static | |
| 356 | */ | |
| 357 | public function withCleanedUpFormData() | |
| 358 | { | |
| 359 | $entity = clone $this; | |
| 360 | if (isset($entity['save'])) { | |
| 361 | unset($entity['save']); | |
| 362 | } | |
| 363 | if (isset($entity['removeImage'])) { | |
| 364 | unset($entity['removeImage']); | |
| 365 | } | |
| 366 | return $entity; | |
| 367 | } | |
| 368 | ||
| 369 | /** | |
| 370 | * @return int|null | |
| 371 | */ | |
| 372 | public function getResolveLevel() | |
| 373 | { | |
| 374 | return $this->resolveLevel; | |
| 375 | } | |
| 376 | ||
| 377 | /** | |
| 378 | * @param int|null $resolveLevel | |
| 379 | * @return self | |
| 380 | */ | |
| 381 | public function setResolveLevel($resolveLevel) | |
| 382 | { | |
| 383 | $this->resolveLevel = $resolveLevel; | |
| 384 | return $this; | |
| 385 | } | |
| 386 | ||
| 387 | /** | |
| 388 | * Set a very strict resolveLevel to reduce data | |
| 389 | * | |
| 390 | * @param Int $resolveLevel | |
| 391 | * @return self|array | |
| 392 | */ | |
| 393 | public function withResolveLevel($resolveLevel): self|array | |
| 394 | { | |
| 395 | if ($resolveLevel >= 0) { | |
| 396 | $entity = clone $this; | |
| 397 | foreach ($entity as $key => $value) { | |
| 398 | if ($value instanceof Entity || $value instanceof \BO\Zmsentities\Collection\Base) { | |
| 399 | $entity[$key] = $value->withResolveLevel($resolveLevel - 1); | |
| 400 | } else { | |
| 401 | $entity[$key] = $value; | |
| 402 | } | |
| 403 | } | |
| 404 | $entity->setResolveLevel($resolveLevel); | |
| 405 | return $entity; | |
| 406 | } else { | |
| 407 | return $this->withReference(); | |
| 408 | } | |
| 409 | } | |
| 410 | ||
| 411 | /** | |
| 412 | * Replace data with a jsonSchema Reference | |
| 413 | * | |
| 414 | * @param Array $additionalData | |
| 415 | * @return self|array | |
| 416 | */ | |
| 417 | public function withReference($additionalData = []): self|array | |
| 418 | { | |
| 419 | if (isset($this[$this::PRIMARY])) { | |
| 420 | $additionalData['$ref'] = | |
| 421 | $this::$schemaRefPrefix . $this->getEntityName() . '/' . $this[$this::PRIMARY] . '/'; | |
| 422 | return $additionalData; | |
| 423 | } else { | |
| 424 | return $this->withResolveLevel(0); | |
| 425 | } | |
| 426 | } | |
| 427 | } |