lots of improvements
This commit is contained in:
95
shared/lib/Resource/Provider/Node/NodeMutableAbstract.php
Normal file
95
shared/lib/Resource/Provider/Node/NodeMutableAbstract.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXF\Resource\Provider\Node;
|
||||
|
||||
/**
|
||||
* Abstract Node Mutable Class
|
||||
*
|
||||
* Provides common implementation for mutable resource nodes
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
abstract class NodeMutableAbstract extends NodeBaseAbstract implements NodeMutableInterface {
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function jsonDeserialize(array|string $data): static {
|
||||
if (is_string($data)) {
|
||||
$data = json_decode($data, true);
|
||||
}
|
||||
|
||||
$this->data = [];
|
||||
|
||||
if (isset($data[static::JSON_PROPERTY_COLLECTION])) {
|
||||
if (!is_string($data[static::JSON_PROPERTY_COLLECTION]) && !is_int($data[static::JSON_PROPERTY_COLLECTION])) {
|
||||
throw new \InvalidArgumentException("Collection must be a string or integer");
|
||||
}
|
||||
$this->data[static::JSON_PROPERTY_COLLECTION] = $data[static::JSON_PROPERTY_COLLECTION];
|
||||
} else {
|
||||
$this->data[static::JSON_PROPERTY_COLLECTION] = null;
|
||||
}
|
||||
|
||||
if (isset($data[static::JSON_PROPERTY_IDENTIFIER])) {
|
||||
if (!is_string($data[static::JSON_PROPERTY_IDENTIFIER]) && !is_int($data[static::JSON_PROPERTY_IDENTIFIER])) {
|
||||
throw new \InvalidArgumentException("Identifier must be a string or integer");
|
||||
}
|
||||
$this->data[static::JSON_PROPERTY_IDENTIFIER] = $data[static::JSON_PROPERTY_IDENTIFIER];
|
||||
} else {
|
||||
$this->data[static::JSON_PROPERTY_IDENTIFIER] = null;
|
||||
}
|
||||
|
||||
if (isset($data[static::JSON_PROPERTY_SIGNATURE])) {
|
||||
if (!is_string($data[static::JSON_PROPERTY_SIGNATURE]) && !is_int($data[static::JSON_PROPERTY_SIGNATURE])) {
|
||||
throw new \InvalidArgumentException("Signature must be a string or integer");
|
||||
}
|
||||
$this->data[static::JSON_PROPERTY_SIGNATURE] = $data[static::JSON_PROPERTY_SIGNATURE];
|
||||
} else {
|
||||
$this->data[static::JSON_PROPERTY_SIGNATURE] = null;
|
||||
}
|
||||
|
||||
if (isset($data[static::JSON_PROPERTY_CREATED])) {
|
||||
if (!is_string($data[static::JSON_PROPERTY_CREATED])) {
|
||||
throw new \InvalidArgumentException("Created date must be a string in ISO 8601 format");
|
||||
}
|
||||
$this->data[static::JSON_PROPERTY_CREATED] = $data[static::JSON_PROPERTY_CREATED];
|
||||
} else {
|
||||
$this->data[static::JSON_PROPERTY_CREATED] = null;
|
||||
}
|
||||
|
||||
if (isset($data[static::JSON_PROPERTY_MODIFIED])) {
|
||||
if (!is_string($data[static::JSON_PROPERTY_MODIFIED])) {
|
||||
throw new \InvalidArgumentException("Modified date must be a string in ISO 8601 format");
|
||||
}
|
||||
$this->data[static::JSON_PROPERTY_MODIFIED] = $data[static::JSON_PROPERTY_MODIFIED];
|
||||
} else {
|
||||
$this->data[static::JSON_PROPERTY_MODIFIED] = null;
|
||||
}
|
||||
|
||||
if (isset($data[static::JSON_PROPERTY_PROPERTIES])) {
|
||||
if (!is_array($data[static::JSON_PROPERTY_PROPERTIES])) {
|
||||
throw new \InvalidArgumentException("Properties must be an array");
|
||||
}
|
||||
$this->getProperties()->jsonDeserialize($data[static::JSON_PROPERTY_PROPERTIES]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
abstract public function getProperties(): NodePropertiesMutableInterface;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
abstract public function setProperties(NodePropertiesMutableInterface $value): static;
|
||||
}
|
||||
Reference in New Issue
Block a user