1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Callbacks\AbstractBooleanCallback |
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 2016 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\Utils\RegistryKeys; |
24
|
|
|
use TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A callback implementation that converts the passed boolean value. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link https://github.com/techdivision/import |
33
|
|
|
* @link http://www.techdivision.com |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractBooleanCallback extends AbstractCallback |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Array with the string => boolean mapping. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $booleanValues = array( |
44
|
|
|
'true' => 1, |
45
|
|
|
'yes' => 1, |
46
|
|
|
'1' => 1, |
47
|
|
|
'false' => 0, |
48
|
|
|
'no' => 0, |
49
|
|
|
'0' => 0 |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Will be invoked by a observer it has been registered for. |
54
|
|
|
* |
55
|
|
|
* @param \TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface|null $observer The observer |
56
|
|
|
* |
57
|
|
|
* @return mixed The modified value |
58
|
|
|
*/ |
59
|
|
|
public function handle(AttributeCodeAndValueAwareObserverInterface $observer = null) |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
// set the observer |
63
|
|
|
$this->setObserver($observer); |
|
|
|
|
64
|
|
|
|
65
|
|
|
// load the attribute code and value |
66
|
|
|
$attributeCode = $observer->getAttributeCode(); |
|
|
|
|
67
|
|
|
$attributeValue = $observer->getAttributeValue(); |
68
|
|
|
|
69
|
|
|
// return NULL, if the value is empty |
70
|
|
|
if ($attributeValue === null || $attributeValue === '') { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// query whether or not, the passed value can be mapped to a boolean representation |
75
|
|
|
if (isset($this->booleanValues[strtolower($attributeValue)])) { |
76
|
|
|
return (boolean) $this->booleanValues[strtolower($attributeValue)]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// query whether or not we're in debug mode |
80
|
|
View Code Duplication |
if ($this->isDebugMode()) { |
|
|
|
|
81
|
|
|
// log a warning and continue with the next value |
82
|
|
|
$this->getSystemLogger()->warning( |
83
|
|
|
$this->appendExceptionSuffix( |
84
|
|
|
sprintf( |
85
|
|
|
'Can\'t map option value "%s" for attribute %s to a boolean representation', |
86
|
|
|
$attributeValue, |
87
|
|
|
$attributeCode |
88
|
|
|
) |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
// add the missing option value to the registry |
93
|
|
|
$this->mergeAttributesRecursive( |
94
|
|
|
array( |
95
|
|
|
RegistryKeys::MISSING_OPTION_VALUES => array( |
96
|
|
|
$attributeCode => array( |
97
|
|
|
$attributeValue => array( |
98
|
|
|
$this->raiseCounter($attributeValue), |
99
|
|
|
array($this->getUniqueIdentifier() => true) |
100
|
|
|
) |
101
|
|
|
) |
102
|
|
|
) |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
// return NULL, if NO value can be mapped to a boolean representation |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// throw an exception if the attribute is not available |
111
|
|
|
throw new \Exception( |
112
|
|
|
$this->appendExceptionSuffix( |
113
|
|
|
sprintf( |
114
|
|
|
'Can\'t map option value "%s" for attribute "%s" to a boolean representation', |
115
|
|
|
$attributeValue, |
116
|
|
|
$attributeCode |
117
|
|
|
) |
118
|
|
|
) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):