Skip to content

Commit

Permalink
Add additional count rules
Browse files Browse the repository at this point in the history
Expanded the counting features in the schema and implemented the corresponding rules. This included adding specific counts for distinct, positive, negative, zero, even, odd, and prime values. This enhancement now allows greater flexibility for users working with different data sets.
  • Loading branch information
SmetDenis committed Mar 25, 2024
1 parent bf9f7cc commit 21d1c41
Show file tree
Hide file tree
Showing 25 changed files with 868 additions and 8 deletions.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Stable Version](https://poser.pugx.org/jbzoo/csv-blueprint/version)](https://packagist.org/packages/jbzoo/csv-blueprint/) [![Total Downloads](https://poser.pugx.org/jbzoo/csv-blueprint/downloads)](https://packagist.org/packages/jbzoo/csv-blueprint/stats) [![Docker Pulls](https://img.shields.io/docker/pulls/jbzoo/csv-blueprint.svg)](https://hub.docker.com/r/jbzoo/csv-blueprint/tags) [![GitHub License](https://img.shields.io/github/license/jbzoo/csv-blueprint)](https://github.com/JBZoo/Csv-Blueprint/blob/master/LICENSE)

<!-- rules-counter -->
[![Static Badge](https://img.shields.io/badge/Rules-234-green?label=Total%20Number%20of%20Rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml) [![Static Badge](https://img.shields.io/badge/Rules-66-green?label=Cell%20Value&labelColor=blue&color=gray)](src/Rules/Cell) [![Static Badge](https://img.shields.io/badge/Rules-163-green?label=Aggregate%20Column&labelColor=blue&color=gray)](src/Rules/Aggregate) [![Static Badge](https://img.shields.io/badge/Rules-5-green?label=Extra%20Checks&labelColor=blue&color=gray)](#extra-checks) [![Static Badge](https://img.shields.io/badge/Rules-275-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
[![Static Badge](https://img.shields.io/badge/Rules-276-green?label=Total%20Number%20of%20Rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml) [![Static Badge](https://img.shields.io/badge/Rules-66-green?label=Cell%20Value&labelColor=blue&color=gray)](src/Rules/Cell) [![Static Badge](https://img.shields.io/badge/Rules-205-green?label=Aggregate%20Column&labelColor=blue&color=gray)](src/Rules/Aggregate) [![Static Badge](https://img.shields.io/badge/Rules-5-green?label=Extra%20Checks&labelColor=blue&color=gray)](#extra-checks) [![Static Badge](https://img.shields.io/badge/Rules-233-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
<!-- /rules-counter -->

## Introduction
Expand Down Expand Up @@ -349,6 +349,62 @@ columns:
count_not_empty_less: 8 # x < 8
count_not_empty_max: 9 # x <= 9

# Number of unique values.
count_distinct_min: 1 # x >= 1
count_distinct_greater: 2 # x > 2
count_distinct_not: 0 # x != 0
count_distinct: 7 # x == 7
count_distinct_less: 8 # x < 8
count_distinct_max: 9 # x <= 9

# Number of positive values.
count_positive_min: 1 # x >= 1
count_positive_greater: 2 # x > 2
count_positive_not: 0 # x != 0
count_positive: 7 # x == 7
count_positive_less: 8 # x < 8
count_positive_max: 9 # x <= 9

# Number of negative values.
count_negative_min: 1 # x >= 1
count_negative_greater: 2 # x > 2
count_negative_not: 0 # x != 0
count_negative: 7 # x == 7
count_negative_less: 8 # x < 8
count_negative_max: 9 # x <= 9

# Number of zero values. Any text and spaces (i.e. anything that doesn't look like a number) will be converted to 0.
count_zero_min: 1 # x >= 1
count_zero_greater: 2 # x > 2
count_zero_not: 0 # x != 0
count_zero: 7 # x == 7
count_zero_less: 8 # x < 8
count_zero_max: 9 # x <= 9

# Number of even values.
count_even_min: 1 # x >= 1
count_even_greater: 2 # x > 2
count_even_not: 0 # x != 0
count_even: 7 # x == 7
count_even_less: 8 # x < 8
count_even_max: 9 # x <= 9

# Number of odd values.
count_odd_min: 1 # x >= 1
count_odd_greater: 2 # x > 2
count_odd_not: 0 # x != 0
count_odd: 7 # x == 7
count_odd_less: 8 # x < 8
count_odd_max: 9 # x <= 9

# Number of prime values.
count_prime_min: 1 # x >= 1
count_prime_greater: 2 # x > 2
count_prime_not: 0 # x != 0
count_prime: 7 # x == 7
count_prime_less: 8 # x < 8
count_prime_max: 9 # x <= 9

# Calculate the median average of a list of numbers.
# See: https://en.wikipedia.org/wiki/Median
median_min: 1.0 # x >= 1.0
Expand Down
49 changes: 49 additions & 0 deletions schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,55 @@
"count_not_empty_less" : 8,
"count_not_empty_max" : 9,

"count_distinct_min" : 1,
"count_distinct_greater" : 2,
"count_distinct_not" : 0,
"count_distinct" : 7,
"count_distinct_less" : 8,
"count_distinct_max" : 9,

"count_positive_min" : 1,
"count_positive_greater" : 2,
"count_positive_not" : 0,
"count_positive" : 7,
"count_positive_less" : 8,
"count_positive_max" : 9,

"count_negative_min" : 1,
"count_negative_greater" : 2,
"count_negative_not" : 0,
"count_negative" : 7,
"count_negative_less" : 8,
"count_negative_max" : 9,

"count_zero_min" : 1,
"count_zero_greater" : 2,
"count_zero_not" : 0,
"count_zero" : 7,
"count_zero_less" : 8,
"count_zero_max" : 9,

"count_even_min" : 1,
"count_even_greater" : 2,
"count_even_not" : 0,
"count_even" : 7,
"count_even_less" : 8,
"count_even_max" : 9,

"count_odd_min" : 1,
"count_odd_greater" : 2,
"count_odd_not" : 0,
"count_odd" : 7,
"count_odd_less" : 8,
"count_odd_max" : 9,

"count_prime_min" : 1,
"count_prime_greater" : 2,
"count_prime_not" : 0,
"count_prime" : 7,
"count_prime_less" : 8,
"count_prime_max" : 9,

"median_min" : 1,
"median_greater" : 2,
"median_not" : 5,
Expand Down
49 changes: 49 additions & 0 deletions schema-examples/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,55 @@
'count_not_empty_less' => 8,
'count_not_empty_max' => 9,

'count_distinct_min' => 1,
'count_distinct_greater' => 2,
'count_distinct_not' => 0,
'count_distinct' => 7,
'count_distinct_less' => 8,

'count_distinct_max' => 9,
'count_positive_min' => 1,
'count_positive_greater' => 2,
'count_positive_not' => 0,
'count_positive' => 7,
'count_positive_less' => 8,

'count_positive_max' => 9,
'count_negative_min' => 1,
'count_negative_greater' => 2,
'count_negative_not' => 0,
'count_negative' => 7,
'count_negative_less' => 8,

'count_negative_max' => 9,
'count_zero_min' => 1,
'count_zero_greater' => 2,
'count_zero_not' => 0,
'count_zero' => 7,
'count_zero_less' => 8,

'count_zero_max' => 9,
'count_even_min' => 1,
'count_even_greater' => 2,
'count_even_not' => 0,
'count_even' => 7,
'count_even_less' => 8,

'count_even_max' => 9,
'count_odd_min' => 1,
'count_odd_greater' => 2,
'count_odd_not' => 0,
'count_odd' => 7,
'count_odd_less' => 8,

'count_odd_max' => 9,
'count_prime_min' => 1,
'count_prime_greater' => 2,
'count_prime_not' => 0,
'count_prime' => 7,
'count_prime_less' => 8,
'count_prime_max' => 9,

'median_min' => 1.0,
'median_greater' => 2.0,
'median_not' => 5.0,
Expand Down
56 changes: 56 additions & 0 deletions schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,62 @@ columns:
count_not_empty_less: 8 # x < 8
count_not_empty_max: 9 # x <= 9

# Number of unique values.
count_distinct_min: 1 # x >= 1
count_distinct_greater: 2 # x > 2
count_distinct_not: 0 # x != 0
count_distinct: 7 # x == 7
count_distinct_less: 8 # x < 8
count_distinct_max: 9 # x <= 9

# Number of positive values.
count_positive_min: 1 # x >= 1
count_positive_greater: 2 # x > 2
count_positive_not: 0 # x != 0
count_positive: 7 # x == 7
count_positive_less: 8 # x < 8
count_positive_max: 9 # x <= 9

# Number of negative values.
count_negative_min: 1 # x >= 1
count_negative_greater: 2 # x > 2
count_negative_not: 0 # x != 0
count_negative: 7 # x == 7
count_negative_less: 8 # x < 8
count_negative_max: 9 # x <= 9

# Number of zero values. Any text and spaces (i.e. anything that doesn't look like a number) will be converted to 0.
count_zero_min: 1 # x >= 1
count_zero_greater: 2 # x > 2
count_zero_not: 0 # x != 0
count_zero: 7 # x == 7
count_zero_less: 8 # x < 8
count_zero_max: 9 # x <= 9

# Number of even values.
count_even_min: 1 # x >= 1
count_even_greater: 2 # x > 2
count_even_not: 0 # x != 0
count_even: 7 # x == 7
count_even_less: 8 # x < 8
count_even_max: 9 # x <= 9

# Number of odd values.
count_odd_min: 1 # x >= 1
count_odd_greater: 2 # x > 2
count_odd_not: 0 # x != 0
count_odd: 7 # x == 7
count_odd_less: 8 # x < 8
count_odd_max: 9 # x <= 9

# Number of prime values.
count_prime_min: 1 # x >= 1
count_prime_greater: 2 # x > 2
count_prime_not: 0 # x != 0
count_prime: 7 # x == 7
count_prime_less: 8 # x < 8
count_prime_max: 9 # x <= 9

# Calculate the median average of a list of numbers.
# See: https://en.wikipedia.org/wiki/Median
median_min: 1.0 # x >= 1.0
Expand Down
49 changes: 49 additions & 0 deletions schema-examples/full_clean.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,55 @@ columns:
count_not_empty_less: 8
count_not_empty_max: 9

count_distinct_min: 1
count_distinct_greater: 2
count_distinct_not: 0
count_distinct: 7
count_distinct_less: 8
count_distinct_max: 9

count_positive_min: 1
count_positive_greater: 2
count_positive_not: 0
count_positive: 7
count_positive_less: 8
count_positive_max: 9

count_negative_min: 1
count_negative_greater: 2
count_negative_not: 0
count_negative: 7
count_negative_less: 8
count_negative_max: 9

count_zero_min: 1
count_zero_greater: 2
count_zero_not: 0
count_zero: 7
count_zero_less: 8
count_zero_max: 9

count_even_min: 1
count_even_greater: 2
count_even_not: 0
count_even: 7
count_even_less: 8
count_even_max: 9

count_odd_min: 1
count_odd_greater: 2
count_odd_not: 0
count_odd: 7
count_odd_less: 8
count_odd_max: 9

count_prime_min: 1
count_prime_greater: 2
count_prime_not: 0
count_prime: 7
count_prime_less: 8
count_prime_max: 9

median_min: 1.0
median_greater: 2.0
median_not: 5.0
Expand Down
40 changes: 40 additions & 0 deletions src/Rules/Aggregate/ComboCountDistinct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCountDistinct extends AbstarctAggregateRuleCombo
{
public const INPUT_TYPE = AbstarctRule::INPUT_TYPE_STRINGS;

protected const NAME = 'number of unique values';

public function getHelpMeta(): array
{
return [['Number of unique values.'], []];
}

protected function getActualAggregate(array $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
}

return \count(\array_unique($colValues));
}
}
4 changes: 4 additions & 0 deletions src/Rules/Aggregate/ComboCountEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function getHelpMeta(): array

protected function getActualAggregate(array $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
}

return \count(\array_filter($colValues, static fn ($colValue) => $colValue === ''));
}
}
40 changes: 40 additions & 0 deletions src/Rules/Aggregate/ComboCountEven.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCountEven extends AbstarctAggregateRuleCombo
{
public const INPUT_TYPE = AbstarctRule::INPUT_TYPE_FLOATS;

protected const NAME = 'number of even values';

public function getHelpMeta(): array
{
return [['Number of even values.'], []];
}

protected function getActualAggregate(array $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
}

return \count(\array_filter(self::stringsToFloat($colValues), static fn ($value) => $value % 2 === 0));
}
}
Loading

0 comments on commit 21d1c41

Please sign in to comment.