Merge branch 'dev' of codeberg.org:streams/streams into dev

This commit is contained in:
Mike Macgirvin 2024-07-30 05:51:39 +10:00
commit c36de06026
4 changed files with 50 additions and 140 deletions

View file

@ -40,7 +40,7 @@ class dba_pdo extends dba_driver
$dsn .= ';charset=utf8mb4'; $dsn .= ';charset=utf8mb4';
} }
if ($this->driver_type === 'pgsql' && !strpos($dsn,'client_encoding')) { if ($this->driver_dbtype === 'pgsql' && !strpos($dsn,'client_encoding')) {
$dsn .= ";options='--client_encoding=UTF8'"; $dsn .= ";options='--client_encoding=UTF8'";
} }

View file

@ -3,10 +3,10 @@
namespace Code\ActivityStreams; namespace Code\ActivityStreams;
class ASObject use Code\Lib\BaseObject;
class ASObject extends BaseObject
{ {
public $string;
public $ldContext;
public $id; public $id;
public $type; public $type;
public $attachment; public $attachment;
@ -70,49 +70,6 @@ class ASObject
return $this; return $this;
} }
/**
* @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;
}
/**
* @return mixed
*/
public function getLdContext()
{
return $this->ldContext;
}
/**
* @param mixed $ldContext
* @return ASObject
*/
public function setLdContext($ldContext)
{
$this->ldContext = $ldContext;
return $this;
}
/** /**
* @return mixed * @return mixed
*/ */
@ -816,27 +773,4 @@ class ASObject
return $this; 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;
}
} }

View file

@ -2,7 +2,9 @@
namespace Code\ActivityStreams; namespace Code\ActivityStreams;
class Link use Code\Lib\BaseObject;
class Link extends BaseObject
{ {
public $type; public $type;
public $href; public $href;
@ -15,24 +17,6 @@ class Link
public $preview; 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 * @return mixed
@ -52,8 +36,6 @@ class Link
return $this; return $this;
} }
/** /**
* @return mixed * @return mixed
*/ */
@ -198,25 +180,4 @@ class Link
return $this; 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;
}
} }

View file

@ -2,12 +2,14 @@
namespace Code\Lib; namespace Code\Lib;
use Code\ActivityStreams\UnhandledElementException; use Code\ActivityStreams\UnhandledElementException;
class BaseObject class BaseObject
{ {
public $string;
public $ldContext;
/** /**
* @param $input * @param $input
* @param $strict * @param $strict
@ -16,12 +18,18 @@ class BaseObject
public function __construct($input = null, $strict = false) public function __construct($input = null, $strict = false)
{ {
if (isset($input) && is_array($input)) { if (isset($input)) {
foreach ($input as $key => $value) { if (is_string($input)) {
if ($strict && !property_exists($this, $key)) { $this->string = $input;
throw new UnhandledElementException("Unhandled element: $key"); }
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;
} }
$this->{$key} = $value;
} }
} }
return $this; return $this;
@ -29,37 +37,44 @@ class BaseObject
public function getDataType($element, $object = null) public function getDataType($element, $object = null)
{ {
if (is_null($object)) { $object = $object ?? $this;
$object = $this; $type = gettype($object[$element]);
if ($type === 'array' && array_is_list($object[$element])) {
return 'list';
} }
return $type;
if (!isset($object[$element])) {
return 'null';
}
if (is_string($object[$element])) {
return 'string';
}
if (is_array($object[$element])) {
if (array_is_list($object[$element])
|| empty($object[$element])) {
return 'array';
} else {
return 'object';
}
}
return 'other';
} }
public function toArray() public function toArray()
{ {
if ($this->string) {
return $this->string;
}
$returnValue = []; $returnValue = [];
foreach ((array)$this as $key => $value) { foreach ((array) $this as $key => $value) {
if (isset($value)) { if (isset($value)) {
$key = ($key === 'ldcontext') ? '@context' : $key; $key = ($key === 'ldContext') ? '@context' : $key;
$returnValue[$key] = $value; $returnValue[$key] = (($value instanceof BaseObject) ? $value->toArray() : $value);
} }
} }
return $returnValue; return $returnValue;
} }
/**
* @return mixed
*/
public function getLdContext()
{
return $this->ldContext;
}
/**
* @param mixed $ldContext
* @return BaseObject
*/
public function setLdContext($ldContext)
{
$this->ldContext = $ldContext;
return $this;
}
} }