Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 34afcdd

Browse files
author
Jens Schulze
committed
feat(Product): add typed getter for product attribute value
1 parent 80c72ec commit 34afcdd

File tree

3 files changed

+360
-6
lines changed

3 files changed

+360
-6
lines changed

src/Core/Model/Common/Attribute.php

Lines changed: 257 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,12 @@ protected function getApiType($attributeName, $value)
8686
}
8787

8888
$apiType = $this->guessApiType($value);
89-
$definition = AttributeDefinition::of($this->getContextCallback());
90-
$definition->setName($attributeName);
91-
$definition->setType(AttributeType::fromArray(['name' => $apiType]));
92-
89+
$elementType = null;
9390
if ($apiType == static::API_SET) {
9491
$elementType = $this->guessApiType(current($value));
95-
$definition->getType()->setElementType(AttributeType::fromArray(['name' => $elementType]));
9692
}
97-
$this->setAttributeDefinition($definition);
93+
$this->setApiType($attributeName, $apiType, $elementType);
94+
9895
return static::$types[$attributeName];
9996
}
10097

@@ -122,6 +119,260 @@ protected function initialize($field)
122119
parent::initialize($field);
123120
}
124121

122+
private function setApiType($attributeName, $valueType, $elementType = null)
123+
{
124+
if (!isset(static::$types[$attributeName])) {
125+
$definition = AttributeDefinition::of($this->getContextCallback());
126+
$definition->setName($attributeName);
127+
$definition->setType(AttributeType::fromArray(['name' => $valueType]));
128+
129+
if ($valueType == static::API_SET && $elementType != null) {
130+
$definition->getType()->setElementType(AttributeType::fromArray(['name' => $elementType]));
131+
}
132+
$this->setAttributeDefinition($definition);
133+
}
134+
}
135+
136+
/**
137+
* @return bool
138+
*/
139+
public function getValueAsBool()
140+
{
141+
$attributeName = $this->getName();
142+
$this->setApiType($attributeName, static::API_BOOL);
143+
return $this->getValue();
144+
}
145+
146+
/**
147+
* @return int|float
148+
*/
149+
public function getValueAsNumber()
150+
{
151+
$attributeName = $this->getName();
152+
$this->setApiType($attributeName, static::API_NUMBER);
153+
return $this->getValue();
154+
}
155+
156+
/**
157+
* @return string
158+
*/
159+
public function getValueAsString()
160+
{
161+
$attributeName = $this->getName();
162+
$this->setApiType($attributeName, static::API_TEXT);
163+
return $this->getValue();
164+
}
165+
166+
/**
167+
* @return LocalizedString
168+
*/
169+
public function getValueAsLocalizedString()
170+
{
171+
$attributeName = $this->getName();
172+
$this->setApiType($attributeName, static::API_LTEXT);
173+
return $this->getValue();
174+
}
175+
176+
/**
177+
* @return LocalizedEnum
178+
*/
179+
public function getValueAsLocalizedEnum()
180+
{
181+
$attributeName = $this->getName();
182+
$this->setApiType($attributeName, static::API_LENUM);
183+
return $this->getValue();
184+
}
185+
186+
/**
187+
* @return Enum
188+
*/
189+
public function getValueAsEnum()
190+
{
191+
$attributeName = $this->getName();
192+
$this->setApiType($attributeName, static::API_ENUM);
193+
return $this->getValue();
194+
}
195+
196+
/**
197+
* @return Money
198+
*/
199+
public function getValueAsMoney()
200+
{
201+
$attributeName = $this->getName();
202+
$this->setApiType($attributeName, static::API_MONEY);
203+
return $this->getValue();
204+
}
205+
206+
/**
207+
* @return DateDecorator
208+
*/
209+
public function getValueAsDate()
210+
{
211+
$attributeName = $this->getName();
212+
$this->setApiType($attributeName, static::API_DATE);
213+
return $this->getValue();
214+
}
215+
216+
/**
217+
* @return TimeDecorator
218+
*/
219+
public function getValueAsTime()
220+
{
221+
$attributeName = $this->getName();
222+
$this->setApiType($attributeName, static::API_TIME);
223+
return $this->getValue();
224+
}
225+
226+
/**
227+
* @return DateTimeDecorator
228+
*/
229+
public function getValueAsDateTime()
230+
{
231+
$attributeName = $this->getName();
232+
$this->setApiType($attributeName, static::API_DATETIME);
233+
return $this->getValue();
234+
}
235+
236+
/**
237+
* @return AttributeCollection
238+
*/
239+
public function getValueAsNested()
240+
{
241+
$attributeName = $this->getName();
242+
$this->setApiType($attributeName, static::API_NESTED);
243+
return $this->getValue();
244+
}
245+
246+
/**
247+
* @return Reference
248+
*/
249+
public function getValueAsReference()
250+
{
251+
$attributeName = $this->getName();
252+
$this->setApiType($attributeName, static::API_REFERENCE);
253+
return $this->getValue();
254+
}
255+
256+
/**
257+
* @return Set
258+
*/
259+
public function getValueAsBoolSet()
260+
{
261+
$attributeName = $this->getName();
262+
$this->setApiType($attributeName, static::API_SET, static::API_BOOL);
263+
return $this->getValue();
264+
}
265+
266+
/**
267+
* @return Set
268+
*/
269+
public function getValueAsNumberSet()
270+
{
271+
$attributeName = $this->getName();
272+
$this->setApiType($attributeName, static::API_SET, static::API_NUMBER);
273+
return $this->getValue();
274+
}
275+
276+
/**
277+
* @return Set
278+
*/
279+
public function getValueAsStringSet()
280+
{
281+
$attributeName = $this->getName();
282+
$this->setApiType($attributeName, static::API_SET, static::API_TEXT);
283+
return $this->getValue();
284+
}
285+
286+
/**
287+
* @return Set
288+
*/
289+
public function getValueAsLocalizedStringSet()
290+
{
291+
$attributeName = $this->getName();
292+
$this->setApiType($attributeName, static::API_SET, static::API_LTEXT);
293+
return $this->getValue();
294+
}
295+
296+
/**
297+
* @return Set
298+
*/
299+
public function getValueAsLocalizedEnumSet()
300+
{
301+
$attributeName = $this->getName();
302+
$this->setApiType($attributeName, static::API_SET, static::API_LENUM);
303+
return $this->getValue();
304+
}
305+
306+
/**
307+
* @return Set
308+
*/
309+
public function getValueAsEnumSet()
310+
{
311+
$attributeName = $this->getName();
312+
$this->setApiType($attributeName, static::API_SET, static::API_ENUM);
313+
return $this->getValue();
314+
}
315+
316+
/**
317+
* @return Set
318+
*/
319+
public function getValueAsMoneySet()
320+
{
321+
$attributeName = $this->getName();
322+
$this->setApiType($attributeName, static::API_SET, static::API_MONEY);
323+
return $this->getValue();
324+
}
325+
326+
/**
327+
* @return Set
328+
*/
329+
public function getValueAsDateSet()
330+
{
331+
$attributeName = $this->getName();
332+
$this->setApiType($attributeName, static::API_SET, static::API_DATE);
333+
return $this->getValue();
334+
}
335+
336+
/**
337+
* @return Set
338+
*/
339+
public function getValueAsTimeSet()
340+
{
341+
$attributeName = $this->getName();
342+
$this->setApiType($attributeName, static::API_SET, static::API_TIME);
343+
return $this->getValue();
344+
}
345+
346+
/**
347+
* @return Set
348+
*/
349+
public function getValueAsDateTimeSet()
350+
{
351+
$attributeName = $this->getName();
352+
$this->setApiType($attributeName, static::API_SET, static::API_DATETIME);
353+
return $this->getValue();
354+
}
355+
356+
/**
357+
* @return Set
358+
*/
359+
public function getValueAsNestedSet()
360+
{
361+
$attributeName = $this->getName();
362+
$this->setApiType($attributeName, static::API_SET, static::API_NESTED);
363+
return $this->getValue();
364+
}
365+
366+
/**
367+
* @return Set
368+
*/
369+
public function getValueAsReferenceSet()
370+
{
371+
$attributeName = $this->getName();
372+
$this->setApiType($attributeName, static::API_SET, static::API_REFERENCE);
373+
return $this->getValue();
374+
}
375+
125376
/**
126377
* @param $value
127378
* @return string

tests/unit/Model/Common/AttributeCollectionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ public function testMagicGet()
5858
$this->assertSame('Test', $collection->test);
5959
}
6060

61+
public function testAttributeAs()
62+
{
63+
$data = [[
64+
'name' => 'collection-enum-set',
65+
'value' => [
66+
['key' => 'myKey', 'label' => 'myLabel']
67+
]
68+
]];
69+
$attributes = AttributeCollection::fromArray($data);
70+
71+
$this->assertInstanceOf(Set::class, $attributes->getByName('collection-enum-set')->getValueAsEnumSet());
72+
$this->assertInstanceOf(Enum::class, $attributes->getByName('collection-enum-set')->getValueAsEnumSet()->getAt(0));
73+
}
74+
75+
6176
public function testMagicGetNotSet()
6277
{
6378
$collection = AttributeCollection::of();

0 commit comments

Comments
 (0)