Code Duplication    Length = 40-43 lines in 2 locations

src/UI/Resolver/Validator/Implementation/Pragmatic/InArrayValidatorTrait.php 1 location

@@ 9-48 (lines=40) @@
6
use AmmitPhp\Ammit\UI\Resolver\UIValidationEngine;
7
use AmmitPhp\Ammit\UI\Resolver\Validator\UIValidatorInterface;
8
9
trait InArrayValidatorTrait
10
{
11
    /** @var UIValidationEngine */
12
    protected $validationEngine;
13
14
    /**
15
     * Exceptions are caught in order to be processed later
16
     * @param mixed $value Array ?
17
     *
18
     * @return mixed
19
     */
20
    public function mustBeInArray($value, array $availableValues, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
21
    {
22
        $this->validationEngine->validateFieldValue(
23
            $parentValidator ?: $this,
24
            function() use ($value, $availableValues, $propertyPath, $exceptionMessage) {
25
                if (in_array($value, $availableValues, true)) {
26
                    return;
27
                }
28
29
                if (null === $exceptionMessage) {
30
                    $exceptionMessage = sprintf(
31
                        'Value "%s" is not valid. Available values are "%s".',
32
                        $value,
33
                        implode('", "', $availableValues)
34
                    );
35
                }
36
37
                throw new InvalidArgumentException(
38
                    $exceptionMessage,
39
                    0,
40
                    $propertyPath,
41
                    $value
42
                );
43
            }
44
        );
45
46
        return $value;
47
    }
48
}
49

src/UI/Resolver/Validator/Implementation/Pure/ArrayValidatorTrait.php 1 location

@@ 9-51 (lines=43) @@
6
use AmmitPhp\Ammit\UI\Resolver\UIValidationEngine;
7
use AmmitPhp\Ammit\UI\Resolver\Validator\UIValidatorInterface;
8
9
trait ArrayValidatorTrait
10
{
11
    /** @var UIValidationEngine */
12
    protected $validationEngine;
13
14
    /**
15
     * Exceptions are caught in order to be processed later
16
     * @param mixed $value Array ?
17
     *
18
     * @return array Empty array if not array
19
     */
20
    public function mustBeArray($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): array
21
    {
22
        $this->validationEngine->validateFieldValue(
23
            $parentValidator ?: $this,
24
            function() use ($value, $propertyPath, $exceptionMessage) {
25
                if (is_array($value)) {
26
                    return;
27
                }
28
29
                if (null === $exceptionMessage) {
30
                    $exceptionMessage = sprintf(
31
                        'Value "%s" is not an array.',
32
                        $value
33
                    );
34
                }
35
36
                throw new InvalidArgumentException(
37
                    $exceptionMessage,
38
                    0,
39
                    $propertyPath,
40
                    $value
41
                );
42
            }
43
        );
44
45
        if (!is_array($value)) {
46
            return [];
47
        }
48
49
        return $value;
50
    }
51
}
52