port some functions from streams

This commit is contained in:
Mario
2023-02-23 09:51:37 +00:00
parent 656400b418
commit efcda1d37d
2 changed files with 49 additions and 18 deletions

View File

@@ -145,36 +145,59 @@ class ActivityStreams {
$this->saved_recips = $arr;
}
/**
* @brief get single property from Activity object
*
* @param string $property
* @param mixed $default return value if property or object not set
* or object is a string id which could not be fetched.
* @return mixed
*/
public function objprop(string $property, mixed $default = false): mixed {
$x = $this->get_property_obj($property,$this->obj);
return (isset($x)) ? $x : $default;
}
/**
* @brief Collects all recipients.
*
* @param string $base
* @param mixed $base
* @param string $namespace (optional) default empty
* @return array
*/
function collect_recips($base = '', $namespace = '') {
$x = [];
public function collect_recips(mixed $base = '', string $namespace = ''): array {
$result = [];
$tmp = [];
$fields = ['to', 'cc', 'bto', 'bcc', 'audience'];
foreach ($fields as $f) {
$y = $this->get_compound_property($f, $base, $namespace);
if ($y) {
if (!is_array($this->raw_recips)) {
$this->raw_recips = [];
}
if (!is_array($y)) {
$y = [$y];
}
$this->raw_recips[$f] = $y;
$x = array_merge($x, $y);
foreach ($fields as $field) {
// don't expand these yet
$values = $this->get_property_obj($field, $base, $namespace);
if ($values) {
$values = force_array($values);
$tmp[$field] = $values;
$result = array_values(array_unique(array_merge($result, $values)));
}
// Merge the object recipients if they exist.
$values = $this->objprop($field);
if ($values) {
$values = force_array($values);
$tmp[$field] = (($tmp[$field]) ? array_merge($tmp[$field], $values) : $values);
$result = array_values(array_unique(array_merge($result, $values)));
}
// remove duplicates
if (is_array($tmp[$field])) {
$tmp[$field] = array_values(array_unique($tmp[$field]));
}
}
// not yet ready for prime time
// $x = $this->expand($x,$base,$namespace);
return $x;
$this->raw_recips = $tmp;
// not yet ready for prime time
// $result = $this->expand($result,$base,$namespace);
return $result;
}
function expand($arr, $base = '', $namespace = '') {
$ret = [];

View File

@@ -3505,6 +3505,14 @@ function flatten_array_recursive($arr) {
return($ret);
}
// Turn $element into an array if it isn't already.
function force_array($element) {
if (empty($element)) {
return [];
}
return (is_array($element)) ? $element : [$element];
}
/**
* @brief Highlight Text.
*