streams/Code/Lib/BaseObject.php

47 lines
1.1 KiB
PHP
Raw Normal View History

2023-07-19 10:36:00 +00:00
<?php
namespace Code\Lib;
use Code\ActivityStreams\UnhandledElementException;
class BaseObject
{
/**
* @param $input
* @param $strict
* @throws UnhandledElementException if $strict
*/
public function __construct($input = null, $strict = false)
{
if ($input) {
if (is_array($input)) {
foreach ($input as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
} else {
if ($strict) {
throw new UnhandledElementException("Unhandled element: $key");
}
}
}
}
}
return $this;
}
public function toArray()
{
$returnValue = [];
foreach ((array)$this as $key => $value) {
if (isset($value)) {
$key = ($key === 'ldcontext') ? '@context' : $key;
$returnValue[$key] = $value;
}
}
return $returnValue;
}
}