Skip to content

Commit be8ac6b

Browse files
committed
Drop NSPL dependency
It is not compatible with PHP 8.1 and as dead as a dodo: ihor/NSPL#22 Let’s copy the cartesian product function and the tests into the repo, simplifying a bit where possible.
1 parent 9fdbda7 commit be8ac6b

File tree

10 files changed

+294
-114
lines changed

10 files changed

+294
-114
lines changed

app/Exporters/CsvExporter.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66

77
use App;
88
use App\Helpers\CsvWriter;
9+
use App\Helpers\Iter;
910
use App\Model\Person;
1011
use App\Model\Team;
1112
use App\Templates\Filters\CategoryFormatFilter;
1213
use Nextras\Orm\Collection\ICollection;
13-
14-
use function nspl\a\map;
15-
use function nspl\a\reduce;
16-
1714
use SplFileObject;
1815

1916
/**
@@ -50,12 +47,10 @@ public function output(): void {
5047
$file = new SplFileObject('php://output', 'a');
5148
$writer = new CsvWriter($file);
5249
$writer->addColumns(['#', 'name', 'registered', 'category', 'message']);
53-
$maxMembers = reduce(
54-
fn($maximum, $personsCount) => max($maximum, $personsCount),
55-
map(
56-
fn($team) => $team->persons->count(),
57-
$this->teams
58-
)
50+
$maxMembers = Iter::reduce(
51+
$this->teams,
52+
static fn(int $maximum, Team $team): int => max($maximum, $team->persons->count()),
53+
0,
5954
);
6055

6156
foreach ($this->teamFields as $name => $field) {

app/Forms/TeamFormFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Components\CategoryEntry;
88
use App\Components\TeamForm;
9+
use App\Helpers\Iter;
910
use App\Model\CategoryData;
1011
use Contributte\Translation\Wrappers\Message;
1112
use Kdyby\Replicator\Container as ReplicatorContainer;
@@ -17,8 +18,6 @@
1718
use Nextras\FormComponents\Controls\DateControl;
1819
use Nextras\FormsRendering\Renderers\Bs5FormRenderer;
1920

20-
use function nspl\a\last;
21-
2221
final class TeamFormFactory {
2322
use Nette\SmartObject;
2423

@@ -114,9 +113,8 @@ public function create(
114113
/** @var ReplicatorContainer */ // For PHPStan.
115114
$replicator = $button->form['persons'];
116115
if (iterator_count($replicator->getContainers()) > $minMembers) {
117-
/** @var ?Container */ // For PHPStan.
118-
$lastPerson = last($replicator->getContainers());
119-
if ($lastPerson) {
116+
$lastPerson = Iter::last($replicator->getContainers());
117+
if ($lastPerson !== null) {
120118
$replicator->remove($lastPerson, true);
121119
}
122120
} else {

app/Helpers/Iter.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
// SPDX-License-Identifier: MIT
4+
// SPDX-FileCopyrightText: 2019 Ihor Burlachenko
5+
// SPDX-FileCopyrightText: 2022 Jan Tojnar
6+
7+
declare(strict_types=1);
8+
9+
namespace App\Helpers;
10+
11+
final class Iter {
12+
/**
13+
* @template T
14+
*
15+
* @param iterable<T> $iterable
16+
* @param callable(T): bool $predicate
17+
*/
18+
public static function all(iterable $iterable, callable $predicate): bool {
19+
foreach ($iterable as $value) {
20+
if (!$predicate($value)) {
21+
return false;
22+
}
23+
}
24+
25+
return true;
26+
}
27+
28+
/**
29+
* @template T
30+
*
31+
* @param iterable<T> $iterable
32+
* @param callable(T): bool $predicate
33+
*/
34+
public static function any(iterable $iterable, callable $predicate): bool {
35+
foreach ($iterable as $value) {
36+
if ($predicate($value)) {
37+
return true;
38+
}
39+
}
40+
41+
return false;
42+
}
43+
44+
/**
45+
* @param iterable<array-key, iterable<mixed>> $inputs
46+
*
47+
* @return array<array<array-key, mixed>>
48+
*/
49+
public static function cartesianProduct(iterable $inputs): array {
50+
$result = [[]];
51+
foreach ($inputs as $key => $values) {
52+
$newResult = [];
53+
foreach ($result as $vector) {
54+
foreach ($values as $value) {
55+
$newResult[] = [...$vector, $key => $value];
56+
}
57+
}
58+
59+
$result = $newResult;
60+
}
61+
62+
return $result;
63+
}
64+
65+
/**
66+
* @template T
67+
*
68+
* @param iterable<T> $iterable
69+
*
70+
* @return ?T
71+
*/
72+
public static function last(iterable $iterable): mixed {
73+
$value = null;
74+
foreach ($iterable as $value) {
75+
}
76+
77+
return $value;
78+
}
79+
80+
/**
81+
* @template T
82+
* @template Carry
83+
*
84+
* @param iterable<T> $iterable
85+
* @param callable(Carry, T): Carry $reducer
86+
* @param Carry $initial
87+
*
88+
* @return Carry
89+
*/
90+
public static function reduce(iterable $iterable, callable $reducer, mixed $initial): mixed {
91+
$carry = $initial;
92+
foreach ($iterable as $value) {
93+
$result = $reducer($carry, $value);
94+
}
95+
96+
return $carry;
97+
}
98+
}

app/Helpers/Op.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
// SPDX-License-Identifier: MIT
4+
// SPDX-FileCopyrightText: 2022 Jan Tojnar
5+
6+
declare(strict_types=1);
7+
8+
namespace App\Helpers;
9+
10+
final class Op {
11+
/**
12+
* @template T
13+
*
14+
* @param T $value
15+
*
16+
* @return T
17+
*/
18+
public static function id(mixed $value): mixed {
19+
return $value;
20+
}
21+
22+
public static function int(string $value): int {
23+
return (int) $value;
24+
}
25+
26+
public static function lt(mixed $a, mixed $b): bool {
27+
return $a < $b;
28+
}
29+
30+
public static function le(mixed $a, mixed $b): bool {
31+
return $a <= $b;
32+
}
33+
34+
public static function idnt(mixed $a, mixed $b): bool {
35+
return $a === $b;
36+
}
37+
38+
public static function ge(mixed $a, mixed $b): bool {
39+
return $a >= $b;
40+
}
41+
42+
public static function gt(mixed $a, mixed $b): bool {
43+
return $a > $b;
44+
}
45+
}

app/model/CategoryData.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,14 @@
55
namespace App\Model;
66

77
use App;
8+
use App\Helpers\Iter;
9+
use App\Helpers\Op;
810
use Closure;
911
use Nette;
1012
use Nette\Application\Application;
1113
use Nette\Forms\Controls\SelectBox;
1214
use Nette\Localization\Translator;
1315

14-
use const nspl\a\all;
15-
use const nspl\a\any;
16-
17-
use function nspl\a\map;
18-
19-
use const nspl\f\id;
20-
use const nspl\op\ge;
21-
use const nspl\op\gt;
22-
use const nspl\op\idnt;
23-
use const nspl\op\int;
24-
use const nspl\op\le;
25-
use const nspl\op\lt;
26-
2716
final class CategoryData {
2817
use Nette\SmartObject;
2918

@@ -40,11 +29,11 @@ final class CategoryData {
4029
public const AGGREGATE_CONSTRAINT_REGEX = '(^\s*(?P<aggr>(sum|min|max))\((?P<key>age)\)(?P<op>[<>]?=?)(?P<val>[0-9]+)$\s*)';
4130

4231
public const OP_LOOKUP = [
43-
'<' => lt,
44-
'<=' => le,
45-
'=' => idnt,
46-
'>=' => ge,
47-
'>' => gt,
32+
'<' => [Op::class, 'lt'],
33+
'<=' => [Op::class, 'le'],
34+
'=' => [Op::class, 'idnt'],
35+
'>=' => [Op::class, 'ge'],
36+
'>' => [Op::class, 'gt'],
4837
];
4938

5039
public const AGGR_LOOKUP = [
@@ -64,8 +53,8 @@ final class CategoryData {
6453
];
6554

6655
public const VALUE_PARSERS = [
67-
'age' => int,
68-
'gender' => id,
56+
'age' => [Op::class, 'int'],
57+
'gender' => [Op::class, 'id'],
6958
];
7059

7160
public const KEY_MESSAGES = [
@@ -74,8 +63,8 @@ final class CategoryData {
7463
];
7564

7665
public const QUANT_LOOKUP = [
77-
'all' => all,
78-
'some' => any,
66+
'all' => [Iter::class, 'all'],
67+
'some' => [Iter::class, 'any'],
7968
];
8069

8170
public function __construct(
@@ -298,7 +287,7 @@ function(SelectBox $entry) use ($aggr, $keyProjection, $op, $comparedValue): boo
298287
$members = $form->getUnsafeValues(null)['persons'];
299288
\assert($members instanceof \Iterator); // For PHPStan.
300289

301-
return $op($aggr(map($keyProjection, iterator_to_array($members))), $comparedValue);
290+
return $op($aggr(array_map($keyProjection, iterator_to_array($members))), $comparedValue);
302291
},
303292
$this->translator->translate($message),
304293
];

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"php": ">= 8.1",
1515
"contributte/mail": "^0.6.0",
1616
"contributte/translation": "^2.0",
17-
"ihor/nspl": "^1.3",
1817
"kdyby/forms-replicator": "^2.0.0",
1918
"latte/latte": "~2.5",
2019
"moneyphp/money": "^4.0",

composer.lock

Lines changed: 1 addition & 73 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)