1. Go to this page and download the library: Download tetthys/commit-validator 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/ */
tetthys / commit-validator example snippets
use CommitValidator\Attributes\Required;
use CommitValidator\Attributes\Length;
final readonly class PostDTO
{
public function __construct(
#[Required('Title is essage: 'Body must be at least 10 chars.')]
public string $body,
) {}
}
[
"product.images" => [
["code" => "min", "message" => "Images must be between 1 and 6."],
],
"product.images.2.url" => [
["code" => "regex", "message" => "Image URL must be a valid http(s) URL."],
],
]
declare(strict_types=1);
namespace App;
use CommitValidator\Attributes\Required;
use CommitValidator\Attributes\Length;
final readonly class CommitPayload
{
public function __construct(
#[Required('Title is blic string $body,
) {}
}
$finalPayload = [
'title' => 'Hello world',
'body' => 'This is a long enough body...',
];
$dto = new \App\CommitPayload(
title: (string)($finalPayload['title'] ?? ''),
body: (string)($finalPayload['body'] ?? ''),
);
use CommitValidator\CommitValidator;
$result = CommitValidator::for(\App\CommitPayload::class)->validate($dto);
if (!$result->isValid()) {
print_r($result->errors());
// abort commit
}
// e.g. app/helpers.php (or any file you autoload)
function cv(): CommitValidator\Rules\Builder
{
return new CommitValidator\Rules\Builder();
}
$validator = CommitValidator::for(\App\CommitPayload::class)
->with('title', cv()->minLen(10, 'Commit title must be >= 10.')->build());
use CommitValidator\Attributes\Required;
use CommitValidator\Attributes\Regex;
use CommitValidator\Attributes\InSet;
final readonly class UserDTO
{
public function __construct(
#[Required]
#[Regex('/^[a-z0-9_]{3,20}$/u', 'Username must be 3..20 lowercase chars.')]
public string $username,
#[Required]
#[InSet(['user', 'admin'], 'Role must be user|admin.')]
public string $role,
) {}
}
use CommitValidator\Attributes\Nested;
use CommitValidator\Attributes\Required;
final readonly class AddressDTO
{
public function __construct(
#[Required] public string $city,
#[Required] public string $street,
) {}
}
final readonly class ProfileDTO
{
public function __construct(
#[Nested(AddressDTO::class)]
public ?AddressDTO $address = null,
) {}
}
use CommitValidator\Attributes\Each;
use CommitValidator\Attributes\Required;
final readonly class ItemDTO
{
public function __construct(
#[Required] public string $name,
) {}
}
final readonly class CartDTO
{
public function __construct(
#[Each(ItemDTO::class)]
public array $items = [],
) {}
}
use CommitValidator\Attributes\Count;
final readonly class GalleryDTO
{
public function __construct(
#[Count(min: 1, max: 6, message: 'Images must be between 1 and 6.')]
public array $images = [],
) {}
}
use CommitValidator\CommitValidator;
$validator = CommitValidator::for(\App\SomeDTO::class);
$result = $validator->validate($dto);
if ($result->isValid()) {
// safe to commit
} else {
$errors = $result->errors();
}
use CommitValidator\CommitValidator;
$validator = CommitValidator::for(\App\CommitPayload::class)
->with('title', cv()->minLen(10, 'Commit title must be >= 10.')->build())
->with('images', cv()->count(1, 6, 'Images must be between 1 and 6.')->build());
$result = $validator->validate($dto);
use CommitValidator\CommitValidator;
$product = \App\ProductDTO::fromArray($final['product'] ?? []);
$result = CommitValidator::for(\App\ProductDTO::class)->validate($product);
if (!$result->isValid()) {
return $result->errors();
}
// persist to DB
declare(strict_types=1);
namespace App;
use CommitValidator\Attributes\Count;
use CommitValidator\Attributes\Each;
use CommitValidator\Attributes\Required;
use CommitValidator\Attributes\Regex;
final readonly class ImageDTO
{
public function __construct(
#[Required]
#[Regex('/^https?:\/\/.+/u', 'Image URL must be a valid http(s) URL.')]
public string $url,
) {}
}
final readonly class ProductDTO
{
public function __construct(
#[Required('Images are
use CommitValidator\CommitValidator;
$product = \App\ProductDTO::fromArray($final['product'] ?? []);
$result = CommitValidator::for(\App\ProductDTO::class)->validate($product);
if (!$result->isValid()) {
print_r($result->errors());
}
use CommitValidator\Attributes\Nested;
use CommitValidator\Attributes\Each;
use CommitValidator\Attributes\Required;
final readonly class AddressDTO
{
public function __construct(
#[Required] public string $city,
#[Required] public string $street,
) {}
}
final readonly class ItemDTO
{
public function __construct(
#[Required] public string $name,
) {}
}
final readonly class OrderDTO
{
public function __construct(
#[Nested(AddressDTO::class)]
public ?AddressDTO $address = null,
#[Each(ItemDTO::class)]
public array $items = [],
) {}
}
use CommitValidator\CommitValidator;
$validator = CommitValidator::for(\App\ProductDTO::class)
->with('images', cv()->
function cv(): CommitValidator\Rules\Builder
{
return new CommitValidator\Rules\Builder();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.