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
|
|
|
* The flag to query whether or not the value has to be validated on the main row only. |
46
|
|
|
* |
47
|
|
|
* @var boolean |
48
|
|
|
*/ |
49
|
|
|
protected $mainRowOnly = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initializes the callback with the loader instance. |
53
|
|
|
* |
54
|
|
|
* @param \TechDivision\Import\Loaders\LoaderInterface $loader The loader instance to load the validations with |
55
|
|
|
* @param boolean $nullable The flag to decide whether or not the value can be empty |
56
|
|
|
* @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only |
57
|
|
|
*/ |
58
|
|
|
public function __construct(LoaderInterface $loader, $nullable = false, $mainRowOnly = false) |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
// pass the loader to the parent instance |
62
|
|
|
parent::__construct($loader); |
63
|
|
|
|
64
|
|
|
// initialize the flags with the passed values |
65
|
|
|
$this->nullable = $nullable; |
66
|
|
|
$this->mainRowOnly = $mainRowOnly; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Will be invoked by a observer it has been registered for. |
71
|
|
|
* |
72
|
|
|
* @param string|null $attributeCode The code of the attribute that has to be validated |
73
|
|
|
* @param string|null $attributeValue The attribute value to be validated |
74
|
|
|
* |
75
|
|
|
* @return mixed The modified value |
76
|
|
|
*/ |
77
|
|
|
public function handle($attributeCode = null, $attributeValue = null) |
78
|
|
|
{ |
79
|
|
|
|
80
|
|
|
// the validations for the attribute with the given code |
81
|
|
|
$validations = $this->getValidations($attributeCode); |
82
|
|
|
|
83
|
|
|
// if the passed value is in the array, return immediately |
84
|
|
|
if (in_array($attributeValue, $validations) || $this->isNullable($attributeValue)) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// throw an exception if NO allowed values have been configured |
89
|
|
|
if (sizeof($validations) === 0) { |
90
|
|
|
throw new \InvalidArgumentException( |
91
|
|
|
sprintf('Missing configuration value for custom validation of attribute "%s"', $attributeCode) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// throw an exception if the value is NOT in the array |
96
|
|
|
throw new \InvalidArgumentException( |
97
|
|
|
sprintf( |
98
|
|
|
'Found invalid value "%s" for column "%s" (must be one of: "%s")', |
99
|
|
|
$attributeValue, |
100
|
|
|
$attributeCode, |
101
|
|
|
implode(', ', $validations) |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Query whether or not the passed value IS empty and empty values are allowed. |
108
|
|
|
* |
109
|
|
|
* @param string $attributeValue The attribute value to query for |
110
|
|
|
* |
111
|
|
|
* @return boolean TRUE if empty values are allowed and the passed value IS empty |
112
|
|
|
*/ |
113
|
|
|
protected function isNullable($attributeValue) |
114
|
|
|
{ |
115
|
|
|
|
116
|
|
|
// query whether or not the passed value IS empty |
117
|
|
|
if ($attributeValue === '' || $attributeValue === null) { |
118
|
|
|
// z1: value can NEVER be empty |
119
|
|
|
if ($this->nullable === false && $this->mainRowOnly === false) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// z3: value can ALWAYS be empty |
124
|
|
|
if ($this->nullable === true && $this->mainRowOnly === false) { |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// z2: value MUST NOT be empty in the main row |
129
|
|
View Code Duplication |
if ($this->nullable === false && $this->mainRowOnly === true) { |
|
|
|
|
130
|
|
|
return $this->isMainRow() ? false : true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// z4: value can ONLY be empty in the main row |
134
|
|
View Code Duplication |
if ($this->nullable === true && $this->mainRowOnly === true) { |
|
|
|
|
135
|
|
|
return $this->isMainRow() ? true : false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// if not, return TRUE immediately |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.