diff options
author | Mario <mario@mariovavti.com> | 2024-10-06 08:59:36 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2024-10-06 08:59:36 +0000 |
commit | 19eeadfe5bafb8bdec3ac102d9b9f49593cc0933 (patch) | |
tree | 4cedc8629789ce45c733a3a3c7df4ae4d601169e /Zotlabs/Lib/BaseObject.php | |
parent | 2693e9e99097c4bbcf8c9103a5876bc0585ff84c (diff) | |
download | volse-hubzilla-19eeadfe5bafb8bdec3ac102d9b9f49593cc0933.tar.gz volse-hubzilla-19eeadfe5bafb8bdec3ac102d9b9f49593cc0933.tar.bz2 volse-hubzilla-19eeadfe5bafb8bdec3ac102d9b9f49593cc0933.zip |
port some libs from streams repo
Diffstat (limited to 'Zotlabs/Lib/BaseObject.php')
-rw-r--r-- | Zotlabs/Lib/BaseObject.php | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/Zotlabs/Lib/BaseObject.php b/Zotlabs/Lib/BaseObject.php new file mode 100644 index 000000000..7125d34cb --- /dev/null +++ b/Zotlabs/Lib/BaseObject.php @@ -0,0 +1,80 @@ +<?php + +namespace Zotlabs\Lib; + +use Zotlabs\ActivityStreams\UnhandledElementException; + +class BaseObject +{ + + public $string; + public $ldContext; + + /** + * @param $input + * @param $strict + * @throws UnhandledElementException if $strict + */ + + public function __construct($input = null, $strict = false) + { + if (isset($input)) { + if (is_string($input)) { + $this->string = $input; + } + elseif(is_array($input)) { + foreach ($input as $key => $value) { + $key = ($key === '@context') ? 'ldContext' : $key; + if ($strict && !property_exists($this, $key)) { + throw new UnhandledElementException("Unhandled element: $key"); + } + $this->{$key} = $value; + } + } + } + return $this; + } + + public function getDataType($element, $object = null) + { + $object = $object ?? $this; + $type = gettype($object[$element]); + if ($type === 'array' && array_is_list($object[$element])) { + return 'list'; + } + return $type; + } + + public function toArray() + { + if ($this->string) { + return $this->string; + } + $returnValue = []; + foreach ((array) $this as $key => $value) { + if (isset($value)) { + $key = ($key === 'ldContext') ? '@context' : $key; + $returnValue[$key] = (($value instanceof BaseObject) ? $value->toArray() : $value); + } + } + return $returnValue; + } + + /** + * @return mixed + */ + public function getLdContext() + { + return $this->ldContext; + } + + /** + * @param mixed $ldContext + * @return BaseObject + */ + public function setLdContext($ldContext) + { + $this->ldContext = $ldContext; + return $this; + } +} |