Completed
Push — 15.x ( 84e4c2...66410f )
by Tim
02:43
created

ArrayValidatorCallback::handle()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
ccs 0
cts 20
cp 0
rs 9.472
cc 4
nc 3
nop 2
crap 20
1
<?php
2
3
/**
4
 * TechDivision\Import\Callbacks\ArrayValidatorCallback
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Callbacks;
22
23
use TechDivision\Import\Loaders\LoaderInterface;
24
25
/**
26
 * Array validator callback implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class ArrayValidatorCallback extends AbstractValidatorCallback
35
{
36
37
    /**
38
     * The flag to query whether or not the value can be empty.
39
     *
40
     * @var boolean
41
     */
42
    protected $nullable = false;
43
44
    /**
45
     * Initializes the callback with the loader instance.
46
     *
47
     * @param \TechDivision\Import\Loaders\LoaderInterface $loader   The loader instance to load the validations with
48
     * @param boolean                                      $nullable The flag to decide whether or not the value can be empty
49
     */
50
    public function __construct(LoaderInterface $loader, $nullable = false)
51
    {
52
53
        // pass the loader to the parent instance
54
        parent::__construct($loader);
55
56
        // initialize the flag with the passed value
57
        $this->nullable = $nullable;
58
    }
59
60
    /**
61
     * Will be invoked by a observer it has been registered for.
62
     *
63
     * @param string|null $attributeCode  The code of the attribute that has to be validated
64
     * @param string|null $attributeValue The attribute value to be validated
65
     *
66
     * @return mixed The modified value
67
     */
68
    public function handle($attributeCode = null, $attributeValue = null)
69
    {
70
71
        // the validations for the attribute with the given code
72
        $validations = $this->getValidations($attributeCode);
73
74
        // if the passed value is in the array, return immediately
75
        if (in_array($attributeValue, $validations) || $this->isNullable($attributeValue)) {
76
            return;
77
        }
78
79
        // throw an exception if NO allowed values have been configured
80
        if (sizeof($validations) === 0) {
81
            throw new \InvalidArgumentException(
82
                sprintf('Missing configuration value for custom validation of attribute "%s"', $attributeCode)
83
            );
84
        }
85
86
        // throw an exception if the value is NOT in the array
87
        throw new \InvalidArgumentException(
88
            sprintf(
89
                'Found invalid value "%s" for column "%s" (must be one of: "%s")',
90
                $attributeValue,
91
                $attributeCode,
92
                implode(', ', $validations)
93
            )
94
        );
95
    }
96
97
    /**
98
     * Query whether or not the passed value IS empty and empty values are allowed.
99
     *
100
     * @param string $attributeValue The attribute value to query for
101
     *
102
     * @return boolean TRUE if empty values are allowed and the passed value IS empty
103
     */
104
    protected function isNullable($attributeValue)
105
    {
106
        return $this->nullable && ($attributeValue === '' || $attributeValue === null);
107
    }
108
}
109