-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathAbstractBooleanCallback.php
More file actions
115 lines (101 loc) · 3.64 KB
/
Copy pathAbstractBooleanCallback.php
File metadata and controls
115 lines (101 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* TechDivision\Import\Callbacks\AbstractBooleanCallback
*
* PHP version 7
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
namespace TechDivision\Import\Callbacks;
use TechDivision\Import\Utils\RegistryKeys;
use TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface;
/**
* A callback implementation that converts the passed boolean value.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
abstract class AbstractBooleanCallback extends AbstractCallback
{
/**
* Array with the string => boolean mapping.
*
* @var array
*/
protected $booleanValues = array(
'true' => 1,
'yes' => 1,
'1' => 1,
'false' => 0,
'no' => 0,
'0' => 0
);
/**
* Will be invoked by a observer it has been registered for.
*
* @param \TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface|null $observer The observer
*
* @return mixed The modified value
*/
public function handle(?AttributeCodeAndValueAwareObserverInterface $observer = null)
{
// set the observer
$this->setObserver($observer);
// load the attribute code and value
$attributeCode = $observer->getAttributeCode();
$attributeValue = $observer->getAttributeValue();
// return NULL, if the value is empty
if ($attributeValue === null || $attributeValue === '') {
return;
}
// query whether or not, the passed value can be mapped to a boolean representation
if (isset($this->booleanValues[strtolower($attributeValue)])) {
return (boolean)$this->booleanValues[strtolower($attributeValue)];
}
// query whether or not we're in debug mode
if ($this->isDebugMode()) {
// log a warning and continue with the next value
$this->getSystemLogger()->warning(
$this->appendExceptionSuffix(
sprintf(
'Can\'t map option value "%s" for attribute %s to a boolean representation',
$attributeValue,
$attributeCode
)
)
);
// add the missing option value to the registry
$this->mergeAttributesRecursive(
array(
RegistryKeys::MISSING_OPTION_VALUES => array(
$attributeCode => array(
$attributeValue => array(
$this->raiseCounter($attributeValue),
array($this->getUniqueIdentifier() => true)
)
)
)
)
);
// return NULL, if NO value can be mapped to a boolean representation
return;
}
// throw an exception if the attribute is not available
throw new \Exception(
$this->appendExceptionSuffix(
sprintf(
'Can\'t map option value "%s" for attribute "%s" to a boolean representation',
$attributeValue,
$attributeCode
)
)
);
}
}