@@ -21,19 +21,6 @@ class Attribute extends JsonObject
2121{
2222 // identifiers for the Api Product Attribute Types:
2323 const T_UNKNOWN = 'unknown ' ; // zero, should evaluate to false
24- const T_TEXTLIKE = 'string ' ; //includes date, datetime, time as these are JSON Strings, too
25- const T_LTEXT = '\Commercetools\Core\Model\Common\LocalizedString ' ;
26- const T_NUMBER = 'float ' ;
27- const T_BOOLEAN = 'bool ' ;
28- const T_ENUM = '\Commercetools\Core\Model\Common\Enum ' ;
29- const T_LENUM = '\Commercetools\Core\Model\Common\LocalizedEnum ' ;
30- const T_MONEY = '\Commercetools\Core\Model\Common\Money ' ;
31- const T_SET = '\Commercetools\Core\Model\Common\Set ' ;
32- const T_NESTED = '\Commercetools\Core\Model\Common\AttributeCollection ' ;
33- const T_REFERENCE = '\Commercetools\Core\Model\Common\Reference ' ;
34- const T_DATETIME = '\DateTime ' ;
35- const T_DATETIME_DECORATOR = '\Commercetools\Core\Model\Common\DateTimeDecorator ' ;
36-
3724
3825 const PROP_VALUE = "value " ;
3926 const PROP_KEY = "key " ;
@@ -60,24 +47,33 @@ class Attribute extends JsonObject
6047
6148 protected static $ types = [];
6249
63- protected $ attributeDefinition ;
64- protected $ valueType ;
65- protected $ valueElementType ;
66- protected $ valueDecorator ;
67-
6850 public function fieldDefinitions ()
6951 {
7052 return [
7153 static ::PROP_NAME => [self ::TYPE => 'string ' ],
72- static ::PROP_VALUE => [
73- self ::TYPE => $ this ->valueType ,
74- self ::DECORATOR => $ this ->valueDecorator ,
75- self ::ELEMENT_TYPE => $ this ->valueElementType ,
76- self ::OPTIONAL => true
77- ],
54+ static ::PROP_VALUE => [],
7855 ];
7956 }
8057
58+ public function fieldDefinition ($ field )
59+ {
60+ if ($ field == static ::PROP_VALUE ) {
61+ if (isset (static ::$ types [$ this ->getName ()])) {
62+ $ fieldDefinition = static ::$ types [$ this ->getName ()];
63+ if (!$ fieldDefinition instanceof AttributeDefinition) {
64+ return null ;
65+ }
66+ $ fieldType = $ fieldDefinition ->getType ();
67+ if (!$ fieldType instanceof AttributeType) {
68+ return null ;
69+ }
70+ return $ fieldType ->fieldTypeDefinition ();
71+ }
72+ return null ;
73+ }
74+ return parent ::fieldDefinition ($ field );
75+ }
76+
8177 /**
8278 * @param $attributeName
8379 * @param $value
@@ -86,71 +82,29 @@ public function fieldDefinitions()
8682 protected function getApiType ($ attributeName , $ value )
8783 {
8884 if (isset (static ::$ types [$ attributeName ])) {
89- $ this ->setAttributeDefinition (static ::$ types [$ attributeName ]);
9085 return static ::$ types [$ attributeName ];
9186 }
9287
9388 $ apiType = $ this ->guessApiType ($ value );
9489 $ definition = AttributeDefinition::of ($ this ->getContextCallback ());
9590 $ definition ->setName ($ attributeName );
96- $ definition ->setType (AttributeType::of ()-> setName ( $ apiType ));
91+ $ definition ->setType (AttributeType::fromArray ([ ' name ' => $ apiType] ));
9792
9893 if ($ apiType == static ::API_SET ) {
9994 $ elementType = $ this ->guessApiType (current ($ value ));
100- $ definition ->getType ()->setElementType (AttributeType::of ()-> setName ( $ elementType ));
95+ $ definition ->getType ()->setElementType (AttributeType::fromArray ([ ' name ' => $ elementType] ));
10196 }
10297 $ this ->setAttributeDefinition ($ definition );
103- static ::$ types [$ attributeName ] = $ definition ;
104-
10598 return static ::$ types [$ attributeName ];
10699 }
107100
108- public static function getTypeByApiType ($ apiType )
109- {
110- $ typeMapping = [
111- static ::API_BOOL => static ::T_BOOLEAN ,
112- static ::API_NUMBER => static ::T_NUMBER ,
113- static ::API_TEXT => static ::T_TEXTLIKE ,
114- static ::API_LTEXT => static ::T_LTEXT ,
115- static ::API_LENUM => static ::T_LENUM ,
116- static ::API_ENUM => static ::T_ENUM ,
117- static ::API_MONEY => static ::T_MONEY ,
118- static ::API_DATE => static ::T_DATETIME ,
119- static ::API_TIME => static ::T_DATETIME ,
120- static ::API_DATETIME => static ::T_DATETIME ,
121- static ::API_SET => static ::T_SET ,
122- static ::API_NESTED => static ::T_NESTED ,
123- static ::API_REFERENCE => static ::T_REFERENCE
124- ];
125-
126-
127- return isset ($ typeMapping [$ apiType ]) ? $ typeMapping [$ apiType ] : null ;
128- }
129-
130101 /**
131102 * @param AttributeDefinition $definition
132103 * @return $this
133104 */
134105 public function setAttributeDefinition (AttributeDefinition $ definition )
135106 {
136107 static ::$ types [$ definition ->getName ()] = $ definition ;
137- $ this ->attributeDefinition = $ definition ;
138- $ apiType = $ definition ->getType ()->getName ();
139- $ this ->valueType = static ::getTypeByApiType ($ apiType );
140- $ this ->valueElementType = null ;
141- $ this ->valueDecorator = null ;
142-
143- switch ($ apiType ) {
144- case static ::API_SET :
145- $ this ->valueElementType = static ::getTypeByApiType (
146- $ definition ->getType ()->getElementType ()->getName ()
147- );
148- break ;
149- case static ::API_TIME :
150- case static ::API_DATETIME :
151- case static ::API_DATE :
152- $ this ->valueDecorator = static ::T_DATETIME_DECORATOR ;
153- }
154108
155109 return $ this ;
156110 }
0 commit comments