respect-validation/library/Rules/NotBlank.php
Henrique Moody 0066786fa7
Remove deprecated methods check() and validate()
We want to release version 3.0 as fresh as possible, without having to
maintain backward compatibility with the previous versions.

Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
2025-12-18 17:29:02 +01:00

56 lines
1.2 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Attribute;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use stdClass;
use function array_filter;
use function is_array;
use function is_numeric;
use function is_string;
use function trim;
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
#[Template(
'{{name}} must not be blank',
'{{name}} must be blank',
)]
final class NotBlank implements Rule
{
public function evaluate(mixed $input): Result
{
return new Result($this->isBlank($input), $input, $this);
}
private function isBlank(mixed $input): bool
{
if (is_numeric($input)) {
return $input != 0;
}
if (is_string($input)) {
$input = trim($input);
}
if ($input instanceof stdClass) {
$input = (array) $input;
}
if (is_array($input)) {
$input = array_filter($input, fn($value) => $this->isBlank($value));
}
return !empty($input);
}
}