-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Expand file tree
/
Copy pathAbstractSource.php
More file actions
185 lines (171 loc) · 4.19 KB
/
AbstractSource.php
File metadata and controls
185 lines (171 loc) · 4.19 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Copyright 2013 Adobe
* All Rights Reserved.
*/
namespace Magento\Eav\Model\Entity\Attribute\Source;
/**
* Entity/Attribute/Model - attribute selection source abstract
* phpcs:disable Magento2.Classes.AbstractApi
* @api
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @since 100.0.2
*/
abstract class AbstractSource implements
\Magento\Eav\Model\Entity\Attribute\Source\SourceInterface,
\Magento\Framework\Option\ArrayInterface
{
/**
* Reference to the attribute instance
*
* @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
*/
protected $_attribute;
/**
* Options array
*
* @var array
*/
protected $_options = null;
/**
* Set attribute instance
*
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @return \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
* @codeCoverageIgnore
*/
public function setAttribute($attribute)
{
$this->_attribute = $attribute;
return $this;
}
/**
* Get attribute instance
*
* @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
* @codeCoverageIgnore
*/
public function getAttribute()
{
return $this->_attribute;
}
/**
* Get a text for option value
*
* @param string|int $value
* @return string|bool
*/
public function getOptionText($value)
{
$options = $this->getAllOptions();
// Fixed for tax_class_id and custom_design
if (count($options) > 0) {
foreach ($options as $option) {
if (isset($option['value']) && $option['value'] == $value) {
return $option['label'] ?? $option['value'];
}
}
}
// End
if (is_scalar($value) && isset($options[$value])) {
return $options[$value];
}
return false;
}
/**
* Get option id.
*
* @param string $value
* @return null|string
*/
public function getOptionId($value)
{
foreach ($this->getAllOptions() as $option) {
if ($this->mbStrcasecmp($option['label'], $value) == 0 || $option['value'] == $value) {
return $option['value'];
}
}
return null;
}
/**
* Add Value Sort To Collection Select
*
* @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
* @param string $dir direction
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @codeCoverageIgnore
*/
public function addValueSortToCollection($collection, $dir = \Magento\Framework\Data\Collection::SORT_ORDER_DESC)
{
return $this;
}
/**
* Retrieve flat column definition
*
* @codeCoverageIgnore
* @return array
*/
public function getFlatColumns()
{
return [];
}
/**
* Retrieve Indexes(s) for Flat
*
* @return array
* @codeCoverageIgnore
*/
public function getFlatIndexes()
{
return [];
}
/**
* Retrieve Select For Flat Attribute update
*
* @param int $store
* @return \Magento\Framework\DB\Select|null
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @codeCoverageIgnore
*/
public function getFlatUpdateSelect($store)
{
return null;
}
/**
* Get a text for index option value
*
* @param string|int $value
* @return string|bool
* @codeCoverageIgnore
*/
public function getIndexOptionText($value)
{
return $this->getOptionText($value);
}
/**
* Get options as array
*
* @return array
* @codeCoverageIgnore
*/
public function toOptionArray()
{
return $this->getAllOptions();
}
/**
* Multibyte support strcasecmp function version.
*
* @param string $str1
* @param string $str2
* @return int
*/
private function mbStrcasecmp($str1, $str2)
{
$encoding = mb_internal_encoding();
return strcmp(
mb_strtoupper($str1, $encoding),
mb_strtoupper($str2, $encoding)
);
}
}