diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:44:51 +0000 |
commit | a1ec78bf0dc93d0e05e5f066f1949dc3baecea06 (patch) | |
tree | ee596ce1bc9840661386f96f9b8d1f919a106317 /vendor/gipfl/json/src/SerializationHelper.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-incubator-a1ec78bf0dc93d0e05e5f066f1949dc3baecea06.tar.xz icingaweb2-module-incubator-a1ec78bf0dc93d0e05e5f066f1949dc3baecea06.zip |
Adding upstream version 0.20.0.upstream/0.20.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gipfl/json/src/SerializationHelper.php')
-rw-r--r-- | vendor/gipfl/json/src/SerializationHelper.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/vendor/gipfl/json/src/SerializationHelper.php b/vendor/gipfl/json/src/SerializationHelper.php new file mode 100644 index 0000000..0714e30 --- /dev/null +++ b/vendor/gipfl/json/src/SerializationHelper.php @@ -0,0 +1,55 @@ +<?php + +namespace gipfl\Json; + +use InvalidArgumentException; +use JsonSerializable; +use stdClass; + +class SerializationHelper +{ + /** + * TODO: Check whether json_encode() is faster + * + * @param mixed $value + * @return bool + */ + public 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)); + } + + public static function getPhpType($var) + { + if (is_object($var)) { + return get_class($var); + } + + return gettype($var); + } +} |