diff --git a/sources/traits/ObjectArray/ObjectArrayLINQTrait.php b/sources/traits/ObjectArray/ObjectArrayLINQTrait.php index 1fd0867..450303b 100644 --- a/sources/traits/ObjectArray/ObjectArrayLINQTrait.php +++ b/sources/traits/ObjectArray/ObjectArrayLINQTrait.php @@ -143,6 +143,22 @@ trait ObjectArrayLINQTrait * @return array Ассоциированный массив с результатом выборки. */ public function GetColumn (string $column, ?callable $wherePredicate = null): array + { + return $this->GetColumnCallback(fn ($item) => property_exists($item, $column) ? $item->$column : null, + $wherePredicate); + } + + /** + * Получает колонку в массиве данных. + * + * @param callable $columnPredicate Функция fn (mixed $item): mixed, возвращающая значение элемента + * колонки. + * @param callable|null $wherePredicate Условие выборки fn (mixed $item): bool, которое проверяет, + * подходит элемент или нет. + * + * @return array Ассоциированный массив с результатом выборки. + */ + public function GetColumnCallback (callable $columnPredicate, ?callable $wherePredicate = null): array { // Создаю результат $result = []; @@ -153,16 +169,12 @@ trait ObjectArrayLINQTrait if (!is_object($item)) continue; - // - пропускаю не имеющие требуемого свойства - if (!property_exists($item, $column)) - continue; - // - пропускаю не удовлетворяющие условию if ($wherePredicate !== null && !$wherePredicate($item)) continue; // - добавляю значение свойства в результат - $result[] = $item->$column; + $result[] = $columnPredicate($item); } // Возвращаю результат