if (array_key_exists('address', $data) && !is_array($data['address'])) {
// throw new
}
$constraint = new Assert\Collection([
'address' => [
new Assert\Blank(),
new Assert\Type('array'),
],
]);
$validator = (new ValidatorBuilder())->getValidator();
$constraints = [
new Assert\Collection([
'fields' => [
'address' => [
new Assert\NotNull(),
new Assert\Type('array'),
],
],
'allowMissingFields' => true,
'allowExtraFields' => true,
]),
new Assert\Collection([
'fields' => [
'required' => [
new Assert\NotNull(),
],
],
'allowExtraFields' => true,
]),
];
$data = [];
dump($data);
dump($validator->validate($data, $constraints)); // Ko
$data = [
'address' => null,
];
dump($data);
dump($validator->validate($data, $constraints)); // Ko
$data = [
'address' => [],
];
dump($data);
dump($validator->validate($data, $constraints)); // Ko
$data = [
'address' => 'address',
];
dump($data);
dump($validator->validate($data, $constraints)); // Ko
$data = [
'address' => [],
'required' => 'value',
];
dump($data);
dump($validator->validate($data, $constraints)); // Ok
new Assert\Collection([
'address' => ...
new Assert\Expression([
'expression' => 'this.isValidAddress()',
'message' => 'Адрес или пустой или массив',
])
public function isValidAddress(): bool
{
return $this->address === null || \is_array($this->address);
}