1. Go to this page and download the library: Download innmind/validation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
innmind / validation example snippets
use Innmind\Validation\{
Shape,
Is,
Each,
Failure,
};
use Innmind\Immutable\Sequence;
$valid = [
'id' => 42,
'username' => 'jdoe',
'addresses' => [
'address 1',
'address 2',
],
'submit' => true,
];
$invalid = [
'id' => '42',
'addresses' => [
'address 1',
null,
],
'submit' => true,
];
$validate = Shape::of('id', Is::int())
->with('username', Is::string())
->with(
'addresses',
Is::list(
Is::string()->map(
static fn(string $address) => new YourModel($address),
)
)
);
$result = $validate($valid)->match(
static fn(array $value) => $value,
static fn() => throw new \RuntimeException('invalid data'),
);
// Here $result looks like:
// [
// 'id' => 42
// 'username' => 'jdoe',
// 'addresses' [
// new YourModel('address 1'),
// new YourModel('address 2'),
// ],
// ]
$errors = $validate($invalid)->match(
static fn() => null,
static fn(Sequence $failures) => $failures
->map(static fn(Failure $failure) => [
$failure->path()->toString(),
$failure->message(),
])
->toList(),
);
// Here $errors looks like:
// [
// ['id', 'Value is not of type int'],
// ['$', 'The key username is mission'],
// ['addresses', 'Value is not of type string']
// ]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.