From e9e20a296b8f598218f290215942edaa294712f1 Mon Sep 17 00:00:00 2001 From: SmetDenis Date: Sun, 31 Mar 2024 03:16:10 +0400 Subject: [PATCH] Add 'ds' PHP extension and transition array to vector The PHP 'ds' (data structures) extension has been added to the GitHub Actions workflows to permit targeting PHP 8.3. Array references in all aggregate rule classes have also been migrated to Ds\Vector. With this change, memory optimization and performance are expected to be enhanced due to the characteristic efficiency of vectors. --- .github/workflows/benchmark.yml | 1 + .github/workflows/main.yml | 8 +++++--- .github/workflows/publish.yml | 1 + Dockerfile | 2 +- composer.json | 3 ++- src/Rules/AbstarctRule.php | 5 +++-- src/Rules/AbstarctRuleCombo.php | 9 +++++---- src/Rules/Aggregate/AbstractAggregateRule.php | 5 ++--- .../Aggregate/AbstractAggregateRuleCombo.php | 19 ++++++++++--------- src/Rules/Aggregate/ComboAverage.php | 5 +++-- src/Rules/Aggregate/ComboCoefOfVar.php | 3 ++- .../Aggregate/ComboContraharmonicMean.php | 3 ++- src/Rules/Aggregate/ComboCount.php | 3 ++- src/Rules/Aggregate/ComboCountDistinct.php | 3 ++- src/Rules/Aggregate/ComboCountEmpty.php | 3 ++- src/Rules/Aggregate/ComboCountEven.php | 3 ++- src/Rules/Aggregate/ComboCountNegative.php | 3 ++- src/Rules/Aggregate/ComboCountNotEmpty.php | 3 ++- src/Rules/Aggregate/ComboCountOdd.php | 3 ++- src/Rules/Aggregate/ComboCountPositive.php | 3 ++- src/Rules/Aggregate/ComboCountPrime.php | 3 ++- src/Rules/Aggregate/ComboCountZero.php | 3 ++- src/Rules/Aggregate/ComboCubicMean.php | 3 ++- src/Rules/Aggregate/ComboFirstNum.php | 3 ++- src/Rules/Aggregate/ComboGeometricMean.php | 3 ++- src/Rules/Aggregate/ComboHarmonicMean.php | 3 ++- .../Aggregate/ComboInterquartileMean.php | 3 ++- src/Rules/Aggregate/ComboLastNum.php | 3 ++- src/Rules/Aggregate/ComboMeanAbsDev.php | 3 ++- src/Rules/Aggregate/ComboMedian.php | 3 ++- src/Rules/Aggregate/ComboMedianAbsDev.php | 3 ++- src/Rules/Aggregate/ComboMidhinge.php | 3 ++- src/Rules/Aggregate/ComboNthNum.php | 3 ++- src/Rules/Aggregate/ComboPercentile.php | 3 ++- .../Aggregate/ComboPopulationVariance.php | 3 ++- src/Rules/Aggregate/ComboQuartiles.php | 3 ++- src/Rules/Aggregate/ComboRootMeanSquare.php | 3 ++- src/Rules/Aggregate/ComboSampleVariance.php | 3 ++- src/Rules/Aggregate/ComboStddev.php | 3 ++- src/Rules/Aggregate/ComboStddevPop.php | 3 ++- src/Rules/Aggregate/ComboSum.php | 5 +++-- src/Rules/Aggregate/ComboTrimean.php | 3 ++- src/Rules/Aggregate/First.php | 3 ++- src/Rules/Aggregate/FirstNot.php | 3 ++- src/Rules/Aggregate/IsUnique.php | 5 +++-- src/Rules/Aggregate/Last.php | 3 ++- src/Rules/Aggregate/LastNot.php | 3 ++- src/Rules/Aggregate/Nth.php | 3 ++- src/Rules/Aggregate/NthNot.php | 3 ++- src/Rules/Aggregate/Sorted.php | 3 ++- src/Rules/Ruleset.php | 3 ++- src/Validators/ValidatorColumn.php | 3 ++- src/Validators/ValidatorCsv.php | 2 +- tests/Commands/ValidateCsvBasicTest.php | 2 +- 54 files changed, 121 insertions(+), 71 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index d941767e..3e33cac1 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -80,6 +80,7 @@ jobs: with: php-version: 8.3 coverage: none + extensions: ds ini-values: opcache.enable_cli=1, opcache.jit=1255 - name: Build local Phar file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4dd7a50f..cd88349b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -56,7 +56,7 @@ jobs: with: php-version: 8.3 coverage: xdebug - extensions: ast + extensions: ast, ds - name: Build project run: make build --no-print-directory @@ -100,7 +100,7 @@ jobs: with: php-version: 8.1 coverage: none - extensions: ast + extensions: ast, ds - name: Install project run: make build --no-print-directory @@ -142,7 +142,7 @@ jobs: with: php-version: highest coverage: none - extensions: ast + extensions: ast, ds - name: Install project run: make build --no-print-directory @@ -182,6 +182,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: 8.3 + extensions: ds - name: Build project in production mode run: make build-prod --no-print-directory @@ -214,6 +215,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-version }} + extensions: ds - name: Build project in production mode run: make build-prod build-phar-file --no-print-directory diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 8e2f0ba9..a61c2649 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -32,6 +32,7 @@ jobs: with: php-version: 8.3 tools: composer + extensions: ds - name: Build project in production mode run: make build-prod build-phar-file --no-print-directory diff --git a/Dockerfile b/Dockerfile index e5cd65d4..f29a585b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,7 @@ FROM php:8.3-cli-alpine # Install PHP extensions ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ -RUN install-php-extensions opcache @composer +RUN install-php-extensions opcache ds @composer # Install application # run `make build-version` before! diff --git a/composer.json b/composer.json index 39305c66..94f5f466 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name" : "jbzoo/csv-blueprint", - "type" : "project", + "type" : "project", "description" : "CLI Utility for Validating and Generating CSV Files Based on Custom Rules. It ensures your data meets specified criteria, streamlining data management and integrity checks.", "license" : "MIT", "keywords" : [ @@ -29,6 +29,7 @@ "require" : { "php" : "^8.1", "ext-mbstring" : "*", + "ext-ds" : "*", "league/csv" : "^9.15.0", "jbzoo/data" : "^7.1.1", diff --git a/src/Rules/AbstarctRule.php b/src/Rules/AbstarctRule.php index ae373cc8..1c60a3d2 100644 --- a/src/Rules/AbstarctRule.php +++ b/src/Rules/AbstarctRule.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules; +use Ds\Vector; use JBZoo\CsvBlueprint\Utils; use JBZoo\CsvBlueprint\Validators\Error; use JBZoo\CsvBlueprint\Validators\ValidatorColumn; @@ -61,7 +62,7 @@ public function __construct( // TODO: Move resolving and validating expected value on this stage to make it only once (before validation). } - public function validate(array|string $cellValue, int $line = ValidatorColumn::FALLBACK_LINE): ?Error + public function validate(array|string|Vector $cellValue, int $line = ValidatorColumn::FALLBACK_LINE): ?Error { // TODO: Extract to abstract boolean cell/agregate rule if ($this->isEnabled($cellValue) === false) { @@ -185,7 +186,7 @@ protected function getOptionAsArray(): array * Optimize performance * There is no need to validate empty values for predicates or if it's disabled. */ - protected function isEnabled(array|string $cellValue): bool + protected function isEnabled(string|Vector $cellValue): bool { // List of rules that should be checked for empty values $exclusions = [ diff --git a/src/Rules/AbstarctRuleCombo.php b/src/Rules/AbstarctRuleCombo.php index 80689781..1831126a 100644 --- a/src/Rules/AbstarctRuleCombo.php +++ b/src/Rules/AbstarctRuleCombo.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\Aggregate\AbstractAggregateRuleCombo; use JBZoo\CsvBlueprint\Rules\Cell\AbstractCellRuleCombo; use JBZoo\CsvBlueprint\Validators\Error; @@ -38,7 +39,7 @@ abstract protected function getExpected(): float; abstract protected function getActual(array|string $value): float; - public function validate(array|string $cellValue, int $line = ValidatorColumn::FALLBACK_LINE): ?Error + public function validate(string|Vector $cellValue, int $line = ValidatorColumn::FALLBACK_LINE): ?Error { $error = $this->validateCombo($cellValue); @@ -51,7 +52,7 @@ public function validate(array|string $cellValue, int $line = ValidatorColumn::F public function test(array|string $cellValue, bool $isHtml = false): string { - $errorMessage = (string)$this->validateCombo($cellValue); + $errorMessage = (string)$this->validateCombo(new Vector($cellValue)); return $isHtml ? $errorMessage : \strip_tags($errorMessage); } @@ -96,7 +97,7 @@ protected static function compare(float $expected, float $actual, string $mode): }; } - private function validateCombo(array|string $cellValue): ?string + private function validateCombo(string|Vector $cellValue): ?string { if ($this instanceof AbstractCellRuleCombo) { if (!\is_string($cellValue)) { @@ -107,7 +108,7 @@ private function validateCombo(array|string $cellValue): ?string } if ($this instanceof AbstractAggregateRuleCombo) { - if (!\is_array($cellValue)) { + if (!$cellValue instanceof Vector) { throw new \InvalidArgumentException('The value should be an array of numbers/strings'); } diff --git a/src/Rules/Aggregate/AbstractAggregateRule.php b/src/Rules/Aggregate/AbstractAggregateRule.php index 5c8a4165..d6915b10 100644 --- a/src/Rules/Aggregate/AbstractAggregateRule.php +++ b/src/Rules/Aggregate/AbstractAggregateRule.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; abstract class AbstractAggregateRule extends AbstarctRule @@ -28,10 +29,8 @@ abstract class AbstractAggregateRule extends AbstarctRule * TODO: This method takes an array reference &$columnValues as input and returns a nullable string. * We use a reference to the array to avoid copying the array. Important memory optimization! * Please DO NOT change the array in this method! - * - * @param string[] $columnValues */ - abstract public function validateRule(array $columnValues): ?string; + abstract public function validateRule(Vector $columnValues): ?string; public function test(array $cellValue, bool $isHtml = false): string { diff --git a/src/Rules/Aggregate/AbstractAggregateRuleCombo.php b/src/Rules/Aggregate/AbstractAggregateRuleCombo.php index f678cba4..f7c19348 100644 --- a/src/Rules/Aggregate/AbstractAggregateRuleCombo.php +++ b/src/Rules/Aggregate/AbstractAggregateRuleCombo.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use JBZoo\CsvBlueprint\Rules\AbstarctRuleCombo; @@ -23,7 +24,7 @@ abstract class AbstractAggregateRuleCombo extends AbstarctRuleCombo { public const INPUT_TYPE = AbstarctRule::INPUT_TYPE_STRINGS; - abstract protected function getActualAggregate(array $colValues): ?float; + abstract protected function getActualAggregate(Vector $colValues): ?float; public function getRuleCode(?string $mode = null): string { @@ -46,7 +47,7 @@ protected function getExpected(): float return $this->getOptionAsFloat(); } - protected function validateComboAggregate(array $colValues, string $mode): ?string + protected function validateComboAggregate(Vector $colValues, string $mode): ?string { $prefix = $mode === self::NOT ? 'not ' : ''; $verb = static::VERBS[$mode]; @@ -54,13 +55,13 @@ protected function validateComboAggregate(array $colValues, string $mode): ?stri try { // TODO: Think about the performance optimization here - if (static::INPUT_TYPE === AbstarctRule::INPUT_TYPE_FLOATS) { - $colValues = \array_map('floatval', $colValues); - } - - if (static::INPUT_TYPE === AbstarctRule::INPUT_TYPE_INTS) { - $colValues = \array_map('intval', $colValues); - } + // if (static::INPUT_TYPE === AbstarctRule::INPUT_TYPE_FLOATS) { + // $colValues = \array_map('floatval', $colValues); + // } + // + // if (static::INPUT_TYPE === AbstarctRule::INPUT_TYPE_INTS) { + // $colValues = \array_map('intval', $colValues); + // } $actual = $this->getActualAggregate($colValues); // Important to use the original method here! } catch (\Throwable $exception) { diff --git a/src/Rules/Aggregate/ComboAverage.php b/src/Rules/Aggregate/ComboAverage.php index 520385a1..b6f089ae 100644 --- a/src/Rules/Aggregate/ComboAverage.php +++ b/src/Rules/Aggregate/ComboAverage.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -30,12 +31,12 @@ public function getHelpMeta(): array return [['Regular the arithmetic mean. The sum of the numbers divided by the count.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; } - return Average::mean($colValues); + return Average::mean($colValues->toArray()); } } diff --git a/src/Rules/Aggregate/ComboCoefOfVar.php b/src/Rules/Aggregate/ComboCoefOfVar.php index 5b593030..c52e0977 100644 --- a/src/Rules/Aggregate/ComboCoefOfVar.php +++ b/src/Rules/Aggregate/ComboCoefOfVar.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -38,7 +39,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboContraharmonicMean.php b/src/Rules/Aggregate/ComboContraharmonicMean.php index dc63a32e..ecebecd7 100644 --- a/src/Rules/Aggregate/ComboContraharmonicMean.php +++ b/src/Rules/Aggregate/ComboContraharmonicMean.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -37,7 +38,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCount.php b/src/Rules/Aggregate/ComboCount.php index 7a8735ec..5370d68b 100644 --- a/src/Rules/Aggregate/ComboCount.php +++ b/src/Rules/Aggregate/ComboCount.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCount extends AbstractAggregateRuleCombo @@ -36,7 +37,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { return \count($colValues); } diff --git a/src/Rules/Aggregate/ComboCountDistinct.php b/src/Rules/Aggregate/ComboCountDistinct.php index 2af8d93f..2f64846a 100644 --- a/src/Rules/Aggregate/ComboCountDistinct.php +++ b/src/Rules/Aggregate/ComboCountDistinct.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCountDistinct extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['Number of unique values.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCountEmpty.php b/src/Rules/Aggregate/ComboCountEmpty.php index 32425180..b761a3d9 100644 --- a/src/Rules/Aggregate/ComboCountEmpty.php +++ b/src/Rules/Aggregate/ComboCountEmpty.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCountEmpty extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['Counts only empty values (string length is 0).'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCountEven.php b/src/Rules/Aggregate/ComboCountEven.php index c7c8d59e..76b6059c 100644 --- a/src/Rules/Aggregate/ComboCountEven.php +++ b/src/Rules/Aggregate/ComboCountEven.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCountEven extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['Number of even values.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCountNegative.php b/src/Rules/Aggregate/ComboCountNegative.php index 74e9336f..c2c0c27d 100644 --- a/src/Rules/Aggregate/ComboCountNegative.php +++ b/src/Rules/Aggregate/ComboCountNegative.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCountNegative extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['Number of negative values.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCountNotEmpty.php b/src/Rules/Aggregate/ComboCountNotEmpty.php index 7fbf20f4..17104b68 100644 --- a/src/Rules/Aggregate/ComboCountNotEmpty.php +++ b/src/Rules/Aggregate/ComboCountNotEmpty.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCountNotEmpty extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['Counts only not empty values (string length is not 0).'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCountOdd.php b/src/Rules/Aggregate/ComboCountOdd.php index 9fad05c2..25a598a6 100644 --- a/src/Rules/Aggregate/ComboCountOdd.php +++ b/src/Rules/Aggregate/ComboCountOdd.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCountOdd extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['Number of odd values.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCountPositive.php b/src/Rules/Aggregate/ComboCountPositive.php index ad428a2e..638b96d4 100644 --- a/src/Rules/Aggregate/ComboCountPositive.php +++ b/src/Rules/Aggregate/ComboCountPositive.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCountPositive extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['Number of positive values.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCountPrime.php b/src/Rules/Aggregate/ComboCountPrime.php index 80aa303d..483a59f9 100644 --- a/src/Rules/Aggregate/ComboCountPrime.php +++ b/src/Rules/Aggregate/ComboCountPrime.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use Respect\Validation\Validator; @@ -30,7 +31,7 @@ public function getHelpMeta(): array return [['Number of prime values.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCountZero.php b/src/Rules/Aggregate/ComboCountZero.php index 71a192ba..2f05c0c7 100644 --- a/src/Rules/Aggregate/ComboCountZero.php +++ b/src/Rules/Aggregate/ComboCountZero.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboCountZero extends AbstractAggregateRuleCombo @@ -35,7 +36,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboCubicMean.php b/src/Rules/Aggregate/ComboCubicMean.php index ff12de85..4e136881 100644 --- a/src/Rules/Aggregate/ComboCubicMean.php +++ b/src/Rules/Aggregate/ComboCubicMean.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -30,7 +31,7 @@ public function getHelpMeta(): array return [['Cubic mean. See: https://en.wikipedia.org/wiki/Cubic_mean'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboFirstNum.php b/src/Rules/Aggregate/ComboFirstNum.php index 7ebe852d..e6847306 100644 --- a/src/Rules/Aggregate/ComboFirstNum.php +++ b/src/Rules/Aggregate/ComboFirstNum.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboFirstNum extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['First number in the column. Expected value is float or integer.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (!isset($colValues[0])) { return null; diff --git a/src/Rules/Aggregate/ComboGeometricMean.php b/src/Rules/Aggregate/ComboGeometricMean.php index c2e94e61..abfcd266 100644 --- a/src/Rules/Aggregate/ComboGeometricMean.php +++ b/src/Rules/Aggregate/ComboGeometricMean.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -38,7 +39,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboHarmonicMean.php b/src/Rules/Aggregate/ComboHarmonicMean.php index 5db8bd49..4c5d4ca7 100644 --- a/src/Rules/Aggregate/ComboHarmonicMean.php +++ b/src/Rules/Aggregate/ComboHarmonicMean.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -38,7 +39,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboInterquartileMean.php b/src/Rules/Aggregate/ComboInterquartileMean.php index 2fda3558..89b66fc6 100644 --- a/src/Rules/Aggregate/ComboInterquartileMean.php +++ b/src/Rules/Aggregate/ComboInterquartileMean.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -40,7 +41,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboLastNum.php b/src/Rules/Aggregate/ComboLastNum.php index f32b90b0..fd59b814 100644 --- a/src/Rules/Aggregate/ComboLastNum.php +++ b/src/Rules/Aggregate/ComboLastNum.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboLastNum extends AbstractAggregateRuleCombo @@ -29,7 +30,7 @@ public function getHelpMeta(): array return [['Last number in the column. Expected value is float or integer.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboMeanAbsDev.php b/src/Rules/Aggregate/ComboMeanAbsDev.php index 41a9533d..0cab8137 100644 --- a/src/Rules/Aggregate/ComboMeanAbsDev.php +++ b/src/Rules/Aggregate/ComboMeanAbsDev.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -37,7 +38,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboMedian.php b/src/Rules/Aggregate/ComboMedian.php index b766f990..29ba4bee 100644 --- a/src/Rules/Aggregate/ComboMedian.php +++ b/src/Rules/Aggregate/ComboMedian.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -36,7 +37,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboMedianAbsDev.php b/src/Rules/Aggregate/ComboMedianAbsDev.php index c87c7021..d06e4cad 100644 --- a/src/Rules/Aggregate/ComboMedianAbsDev.php +++ b/src/Rules/Aggregate/ComboMedianAbsDev.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -38,7 +39,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboMidhinge.php b/src/Rules/Aggregate/ComboMidhinge.php index 63411186..67386b1b 100644 --- a/src/Rules/Aggregate/ComboMidhinge.php +++ b/src/Rules/Aggregate/ComboMidhinge.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -38,7 +39,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboNthNum.php b/src/Rules/Aggregate/ComboNthNum.php index 9861037a..99820335 100644 --- a/src/Rules/Aggregate/ComboNthNum.php +++ b/src/Rules/Aggregate/ComboNthNum.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use function JBZoo\Utils\float; @@ -56,7 +57,7 @@ protected function getExpected(): float return float($this->getParams()[self::VAL]); } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboPercentile.php b/src/Rules/Aggregate/ComboPercentile.php index 3bec50e3..04682069 100644 --- a/src/Rules/Aggregate/ComboPercentile.php +++ b/src/Rules/Aggregate/ComboPercentile.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -59,7 +60,7 @@ protected function getExpected(): float return float($this->getParams()[self::VAL]); } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboPopulationVariance.php b/src/Rules/Aggregate/ComboPopulationVariance.php index f33b195e..98a506f5 100644 --- a/src/Rules/Aggregate/ComboPopulationVariance.php +++ b/src/Rules/Aggregate/ComboPopulationVariance.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -37,7 +38,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboQuartiles.php b/src/Rules/Aggregate/ComboQuartiles.php index ed52ad8c..9de16cba 100644 --- a/src/Rules/Aggregate/ComboQuartiles.php +++ b/src/Rules/Aggregate/ComboQuartiles.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use JBZoo\CsvBlueprint\Utils; use MathPHP\Statistics\Descriptive; @@ -67,7 +68,7 @@ protected function getExpected(): float return float($this->getParams()[self::VAL]); } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboRootMeanSquare.php b/src/Rules/Aggregate/ComboRootMeanSquare.php index da682cb6..a6ff26d1 100644 --- a/src/Rules/Aggregate/ComboRootMeanSquare.php +++ b/src/Rules/Aggregate/ComboRootMeanSquare.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -37,7 +38,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboSampleVariance.php b/src/Rules/Aggregate/ComboSampleVariance.php index bd785d07..ffbae5b0 100644 --- a/src/Rules/Aggregate/ComboSampleVariance.php +++ b/src/Rules/Aggregate/ComboSampleVariance.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -37,7 +38,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboStddev.php b/src/Rules/Aggregate/ComboStddev.php index 08b1ea17..e77ba32d 100644 --- a/src/Rules/Aggregate/ComboStddev.php +++ b/src/Rules/Aggregate/ComboStddev.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -42,7 +43,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboStddevPop.php b/src/Rules/Aggregate/ComboStddevPop.php index cdf9f67b..387b943e 100644 --- a/src/Rules/Aggregate/ComboStddevPop.php +++ b/src/Rules/Aggregate/ComboStddevPop.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Descriptive; @@ -35,7 +36,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/ComboSum.php b/src/Rules/Aggregate/ComboSum.php index 6b85d513..44d44168 100644 --- a/src/Rules/Aggregate/ComboSum.php +++ b/src/Rules/Aggregate/ComboSum.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class ComboSum extends AbstractAggregateRuleCombo @@ -29,8 +30,8 @@ public function getHelpMeta(): array return [['Sum of the numbers in the column. Example: [1, 2, 3] => 6.'], []]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { - return \array_sum($colValues); + return $colValues->sum(); } } diff --git a/src/Rules/Aggregate/ComboTrimean.php b/src/Rules/Aggregate/ComboTrimean.php index d471b31f..6294ef2c 100644 --- a/src/Rules/Aggregate/ComboTrimean.php +++ b/src/Rules/Aggregate/ComboTrimean.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use MathPHP\Statistics\Average; @@ -38,7 +39,7 @@ public function getHelpMeta(): array ]; } - protected function getActualAggregate(array $colValues): ?float + protected function getActualAggregate(Vector $colValues): ?float { if (\count($colValues) === 0) { return null; diff --git a/src/Rules/Aggregate/First.php b/src/Rules/Aggregate/First.php index 37c2c968..fbbfd633 100644 --- a/src/Rules/Aggregate/First.php +++ b/src/Rules/Aggregate/First.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class First extends AbstractAggregateRule @@ -30,7 +31,7 @@ public function getHelpMeta(): array ]; } - public function validateRule(array $columnValues): ?string + public function validateRule(Vector $columnValues): ?string { if (\count($columnValues) === 0) { return null; diff --git a/src/Rules/Aggregate/FirstNot.php b/src/Rules/Aggregate/FirstNot.php index 5eecc1bf..e59c142e 100644 --- a/src/Rules/Aggregate/FirstNot.php +++ b/src/Rules/Aggregate/FirstNot.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class FirstNot extends AbstractAggregateRule @@ -35,7 +36,7 @@ public function getHelpMeta(): array ]; } - public function validateRule(array $columnValues): ?string + public function validateRule(Vector $columnValues): ?string { if (\count($columnValues) === 0) { return null; diff --git a/src/Rules/Aggregate/IsUnique.php b/src/Rules/Aggregate/IsUnique.php index 86434e62..90dc1142 100644 --- a/src/Rules/Aggregate/IsUnique.php +++ b/src/Rules/Aggregate/IsUnique.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class IsUnique extends AbstractAggregateRule @@ -30,13 +31,13 @@ public function getHelpMeta(): array ]; } - public function validateRule(array $columnValues): ?string + public function validateRule(Vector $columnValues): ?string { if (\count($columnValues) === 0) { return null; } - $uValuesCount = \count(\array_unique($columnValues)); + $uValuesCount = \count(\array_unique($columnValues->toArray())); $valuesCount = \count($columnValues); if ($uValuesCount !== $valuesCount) { diff --git a/src/Rules/Aggregate/Last.php b/src/Rules/Aggregate/Last.php index 153911c7..b70521ff 100644 --- a/src/Rules/Aggregate/Last.php +++ b/src/Rules/Aggregate/Last.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class Last extends AbstractAggregateRule @@ -30,7 +31,7 @@ public function getHelpMeta(): array ]; } - public function validateRule(array $columnValues): ?string + public function validateRule(Vector $columnValues): ?string { if (\count($columnValues) === 0) { return null; diff --git a/src/Rules/Aggregate/LastNot.php b/src/Rules/Aggregate/LastNot.php index 28358415..81a3b153 100644 --- a/src/Rules/Aggregate/LastNot.php +++ b/src/Rules/Aggregate/LastNot.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class LastNot extends AbstractAggregateRule @@ -35,7 +36,7 @@ public function getHelpMeta(): array ]; } - public function validateRule(array $columnValues): ?string + public function validateRule(Vector $columnValues): ?string { if (\count($columnValues) === 0) { return null; diff --git a/src/Rules/Aggregate/Nth.php b/src/Rules/Aggregate/Nth.php index 9b6c150c..9552ec7e 100644 --- a/src/Rules/Aggregate/Nth.php +++ b/src/Rules/Aggregate/Nth.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class Nth extends AbstractAggregateRule @@ -34,7 +35,7 @@ public function getHelpMeta(): array ]; } - public function validateRule(array $columnValues): ?string + public function validateRule(Vector $columnValues): ?string { if (\count($columnValues) === 0) { return null; diff --git a/src/Rules/Aggregate/NthNot.php b/src/Rules/Aggregate/NthNot.php index 8870d7b8..3eb51cdd 100644 --- a/src/Rules/Aggregate/NthNot.php +++ b/src/Rules/Aggregate/NthNot.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; final class NthNot extends AbstractAggregateRule @@ -39,7 +40,7 @@ public function getHelpMeta(): array ]; } - public function validateRule(array $columnValues): ?string + public function validateRule(Vector $columnValues): ?string { if (\count($columnValues) === 0) { return null; diff --git a/src/Rules/Aggregate/Sorted.php b/src/Rules/Aggregate/Sorted.php index 66104cbb..369f8953 100644 --- a/src/Rules/Aggregate/Sorted.php +++ b/src/Rules/Aggregate/Sorted.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules\Aggregate; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use JBZoo\CsvBlueprint\Utils; @@ -50,7 +51,7 @@ public function getHelpMeta(): array ]; } - public function validateRule(array $columnValues): ?string + public function validateRule(Vector $columnValues): ?string { if (\count($columnValues) === 0) { return null; diff --git a/src/Rules/Ruleset.php b/src/Rules/Ruleset.php index b04ba9d0..0142aa00 100644 --- a/src/Rules/Ruleset.php +++ b/src/Rules/Ruleset.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules; +use Ds\Vector; use JBZoo\CsvBlueprint\Rules\Cell\AbstractCellRuleCombo; use JBZoo\CsvBlueprint\Utils; use JBZoo\CsvBlueprint\Validators\ErrorSuite; @@ -43,7 +44,7 @@ public function __construct(array $rules, string $columnNameId) } } - public function validateRuleSet(array|string $cellValue, int $line, int $linesToAggregate = 0): ErrorSuite + public function validateRuleSet(string|Vector $cellValue, int $line, int $linesToAggregate = 0): ErrorSuite { $errors = new ErrorSuite(); diff --git a/src/Validators/ValidatorColumn.php b/src/Validators/ValidatorColumn.php index 12644bd5..fa0beeed 100644 --- a/src/Validators/ValidatorColumn.php +++ b/src/Validators/ValidatorColumn.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint\Validators; +use Ds\Vector; use JBZoo\CsvBlueprint\Csv\Column; use JBZoo\CsvBlueprint\Rules\AbstarctRule; use JBZoo\CsvBlueprint\Rules\Ruleset; @@ -40,7 +41,7 @@ public function validateCell(string $cellValue, int $line): ErrorSuite return $this->cellRuleset->validateRuleSet($cellValue, $line); } - public function validateList(array $cellValue, int $linesToAggregate): ErrorSuite + public function validateList(Vector $cellValue, int $linesToAggregate): ErrorSuite { return $this->aggRuleset->validateRuleSet($cellValue, self::FALLBACK_LINE, $linesToAggregate); } diff --git a/src/Validators/ValidatorCsv.php b/src/Validators/ValidatorCsv.php index 5a0a941f..4bf90260 100644 --- a/src/Validators/ValidatorCsv.php +++ b/src/Validators/ValidatorCsv.php @@ -112,7 +112,7 @@ private function validateLines(bool $quickStop = false): ErrorSuite foreach ($mappedColumns as $columnIndex => $column) { $messPrefix = "Column \"{$column->getHumanName()}\" -"; // System message prefix. Debug only! - $columValues = []; + $columValues = new \Ds\Vector(); Utils::debug("{$messPrefix} Column start"); $colValidator = $column->getValidator(); diff --git a/tests/Commands/ValidateCsvBasicTest.php b/tests/Commands/ValidateCsvBasicTest.php index 5ccc7ffb..811cd571 100644 --- a/tests/Commands/ValidateCsvBasicTest.php +++ b/tests/Commands/ValidateCsvBasicTest.php @@ -32,7 +32,7 @@ public function testValidateOneCsvPositive(): void 'schema' => Tools::DEMO_YML_VALID, ]); - $expected = $expected = <<<'TXT' + $expected = <<<'TXT' CSV Blueprint: Unknown version (PhpUnit) Found Schemas : 1 Found CSV files : 1