1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Callbacks\MultipleValuesValidatorCallback |
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/impor |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Callbacks; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A callback implementation that validates the a list of values. |
25
|
|
|
* |
26
|
|
|
* @author Tim Wagner <[email protected]> |
27
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
29
|
|
|
* @link https://github.com/techdivision/import |
30
|
|
|
* @link http://www.techdivision.com |
31
|
|
|
*/ |
32
|
|
|
class MultipleValuesValidatorCallback extends ArrayValidatorCallback |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Will be invoked by the observer it has been registered for. |
37
|
|
|
* |
38
|
|
|
* @param string|null $attributeCode The code of the attribute that has to be validated |
39
|
|
|
* @param string|null $attributeValue The attribute value to be validated |
40
|
|
|
* |
41
|
|
|
* @return mixed The modified value |
42
|
|
|
*/ |
43
|
|
|
public function handle($attributeCode = null, $attributeValue = null) |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
// explode the values and query whether or not an empty value is allowed |
47
|
|
|
if ($this->isNullable($values = $this->getSubject()->explode($attributeValue))) { |
|
|
|
|
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// load the validations for the column |
52
|
|
|
$validations = $this->getValidations(); |
53
|
|
|
|
54
|
|
|
// iterate over the values and validate them |
55
|
|
|
foreach ($values as $value) { |
56
|
|
|
// query whether or not the value is valid |
57
|
|
|
if (in_array($value, $validations)) { |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// throw an exception if the value is NOT in the array |
62
|
|
|
throw new \InvalidArgumentException( |
63
|
|
|
sprintf('Found invalid value "%s" for column "%s"', $value, $attributeCode) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: