mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-09-25 10:57:41 +02:00
SerializableValue: new helper class
This commit is contained in:
parent
f7e9c145eb
commit
6152b5d1de
26
library/Director/Data/InvalidDataException.php
Normal file
26
library/Director/Data/InvalidDataException.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\Data;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
class InvalidDataException extends InvalidArgumentException
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $expected
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function __construct($expected, $value)
|
||||||
|
{
|
||||||
|
parent::__construct("$expected expected, got " . static::getPhpType($value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPhpType($var)
|
||||||
|
{
|
||||||
|
if (is_object($var)) {
|
||||||
|
return get_class($var);
|
||||||
|
}
|
||||||
|
|
||||||
|
return gettype($var);
|
||||||
|
}
|
||||||
|
}
|
10
library/Director/Data/Serializable.php
Normal file
10
library/Director/Data/Serializable.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\Data;
|
||||||
|
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
|
interface Serializable extends JsonSerializable
|
||||||
|
{
|
||||||
|
public static function fromSerialization($value);
|
||||||
|
}
|
89
library/Director/Data/SerializableValue.php
Normal file
89
library/Director/Data/SerializableValue.php
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\Data;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use JsonSerializable;
|
||||||
|
use stdClass;
|
||||||
|
use function get_class;
|
||||||
|
use function gettype;
|
||||||
|
use function is_array;
|
||||||
|
use function is_object;
|
||||||
|
use function is_scalar;
|
||||||
|
|
||||||
|
class SerializableValue implements Serializable
|
||||||
|
{
|
||||||
|
protected $value = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param stdClass|array $object
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public static function fromSerialization($value)
|
||||||
|
{
|
||||||
|
$self = new static;
|
||||||
|
static::assertSerializableValue($value);
|
||||||
|
$self->value = $value;
|
||||||
|
|
||||||
|
return $self;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function wantSerializable($value)
|
||||||
|
{
|
||||||
|
if ($value instanceof SerializableValue) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return static::fromSerialization($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: Check whether json_encode() is faster
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected static function assertSerializableValue($value)
|
||||||
|
{
|
||||||
|
if ($value === null || is_scalar($value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (is_object($value)) {
|
||||||
|
if ($value instanceof JsonSerializable) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($value instanceof stdClass) {
|
||||||
|
foreach ((array) $value as $val) {
|
||||||
|
static::assertSerializableValue($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($value)) {
|
||||||
|
foreach ($value as $val) {
|
||||||
|
static::assertSerializableValue($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InvalidArgumentException('Serializable value expected, got ' . static::getPhpType($value));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function getPhpType($var)
|
||||||
|
{
|
||||||
|
if (is_object($var)) {
|
||||||
|
return get_class($var);
|
||||||
|
}
|
||||||
|
|
||||||
|
return gettype($var);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize()
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user