Skip to content

Commit cc94c43

Browse files
committed
Merge branch 'develop'
* develop: update copyright specify next release add Is::value() reuse workflows reuse workflows to test against php 8.4 update changelog fix php 8.4 deprecations use innmind/static-analysis
2 parents cd7407b + 70982d6 commit cc94c43

File tree

10 files changed

+142
-123
lines changed

10 files changed

+142
-123
lines changed

.github/workflows/ci.yml

+7-86
Original file line numberDiff line numberDiff line change
@@ -4,92 +4,13 @@ on: [push]
44

55
jobs:
66
blackbox:
7-
runs-on: ${{ matrix.os }}
8-
strategy:
9-
matrix:
10-
os: [ubuntu-latest, macOS-latest]
11-
php-version: ['8.2', '8.3']
12-
dependency-versions: ['lowest', 'highest']
13-
name: 'BlackBox'
14-
steps:
15-
- name: Checkout
16-
uses: actions/checkout@v4
17-
- name: Setup PHP
18-
uses: shivammathur/setup-php@v2
19-
with:
20-
php-version: ${{ matrix.php-version }}
21-
extensions: mbstring, intl
22-
coverage: none
23-
- name: Composer
24-
uses: "ramsey/composer-install@v3"
25-
with:
26-
dependency-versions: ${{ matrix.dependencies }}
27-
- name: BlackBox
28-
run: php blackbox.php
7+
uses: innmind/github-workflows/.github/workflows/black-box-matrix.yml@main
298
coverage:
30-
runs-on: ${{ matrix.os }}
31-
strategy:
32-
matrix:
33-
os: [ubuntu-latest, macOS-latest]
34-
php-version: ['8.2', '8.3']
35-
dependency-versions: ['lowest', 'highest']
36-
name: 'Coverage'
37-
steps:
38-
- name: Checkout
39-
uses: actions/checkout@v4
40-
- name: Setup PHP
41-
uses: shivammathur/setup-php@v2
42-
with:
43-
php-version: ${{ matrix.php-version }}
44-
extensions: mbstring, intl
45-
coverage: xdebug
46-
- name: Composer
47-
uses: "ramsey/composer-install@v3"
48-
with:
49-
dependency-versions: ${{ matrix.dependencies }}
50-
- name: BlackBox
51-
run: php blackbox.php
52-
env:
53-
ENABLE_COVERAGE: 'true'
54-
- uses: codecov/codecov-action@v4
55-
with:
56-
token: ${{ secrets.CODECOV_TOKEN }}
9+
uses: innmind/github-workflows/.github/workflows/coverage-matrix.yml@main
10+
secrets: inherit
5711
psalm:
58-
runs-on: ubuntu-latest
59-
strategy:
60-
matrix:
61-
php-version: ['8.2', '8.3']
62-
dependency-versions: ['lowest', 'highest']
63-
name: 'Psalm'
64-
steps:
65-
- name: Checkout
66-
uses: actions/checkout@v4
67-
- name: Setup PHP
68-
uses: shivammathur/setup-php@v2
69-
with:
70-
php-version: ${{ matrix.php-version }}
71-
extensions: mbstring, intl
72-
- name: Composer
73-
uses: "ramsey/composer-install@v3"
74-
with:
75-
dependency-versions: ${{ matrix.dependencies }}
76-
- name: Psalm
77-
run: vendor/bin/psalm --shepherd
12+
uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@main
7813
cs:
79-
runs-on: ubuntu-latest
80-
strategy:
81-
matrix:
82-
php-version: ['8.2']
83-
name: 'CS'
84-
steps:
85-
- name: Checkout
86-
uses: actions/checkout@v4
87-
- name: Setup PHP
88-
uses: shivammathur/setup-php@v2
89-
with:
90-
php-version: ${{ matrix.php-version }}
91-
extensions: mbstring, intl
92-
- name: Composer
93-
uses: "ramsey/composer-install@v3"
94-
- name: CS
95-
run: vendor/bin/php-cs-fixer fix --diff --dry-run
14+
uses: innmind/github-workflows/.github/workflows/cs.yml@main
15+
with:
16+
php-version: '8.2'

.github/workflows/documentation.yml

+2-19
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,5 @@ permissions:
66
contents: write
77
jobs:
88
deploy:
9-
runs-on: ubuntu-latest
10-
steps:
11-
- uses: actions/checkout@v4
12-
- name: Configure Git Credentials
13-
run: |
14-
git config user.name github-actions[bot]
15-
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
16-
- uses: actions/setup-python@v5
17-
with:
18-
python-version: 3.x
19-
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
20-
- uses: actions/cache@v4
21-
with:
22-
key: mkdocs-material-${{ env.cache_id }}
23-
path: .cache
24-
restore-keys: |
25-
mkdocs-material-
26-
- run: pip install mkdocs-material
27-
- run: mkdocs gh-deploy --force
9+
uses: innmind/github-workflows/.github/workflows/documentation.yml@main
10+
secrets: inherit

.github/workflows/release.yml

+2-14
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,7 @@ on:
55
tags:
66
- '*'
77

