From 501a27310c96030467516d3829fb4e1f1820cb82 Mon Sep 17 00:00:00 2001 From: Denis Smetannikov Date: Thu, 17 Aug 2023 03:04:38 +0300 Subject: [PATCH] New function `Arr::getSchema()` (#45) --- README.md | 41 +++++++++++++++++++++++++--- src/Arr.php | 20 ++++++++++++++ tests/ArrayTest.php | 66 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 228efee..ac3d091 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,37 @@ [![Stable Version](https://poser.pugx.org/jbzoo/utils/version)](https://packagist.org/packages/jbzoo/utils/) [![Total Downloads](https://poser.pugx.org/jbzoo/utils/downloads)](https://packagist.org/packages/jbzoo/utils/stats) [![Dependents](https://poser.pugx.org/jbzoo/utils/dependents)](https://packagist.org/packages/jbzoo/utils/dependents?order_by=downloads) [![GitHub License](https://img.shields.io/github/license/jbzoo/utils)](https://github.com/JBZoo/Utils/blob/master/LICENSE) + + * [Install](#install) + * [Usage](#usage) + * [Smart functions](#smart-functions) + * [JBZoo\Utils\Arr](#jbzooutilsarr) + * [JBZoo\Utils\Cli](#jbzooutilscli) + * [JBZoo\Utils\Csv](#jbzooutilscsv) + * [JBZoo\Utils\Dates](#jbzooutilsdates) + * [JBZoo\Utilsmail](#jbzooutilsemail) + * [JBZoo\Utilsnv](#jbzooutilsenv) + * [JBZoo\Utils\FS](#jbzooutilsfs) + * [JBZoo\Utils\Filter](#jbzooutilsfilter) + * [JBZoo\Utils\Http](#jbzooutilshttp) + * [JBZoo\Utils\IP](#jbzooutilsip) + * [JBZoo\Utils\Image](#jbzooutilsimage) + * [JBZoo\Utils\PhpDocs](#jbzooutilsphpdocs) + * [JBZoo\Utils\Ser](#jbzooutilsser) + * [JBZoo\Utils\Slug](#jbzooutilsslug) + * [JBZoo\Utils\Stats](#jbzooutilsstats) + * [JBZoo\Utils\Str](#jbzooutilsstr) + * [JBZoo\Utils\Sys](#jbzooutilssys) + * [JBZoo\Utils\Timer](#jbzooutilstimer) + * [JBZoo\Utils\Url](#jbzooutilsurl) + * [JBZoo\Utils\Vars](#jbzooutilsvars) + * [JBZoo\Utils\Xml](#jbzooutilsxml) + * [Links (ideas and some functions)](#links-ideas-and-some-functions) + * [Unit tests and check code style](#unit-tests-and-check-code-style) + * [License](#license) + * [See Also](#see-also) + + Collection of PHP functions, mini classes and snippets for everyday developer's routine life. @@ -59,6 +90,8 @@ Arr::flat(array $array, bool $preserveKeys = true): array; Arr::getField(array $arrayList, string $fieldName = 'id'): array; // Get one field from array of arrays (array of objects). +Arr::getSchema(array $array): array; // Returns type of variables as array schema. + Arr::groupByKey(array $arrayList, string $key = 'id'): array; // Group array by key and return list of grouped values. Arr::implode(string $glue, array $array): string; // Array imploding for nested array. @@ -497,12 +530,14 @@ Stats::linSpace(float $min, float $max, int $num = 50, bool $endpoint = true): a Stats::mean(??array $values): float; // Returns the mean (average) value of the given values. -Stats::median(array $data): ??float; // Calculate the median of a given population. +Stats::median(array $data): float; // Calculate the median of a given population. -Stats::percentile(array $data, int|float $percentile = 95): ??float; // Calculate the percentile of a given population. +Stats::percentile(array $data, int|float $percentile = 95): float; // Calculate the percentile of a given population. Stats::renderAverage(array $values, int $rounding = 3): string; // Render human readable string of average value and system error. +Stats::renderMedian(array $values, int $rounding = 3): string; // Render human readable string of average value and system error. + Stats::stdDev(array $values, bool $sample = false): float; // Returns the standard deviation of a given population. Stats::variance(array $values, bool $sample = false): float; // Returns the variance for a given population. @@ -649,7 +684,7 @@ Sys::isFunc(Closure|string $funcName): bool; // Checks if function exists and ca Sys::isHHVM(): bool; // Returns true when the runtime used is HHVM. -Sys::isPHP(string $version, string $current = '8.1.22'): bool; // Compares PHP versions. +Sys::isPHP(string $version, string $current = '8.2.9'): bool; // Compares PHP versions. Sys::isPHPDBG(): bool; // Returns true when the runtime used is PHP with the PHPDBG SAPI. diff --git a/src/Arr.php b/src/Arr.php index f8c33a4..3c73897 100644 --- a/src/Arr.php +++ b/src/Arr.php @@ -410,4 +410,24 @@ public static function removeByValue(array $array, float|bool|int|string|null $v \ARRAY_FILTER_USE_BOTH, ); } + + /** + * Returns type of variables as array schema. + */ + public static function getSchema(array $array): array + { + $result = []; + + foreach ($array as $key => $value) { + if (\is_array($value)) { + $result[$key] = self::getSchema($value); + } elseif (\is_object($value)) { + $result[$key] = '\\' . \get_class($value); + } else { + $result[$key] = \get_debug_type($value); + } + } + + return $result; + } } diff --git a/tests/ArrayTest.php b/tests/ArrayTest.php index 9cfa570..213c9ab 100644 --- a/tests/ArrayTest.php +++ b/tests/ArrayTest.php @@ -464,4 +464,70 @@ public function testRemoveByValue(): void isSame([1, '1', 1.5, '1.5', true], \array_values(Arr::removeByValue([0, 0, 1, '1', 1.5, '1.5', true, 0], 0))); } + + public function testGetSchema(): void + { + $test = [ + // regular types + 'string-zero' => '0', + 'string-empty' => '', + 'string' => 'qwerty', + 'number-zero' => 0, + 'number' => 10, + 'float' => 0.1, + 'float-empty' => 0.0, + 'bool-true' => true, + 'bool-false' => false, + 'null' => null, + + // objects + 'object' => (object)['prop-1' => 'prop-value-1'], + 'date' => new \DateTimeImmutable(), + 'utils' => new Arr(), + + // array + 'array_empty' => [], + 'array_not_empty' => [ + '123' => '123321', + 'key' => 'value', + 'arr' => [new \DateTimeImmutable(), []], + ], + + 'res' => \fopen(__FILE__, 'r'), + ]; + + isSame([ + 'string-zero' => 'string', + 'string-empty' => 'string', + 'string' => 'string', + + 'number-zero' => 'int', + 'number' => 'int', + + 'float' => 'float', + 'float-empty' => 'float', + + 'bool-true' => 'bool', + 'bool-false' => 'bool', + + 'null' => 'null', + + 'object' => '\stdClass', + 'date' => '\DateTimeImmutable', + 'utils' => '\JBZoo\Utils\Arr', + + 'array_empty' => [], + 'array_not_empty' => [ + 123 => 'string', + 'key' => 'string', + 'arr' => [ + 0 => '\DateTimeImmutable', + 1 => [], + ], + ], + 'res' => 'resource (stream)', + ], Arr::getSchema($test)); + + isSame([], Arr::getSchema([])); + } }