Skip to content

Commit

Permalink
New function Arr::getSchema() (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis authored Aug 17, 2023
1 parent d23d1f3 commit 501a273
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


<!--ts-->
* [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)
<!--te-->


Collection of PHP functions, mini classes and snippets for everyday developer's routine life.

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down
20 changes: 20 additions & 0 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
66 changes: 66 additions & 0 deletions tests/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]));
}
}

0 comments on commit 501a273

Please sign in to comment.