Lines
86.11%
155 / 180
Methods
73.33%
33 / 45
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| getDefaults | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 1 | ||
| getSource | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getLabel | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getContact | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getProviderList | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| getScopeList | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 30 | ||
| getRequestList | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 5 | ||
| hasProvider | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 3.07 | ||
| getRequestRelationList | 85.71% | 6 / 7 | 0.00% | 0 / 1 | 4.05 | ||
| hasRequest | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 3.07 | ||
| isEditable | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| isCompleteAndEditable | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 5.39 | ||
| withCleanedUpFormData | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] __construct | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] exchangeArray | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] offsetSet | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] getUnflattenedArray | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] getValidator | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] isValid | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] testValid | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] createExample | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] getExample | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] readJsonSchema | 66.66% | 6 / 9 | 0.00% | 0 / 1 | 3.33 | ||
| [BO\Zmsentities\Schema\Entity] getEntityName | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] setJsonCompressLevel | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] jsonSerialize | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] __toString | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] __get | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] __isset | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] __set | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] __unset | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] __clone | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsentities\Schema\Entity] addData | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 6 | ||
| [BO\Zmsentities\Schema\Entity] withData | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] hasId | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] getId | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsentities\Schema\Entity] toProperty | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] hasProperty | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] getProperty | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] withProperty | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmsentities\Schema\Entity] withLessData | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] getResolveLevel | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] setResolveLevel | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Schema\Entity] withResolveLevel | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 5 | ||
| [BO\Zmsentities\Schema\Entity] withReference | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| 5 | class Source extends Schema\Entity | |
| 6 | { | |
| 7 | public const string PRIMARY = 'source'; | |
| 8 | ||
| 9 | public static $schema = 'source.json'; | |
| 10 | ||
| 11 | /** | |
| 12 | * @return (Collection\ProviderList|Collection\RequestList|Collection\RequestRelationList|Contact|false|string)[] | |
| 13 | * | |
| 14 | */ | |
| 15 | #[\Override] | |
| 16 | public function getDefaults() | |
| 17 | { | |
| 18 | return [ | |
| 19 | 'source' => '', | |
| 20 | 'contact' => new Contact([ | |
| 21 | 'name' => '', | |
| 22 | 'email' => '' | |
| 23 | ]), | |
| 24 | 'providers' => new Collection\ProviderList(), | |
| 25 | 'requests' => new Collection\RequestList(), | |
| 26 | 'requestrelation' => new Collection\RequestRelationList(), | |
| 27 | 'label' => '', | |
| 28 | 'editable' => false | |
| 29 | ]; | |
| 30 | } | |
| 31 | ||
| 32 | public function getSource(): mixed | |
| 33 | { | |
| 34 | return $this->toProperty()->source->get(); | |
| 35 | } | |
| 36 | ||
| 37 | public function getLabel(): mixed | |
| 38 | { | |
| 39 | return $this->toProperty()->label->get(); | |
| 40 | } | |
| 41 | ||
| 42 | public function getContact(): mixed | |
| 43 | { | |
| 44 | return $this->toProperty()->contact->get(); | |
| 45 | } | |
| 46 | ||
| 47 | public function getProviderList(): Collection\ProviderList | |
| 48 | { | |
| 49 | $providerList = new Collection\ProviderList(); | |
| 50 | foreach ($this->toProperty()->providers->get() as $provider) { | |
| 51 | if (! $provider instanceof Provider) { | |
| 52 | $provider = new Provider($provider); | |
| 53 | } | |
| 54 | $providerList->addEntity($provider); | |
| 55 | } | |
| 56 | return $providerList; | |
| 57 | } | |
| 58 | ||
| 59 | public function getScopeList(): Collection\ScopeList | |
| 60 | { | |
| 61 | $scopeList = new Collection\ScopeList(); | |
| 62 | $scopes = $this->toProperty()->scopes->get(); | |
| 63 | if ($scopes && is_iterable($scopes)) { | |
| 64 | foreach ($scopes as $scope) { | |
| 65 | if (!$scope instanceof Scope) { | |
| 66 | $scope = new Scope($scope); | |
| 67 | } | |
| 68 | $scopeList->addEntity($scope); | |
| 69 | } | |
| 70 | } | |
| 71 | return $scopeList; | |
| 72 | } | |
| 73 | ||
| 74 | public function getRequestList(): Collection\RequestList | |
| 75 | { | |
| 76 | $requestList = new Collection\RequestList(); | |
| 77 | $requests = $this->toProperty()->requests->get(); | |
| 78 | if ($requests && is_iterable($requests)) { | |
| 79 | foreach ($requests as $request) { | |
| 80 | if (! $request instanceof Request) { | |
| 81 | $request = new Request($request); | |
| 82 | } | |
| 83 | $requestList->addEntity($request); | |
| 84 | } | |
| 85 | } | |
| 86 | return $requestList; | |
| 87 | } | |
| 88 | ||
| 89 | public function hasProvider(mixed $providerIdCsv): bool | |
| 90 | { | |
| 91 | $providerIds = explode(',', $providerIdCsv); | |
| 92 | foreach ($providerIds as $providerId) { | |
| 93 | if (! in_array($providerId, $this->getProviderList()->getIds())) { | |
| 94 | return false; | |
| 95 | } | |
| 96 | } | |
| 97 | return true; | |
| 98 | } | |
| 99 | ||
| 100 | public function getRequestRelationList(): Collection\RequestRelationList | |
| 101 | { | |
| 102 | $requestRelationList = new \BO\Zmsentities\Collection\RequestRelationList(); | |
| 103 | if (isset($this['requestrelation'])) { | |
| 104 | foreach ($this['requestrelation'] as $entity) { | |
| 105 | if (! $entity instanceof RequestRelation) { | |
| 106 | $entity = new RequestRelation($entity); | |
| 107 | } | |
| 108 | $requestRelationList->addEntity($entity); | |
| 109 | } | |
| 110 | } | |
| 111 | return $requestRelationList; | |
| 112 | } | |
| 113 | ||
| 114 | public function hasRequest(mixed $requestIdCsv): bool | |
| 115 | { | |
| 116 | $requestIds = explode(',', $requestIdCsv); | |
| 117 | foreach ($requestIds as $requestId) { | |
| 118 | if (! in_array($requestId, $this->getRequestList()->getIds())) { | |
| 119 | return false; | |
| 120 | } | |
| 121 | } | |
| 122 | return true; | |
| 123 | } | |
| 124 | ||
| 125 | public function isEditable(): bool | |
| 126 | { | |
| 127 | return ($this->toProperty()->editable->get()) ? true : false; | |
| 128 | } | |
| 129 | ||
| 130 | public function isCompleteAndEditable(): bool | |
| 131 | { | |
| 132 | $source = $this->getSource(); | |
| 133 | if (empty($source) || !is_string($source)) { | |
| 134 | return false; | |
| 135 | } | |
| 136 | return ($this->isEditable() && 0 < $this->getProviderList()->count() && $this->getRequestList()->count()); | |
| 137 | } | |
| 138 | ||
| 139 | #[\Override] | |
| 140 | public function withCleanedUpFormData() | |
| 141 | { | |
| 142 | $entity = parent::withCleanedUpFormData(); | |
| 143 | $providerList = $entity->getProviderList(); | |
| 144 | $requestList = $entity->getRequestList(); | |
| 145 | $entity->providers = $providerList->withDataAsObject(); | |
| 146 | $entity->requests = $requestList->withDataAsObject(); | |
| 147 | return $entity; | |
| 148 | } | |
| 149 | } |
Inherited from BO\Zmsentities\Schema\Entity
| 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 | } |
| 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 | } |
| 82 | public function offsetSet(mixed $key, mixed $value): void | |
| 83 | { | |
| 84 | /** @psalm-suppress PossiblyNullArgument */ | |
| 85 | parent::offsetSet($key, $value); | |
| 86 | } |
| 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 | } |
| 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 | } |
| 128 | public function isValid(int $resolveLevel = 0): bool | |
| 129 | { | |
| 130 | $validator = $this->getValidator('de_DE', $resolveLevel = 0); | |
| 131 | return $validator->isValid(); | |
| 132 | } |
| 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 | } |
| 157 | public static function createExample() | |
| 158 | { | |
| 159 | $object = new static(); | |
| 160 | return $object->getExample(); | |
| 161 | } |
| 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 | } |
| 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 | } |
| 193 | public function getEntityName(): string | |
| 194 | { | |
| 195 | $entity = get_class($this); | |
| 196 | $entity = preg_replace('#.*[\\\]#', '', $entity) ?? ''; | |
| 197 | $entity = strtolower($entity); | |
| 198 | return $entity; | |
| 199 | } |
| 201 | public function setJsonCompressLevel(mixed $jsonCompressLevel): static | |
| 202 | { | |
| 203 | $this->jsonCompressLevel = $jsonCompressLevel; | |
| 204 | return $this; | |
| 205 | } |
| 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 | } |
| 225 | public function __toString() | |
| 226 | { | |
| 227 | $encoded = json_encode($this->jsonSerialize(), JSON_HEX_QUOT); | |
| 228 | return $encoded !== false ? $encoded : ''; | |
| 229 | } |
| 235 | public function __get(string $name): mixed | |
| 236 | { | |
| 237 | return $this->offsetGet($name); | |
| 238 | } |
| 240 | public function __isset(string $name): bool | |
| 241 | { | |
| 242 | return $this->offsetExists($name); | |
| 243 | } |
| 245 | public function __set(string $name, mixed $value): void | |
| 246 | { | |
| 247 | $this->offsetSet($name, $value); | |
| 248 | } |
| 250 | public function __unset(string $name): void | |
| 251 | { | |
| 252 | $this->offsetUnset($name); | |
| 253 | } |
| 255 | public function __clone() | |
| 256 | { | |
| 257 | foreach ($this as $key => $property) { | |
| 258 | if (is_object($property)) { | |
| 259 | $this[$key] = clone $property; | |
| 260 | } | |
| 261 | } | |
| 262 | } |
| 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 | } |
| 294 | public function withData(mixed $mergeData): static | |
| 295 | { | |
| 296 | $entity = clone $this; | |
| 297 | $entity->addData($mergeData); | |
| 298 | return $entity; | |
| 299 | } |
| 301 | public function hasId(): bool | |
| 302 | { | |
| 303 | return (false !== $this->getId()) ? true : false; | |
| 304 | } |
| 306 | public function getId(): mixed | |
| 307 | { | |
| 308 | $idName = $this::PRIMARY; | |
| 309 | return ($this->offsetExists($idName) && $this[$idName]) ? $this[$idName] : false; | |
| 310 | } |
| 317 | public function toProperty() | |
| 318 | { | |
| 319 | return new \BO\Zmsentities\Helper\Property($this); | |
| 320 | } |
| 322 | public function hasProperty(mixed $propertyName): mixed | |
| 323 | { | |
| 324 | return $this->toProperty()->{$propertyName}->isAvailable(); | |
| 325 | } |
| 327 | public function getProperty(string $propertyName, mixed $default = ''): mixed | |
| 328 | { | |
| 329 | return $this->toProperty()->{$propertyName}->get($default); | |
| 330 | } |
| 335 | public function withProperty(mixed $propertyName, mixed $newValue): static | |
| 336 | { | |
| 337 | $entity = clone $this; | |
| 338 | $entity[$propertyName] = $newValue; | |
| 339 | return $entity; | |
| 340 | } |
| 347 | public function withLessData() | |
| 348 | { | |
| 349 | return clone $this; | |
| 350 | } |
| 372 | public function getResolveLevel() | |
| 373 | { | |
| 374 | return $this->resolveLevel; | |
| 375 | } |
| 381 | public function setResolveLevel($resolveLevel) | |
| 382 | { | |
| 383 | $this->resolveLevel = $resolveLevel; | |
| 384 | return $this; | |
| 385 | } |
| 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 | } |
| 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 | } |