refactor Link-derived objects

This commit is contained in:
Mike Macgirvin 2024-07-25 20:29:07 +10:00
parent 19bcfd0de2
commit 5c8584ef00

View file

@ -2,7 +2,9 @@
namespace Code\ActivityStreams;
class Link
use Code\Lib\BaseObject;
class Link extends BaseObject
{
public $type;
public $href;
@ -15,24 +17,6 @@ class Link
public $preview;
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;
}
/**
* @return mixed
@ -52,8 +36,6 @@ class Link
return $this;
}
/**
* @return mixed
*/
@ -198,25 +180,4 @@ class Link
return $this;
}
public function toArray()
{
if ($this->string) {
return $this->string;
}
$returnValue = [];
foreach ((array) $this as $key => $value) {
if (isset($value)) {
$key = ($key === 'ldcontext') ? '@context' : $key;
if ($value instanceof ASObject || $value instanceof Link) {
$returnValue[$key] = $value->toArray();
}
else {
$returnValue[$key] = $value;
}
}
}
return $returnValue;
}
}