Skip to content
This repository was archived by the owner on Jul 6, 2024. It is now read-only.

Latest commit

 

History

History
54 lines (47 loc) · 1.01 KB

File metadata and controls

54 lines (47 loc) · 1.01 KB

CallbackConstraint

<?php

use Chubbyphp\Validation\Constraint\CallbackConstraint;
use Chubbyphp\Validation\Error\Error;
use Chubbyphp\Validation\ValidatorContextInterface;
use Chubbyphp\Validation\ValidatorInterface;

$constraint = new CallbackConstraint(
    function (
        string $path,
        $value,
        ValidatorContextInterface $context,
        ValidatorInterface $validator = null
    ) {
        if (null === $value){
            return [];
        }

        return [
            new Error(
                $path,
                'constraint.callback',
                ['value' => $value]
            )
        ];
    }
);

/** @var ValidatorContextInterface $context */
$context = ...;

$errors = $constraint->validate(
    'path.to.property',
    null,
    $context
);
// [];

$errors = $constraint->validate(
    'path.to.property',
    'value',
    $context
);
// [
//     new Error(
//         'path.to.property',
//         'constraint.callback',
//         ['value' => 'value']
//     )
// ];