Callback::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\StringifyCallbackTrait;
12
use Particle\Validator\Exception\InvalidValueException;
13
use Particle\Validator\Rule;
14
use Particle\Validator\Value\Container;
15
16
/**
17
 * This rule is for validating a value with a custom callback.
18
 *
19
 * @package Particle\Validator\Rule
20
 */
21
class Callback extends Rule
22
{
23
    use StringifyCallbackTrait;
24
25
    /**
26
     * A constant that will be used to indicate that the callback returned false.
27
     */
28
    const INVALID_VALUE = 'Callback::INVALID_VALUE';
29
30
    /**
31
     * The message templates which can be returned by this validator.
32
     *
33
     * @var array
34
     */
35
    protected $messageTemplates = [
36
        self::INVALID_VALUE => '{{ name }} is invalid',
37
    ];
38
39
    /**
40
     * @var callable
41
     */
42
    protected $callback;
43
44
    /**
45
     * @var Container
46
     */
47
    protected $input;
48
49
    /**
50
     * Construct the Callback validator.
51
     *
52
     * @param callable $callback
53
     */
54 5
    public function __construct(callable $callback)
55
    {
56 5
        $this->callback = $callback;
57 5
    }
58
59
    /**
60
     * Validates $value by calling the callback.
61
     *
62
     * @param mixed $value
63
     * @return bool
64
     */
65 4
    public function validate($value)
66
    {
67
        try {
68 4
            $result = call_user_func($this->callback, $value, $this->values);
69
70 3
            if ($result === true) {
71 2
                return true;
72
            }
73 1
            return $this->error(self::INVALID_VALUE);
74 1
        } catch (InvalidValueException $exception) {
75 1
            $reason = $exception->getIdentifier();
76 1
            $this->messageTemplates[$reason] = $exception->getMessage();
77
78 1
            return $this->error($reason);
79
        }
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 3
    protected function getMessageParameters()
86
    {
87 3
        return array_merge(parent::getMessageParameters(), [
88 3
            'callback' => $this->getCallbackAsString($this->callback),
89 3
        ]);
90
    }
91
92
    /**
93
     * Validates the value according to this rule, and returns the result as a bool.
94
     *
95
     * @param string $key
96
     * @param Container $input
97
     * @return bool
98
     */
99 4
    public function isValid($key, Container $input)
100
    {
101 4
        $this->values = $input->getArrayCopy();
102
103 4
        return parent::isValid($key, $input);
104
    }
105
}
106