respect-validation/library/Rules/ContainsAny.php
Alexandre Gomes Gaigalas ab3732f91f Use SPDX IDs for licensing
SPDX IDs are shorter than licensing notes previously used, and
adhere better to FOSS standards. It is also machine-readable.
2023-02-19 00:19:10 -03:00

51 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 function array_map;
/**
* Validates if the input contains at least one of defined values
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Kirill Dlussky <kirill@dlussky.ru>
*/
final class ContainsAny extends AbstractEnvelope
{
/**
* Initializes the rule.
*
* @param mixed[] $needles At least one of the values provided must be found in input string or array
* @param bool $identical Defines whether the value should be compared strictly, when validating array
*/
public function __construct(array $needles, bool $identical = false)
{
parent::__construct(
new AnyOf(...$this->getRules($needles, $identical)),
['needles' => $needles]
);
}
/**
* @param mixed[] $needles
*
* @return Contains[]
*/
private function getRules(array $needles, bool $identical): array
{
return array_map(
static function ($needle) use ($identical): Contains {
return new Contains($needle, $identical);
},
$needles
);
}
}