diff --git a/Engine/Modules/Core/Helper/DoctrineHelper.php b/Engine/Modules/Core/Helper/DoctrineHelper.php new file mode 100644 index 000000000..cba5f1b8f --- /dev/null +++ b/Engine/Modules/Core/Helper/DoctrineHelper.php @@ -0,0 +1,96 @@ +$valueMethod(); + } + if ($removeDuplicates) { + $result = array_unique($result); + } + + return $result; + } + + /** + * @param AbstractModel[] $entities + * @param string $keyMethod + * @param string|null $valueMethod + * + * @return array + */ + public static function map(array $entities, string $keyMethod, ?string $valueMethod = null) : array + { + $result = []; + $checked = false; + foreach ($entities as $entity) { + if ( !$checked) { + if ( !method_exists($entity, $keyMethod)) { + throw new InvalidArgumentException("Key method '$keyMethod' does not exist."); + } + if ($valueMethod !== null && !method_exists($entity, $valueMethod)) { + throw new InvalidArgumentException("Value method '$valueMethod' does not exist."); + } + } + $key = $entity->$keyMethod(); + $value = $valueMethod === null ? $entity : $entity->$valueMethod(); + if ($checked || is_int($key) || is_string($key)) { + $result[$key] = $value; + $checked = true; + } else { + throw new InvalidArgumentException("Key value of '$keyMethod' is no int or string."); + } + } + + return $result; + } + + /** + * @param AbstractModel[] $entities + * @param int $maxDepth + * @param array $excludeProperties + * + * @return array + */ + public static function toArray(array $entities, int $maxDepth = 2, array $excludeProperties = []) : array + { + $result = []; + foreach ($entities as $entity) { + $result[] = $entity->toArray($maxDepth, $excludeProperties); + } + + return $result; + } + +}