Added FlexCollection::getDistinctValues() to get all the assigned values from the field

This commit is contained in:
Matias Griese
2020-12-23 21:05:58 +02:00
parent 5bb4ca7822
commit 4345a629f1
2 changed files with 29 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
* Added support for overriding configuration by using environment variables
* Use PHP 7.4 serialization (the old `Serializable` methods are now final and cannot be overridden)
* Enabled `ETag` setting by default for 304 responses
* Added `FlexCollection::getDistinctValues()` to get all the assigned values from the field
1. [](#improved)
* Make it possible to use an absolute path when loading a blueprint
* Make serialize methods final in `ContentBlock`, `AbstractFile`, `FormTrait`, `ObjectCollectionTrait` and `ObjectTrait`

View File

@@ -71,6 +71,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
'isAuthorized' => 'session',
'search' => true,
'sort' => true,
'getDistinctValues' => true
];
}
@@ -272,6 +273,33 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
return $keys;
}
/**
* Get all the values in property.
*
* Supports either single scalar values or array of scalar values.
*
* @param string $property Object property to be used to make groups.
* @param string|null $separator Separator, defaults to '.'
* @return array
*/
public function getDistinctValues(string $property, string $separator = null): array
{
$list = [];
/** @var FlexObjectInterface $element */
foreach ($this->getIterator() as $element) {
$value = (array)$element->getNestedProperty($property, null, $separator);
foreach ($value as $v) {
if (is_scalar($v)) {
$t = gettype($v) . (string)$v;
$list[$t] = $v;
}
}
}
return array_values($list);
}
/**
* {@inheritdoc}
* @see FlexCollectionInterface::withKeyField()