1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kiboko\Component\Connector\Discoverer; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Pim\Component\Catalog\Model\AttributeInterface; |
7
|
|
|
use Pim\Component\Catalog\Model\AttributeOptionInterface; |
8
|
|
|
use Pim\Component\Catalog\Model\AttributeOptionValueInterface; |
9
|
|
|
use Pim\Component\Catalog\Repository\AttributeOptionRepositoryInterface; |
10
|
|
|
use Pim\Component\Catalog\Repository\AttributeRepositoryInterface; |
11
|
|
|
use Symfony\Component\Config\Loader\FileLoader; |
12
|
|
|
|
13
|
|
|
class AttributeOptionDiscoverer |
14
|
|
|
implements OptionDiscovererInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var AttributeRepositoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $attributeRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var AttributeOptionRepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $attributeOptionRepository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var AttributeInterface |
28
|
|
|
*/ |
29
|
|
|
private $attribute; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AttributeOptionInterface[] |
33
|
|
|
*/ |
34
|
|
|
private $attributeOptions; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $mapping; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param AttributeRepositoryInterface $attributeRepository |
43
|
|
|
* @param AttributeOptionRepositoryInterface $attributeOptionRepository |
44
|
|
|
* @param string $attributeCode |
45
|
|
|
* @param FileLoader $loader |
46
|
|
|
* @param string $mappingFile |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
AttributeRepositoryInterface $attributeRepository, |
50
|
|
|
AttributeOptionRepositoryInterface $attributeOptionRepository, |
51
|
|
|
$attributeCode, |
52
|
|
|
FileLoader $loader = null, |
53
|
|
|
$mappingFile = null |
54
|
|
|
) { |
55
|
|
|
$this->attributeRepository = $attributeRepository; |
56
|
|
|
$this->attributeOptionRepository = $attributeOptionRepository; |
57
|
|
|
$this->mapping = []; |
58
|
|
|
|
59
|
|
|
$this->attribute = $this->attributeRepository->findOneByIdentifier($attributeCode); |
60
|
|
|
if ($this->attribute) { |
61
|
|
|
$this->attributeOptions = $this->attribute->getOptions(); |
62
|
|
|
} else { |
63
|
|
|
$this->attributeOptions = new ArrayCollection(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($mappingFile !== null && $loader !== null) { |
67
|
|
|
foreach ($loader->load($mappingFile) as $rawValue => $optionCode) { |
68
|
|
|
$this->mapTo($rawValue, $optionCode); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $rawValue |
75
|
|
|
* @param string $cleanValue |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function mapTo($rawValue, $cleanValue) |
79
|
|
|
{ |
80
|
|
|
$this->mapping[$rawValue] = $cleanValue; |
81
|
|
|
|
82
|
|
|
return $this->mapping; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $value |
87
|
|
|
* @param string $locale |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function find($value, $locale = null) |
91
|
|
|
{ |
92
|
|
|
if (isset($this->mapping[$value])) { |
93
|
|
|
$value = $this->mapping[$value]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @var AttributeOptionInterface $option */ |
97
|
|
|
foreach ($this->attributeOptions as $option) { |
98
|
|
|
if ($locale !== null) { |
99
|
|
|
$option->setLocale($locale); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($option->getOptionValue() instanceof AttributeOptionValueInterface && |
103
|
|
|
$option->getOptionValue()->getLabel() === $value |
104
|
|
|
) { |
105
|
|
|
return $option->getCode(); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.