Provide flag to throw exception on unhandled element

This commit is contained in:
Mike Macgirvin 2023-07-17 21:42:59 +10:00
parent 56c25b1c8d
commit 3f84b217f8
2 changed files with 29 additions and 8 deletions

View file

@ -47,15 +47,28 @@ class ASObject
public $canSearch;
public $commentPolicy;
public function __construct($array = null)
/**
* @param $input
* @param $strict
* @throws UnhandledElementException if $strict
*/
public function __construct($input = null, $strict = false)
{
if ($array && is_array($array)) {
foreach ($array as $key => $value) {
if ($key === '@context') {
$key = 'ldcontext';
}
if (property_exists($this,$key)) {
$this->{$key} = $value;
if ($input) {
if (is_array($input)) {
foreach ($input as $key => $value) {
if ($key === '@context') {
$key = 'ldcontext';
}
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
else {
if ($strict) {
throw new UnhandledElementException("Unhandled element: $key");
}
}
}
}
}

View file

@ -0,0 +1,8 @@
<?php
namespace Code\ActivityStreams;
class UnhandledElementException extends \Exception
{
}