8-
permissions:
9-
contents: write
10-
118
jobs:
129
release:
13-
name: Create release
14-
runs-on: ubuntu-22.04
15-
steps:
16-
- name: Create release
17-
env:
18-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19-
tag: ${{ github.ref_name }}
20-
run: |
21-
gh release create "$tag" \
22-
--repo="$GITHUB_REPOSITORY" \
23-
--generate-notes
10+
uses: innmind/github-workflows/.github/workflows/release.yml@main
11+
secrets: inherit

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 1.8.0 - 2025-02-09
4+
5+
### Added
6+
7+
- `Is::value()` to allow to defined discriminators
8+
9+
### Fixed
10+
11+
- Support for PHP `8.4`
12+
313
## 1.7.0 - 2024-11-11
414

515
### Changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Innmind
3+
Copyright (c) 2025 Innmind
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
},
2727
"require-dev": {
28-
"vimeo/psalm": "~5.0",
28+
"innmind/static-analysis": "~1.1",
2929
"innmind/black-box": "~5.5",
3030
"innmind/coding-standard": "~2.0"
3131
}

docs/constraints/primitives.md

+46
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,49 @@ use Innmind\Validation\Is;
6868

6969
$validate = Is::list(Is::string());
7070
```
71+
72+
## Specified value
73+
74+
This constraint makes sure the the input value is the expected one.
75+
76+
```php
77+
use Innmind\Validation\Is;
78+
79+
$validate = Is::value(42);
80+
```
81+
82+
If you call the constraint with any other value than `42`, the validation will fail. Of course you can specify any value of any type you wish.
83+
84+
??? tip
85+
This is especially useful to define discriminators when the input can be multiple [shapes](array-shapes.md) that are defined by a key.
86+
87+
```php
88+
use Innmind\Validation\Is;
89+
90+
$shapeA = Is::shape('discriminator', Is::value('a'))
91+
->with('some-key', Is::string());
92+
$shapeB = Is::shape('discriminator', Is::value('b'))
93+
->with('other-key', Is::int());
94+
95+
$validate = $shapeA->or($shapeB);
96+
```
97+
98+
If you can `$validate` with one of the following values it will succeed:
99+
100+
=== "A"
101+
```php
102+
[
103+
'discriminator' => 'a',
104+
'some-key' => 'foo',
105+
]
106+
```
107+
108+
=== "B"
109+
```php
110+
[
111+
'discriminator' => 'b',
112+
'other-key' => 42,
113+
]
114+
```
115+
116+
Otherwise it will fail for any other array shape.

proofs/is.php

+49
Original file line numberDiff line numberDiff line change
@@ -643,4 +643,53 @@ static function($assert, $value) {
643643
);
644644
},
645645
);
646+
647+
yield proof(
648+
'Is::value()',
649+
given(
650+
Set\Type::any(),
651+
Set\Type::any(),
652+
Set\Strings::any(),
653+
)->filter(static fn($a, $b) => $a !== $b),
654+
static function($assert, $a, $b, $message) {
655+
$assert->same(
656+
$a,
657+
Is::value($a)($a)->match(
658+
static fn($value) => $value,
659+
static fn() => null,
660+
),
661+
);
662+
$assert->null(
663+
Is::value($a)($b)->match(
664+
static fn($value) => $value,
665+
static fn() => null,
666+
),
667+
);
668+
$assert->same(
669+
[['$', $message]],
670+
Is::value($a, $message)($b)->match(
671+
static fn() => null,
672+
static fn($failures) => $failures
673+
->map(static fn($failure) => [
674+
$failure->path()->toString(),
675+
$failure->message(),
676+
])
677+
->toList(),
678+
),
679+
);
680+
$assert
681+
->string(
682+
Is::value($a)($b)->match(
683+
static fn() => null,
684+
static fn($failures) => $failures
685+
->map(static fn($failure) => [
686+
$failure->path()->toString(),
687+
$failure->message(),
688+
])
689+
->toList()[0][1],
690+
),
691+
)
692+
->startsWith('Not of expected value of type');
693+
},
694+
);
646695
};

src/Is.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static function null(): self
125125
*
126126
* @return Constraint<mixed, list<E>>
127127
*/
128-
public static function list(Constraint $each = null): Constraint
128+
public static function list(?Constraint $each = null): Constraint
129129
{
130130
/** @var self<array, list<mixed>> */
131131
$list = new self(\array_is_list(...), 'list');
@@ -182,6 +182,28 @@ public static function just(?string $message = null): Constraint
182182
));
183183
}
184184

185+
/**
186+
* @psalm-pure
187+
* @template V
188+
*
189+
* @param V $value
190+
* @param ?non-empty-string $message
191+
*
192+
* @return Constraint<mixed, V>
193+
*/
194+
public static function value(mixed $value, ?string $message = null): Constraint
195+
{
196+
return Of::callable(static fn(mixed $in) => match ($in) {
197+
$value => Validation::success($value),
198+
default => Validation::fail(Failure::of(
199+
$message ?? \sprintf(
200+
'Not of expected value of type %s',
201+
\gettype($value),
202+
),
203+
)),
204+
});
205+
}
206+
185207
/**
186208
* @param non-empty-string $message
187209
*

src/Shape.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function with(string $key, Constraint $constraint): self
8787
/**
8888
* @param non-empty-string $key
8989
*/
90-
public function optional(string $key, Constraint $constraint = null): self
90+
public function optional(string $key, ?Constraint $constraint = null): self
9191
{
9292
$optional = $this->optional;
9393
$optional[] = $key;

0 commit comments

Comments
 (0)