1: <?php
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: class Mage_Api_Helper_Data extends Mage_Core_Helper_Abstract
35: {
36: const XML_PATH_API_WSI = 'api/config/compliance_wsi';
37:
38: 39: 40: 41: 42:
43: public function getV2AdapterCode()
44: {
45: return $this->isComplianceWSI() ? 'soap_wsi' : 'soap_v2';
46: }
47:
48: 49: 50:
51: public function isComplianceWSI()
52: {
53: return Mage::getStoreConfig(self::XML_PATH_API_WSI);
54: }
55:
56: 57: 58: 59: 60: 61:
62: public function wsiArrayUnpacker(&$obj)
63: {
64: if (is_object($obj)) {
65:
66: $modifiedKeys = $this->clearWsiFootprints($obj);
67:
68: foreach ($obj as $key => $value) {
69: if (is_object($value)) {
70: $this->wsiArrayUnpacker($value);
71: }
72: if (is_array($value)) {
73: foreach ($value as &$val) {
74: if (is_object($val)) {
75: $this->wsiArrayUnpacker($val);
76: }
77: }
78: }
79: }
80:
81: foreach ($modifiedKeys as $arrKey) {
82: $this->associativeArrayUnpack($obj->$arrKey);
83: }
84: }
85: }
86:
87: 88: 89: 90: 91: 92:
93: public function v2AssociativeArrayUnpacker(&$obj)
94: {
95: if (is_object($obj)
96: && property_exists($obj, 'key')
97: && property_exists($obj, 'value')
98: ) {
99: if (count(array_keys(get_object_vars($obj))) == 2) {
100: $obj = array($obj->key => $obj->value);
101: return true;
102: }
103: } elseif (is_array($obj)) {
104: $arr = array();
105: $needReplacement = true;
106: foreach ($obj as $key => &$value) {
107: $isAssoc = $this->v2AssociativeArrayUnpacker($value);
108: if ($isAssoc) {
109: foreach ($value as $aKey => $aVal) {
110: $arr[$aKey] = $aVal;
111: }
112: } else {
113: $needReplacement = false;
114: }
115: }
116: if ($needReplacement) {
117: $obj = $arr;
118: }
119: } elseif (is_object($obj)) {
120: $objectKeys = array_keys(get_object_vars($obj));
121:
122: foreach ($objectKeys as $key) {
123: $this->v2AssociativeArrayUnpacker($obj->$key);
124: }
125: }
126: return false;
127: }
128:
129: 130: 131: 132: 133:
134: public function associativeArrayUnpack(&$mixed)
135: {
136: if (is_array($mixed)) {
137: $tmpArr = array();
138: foreach ($mixed as $key => $value) {
139: if (is_object($value)) {
140: $value = get_object_vars($value);
141: if (count($value) == 2 && isset($value['key']) && isset($value['value'])) {
142: $tmpArr[$value['key']] = $value['value'];
143: }
144: }
145: }
146: if (count($tmpArr)) {
147: $mixed = $tmpArr;
148: }
149: }
150:
151: if (is_object($mixed)) {
152: $numOfVals = count(get_object_vars($mixed));
153: if ($numOfVals == 2 && isset($mixed->key) && isset($mixed->value)) {
154: $mixed = get_object_vars($mixed);
155: 156: 157: 158:
159: $mixed = array($mixed['key'] => $mixed['value']);
160: }
161: }
162: }
163:
164: 165: 166: 167: 168: 169:
170: public function (&$obj)
171: {
172: $modifiedKeys = array();
173:
174: $objectKeys = array_keys(get_object_vars($obj));
175:
176: foreach ($objectKeys as $key) {
177: if (is_object($obj->$key) && isset($obj->$key->complexObjectArray)) {
178: if (is_array($obj->$key->complexObjectArray)) {
179: $obj->$key = $obj->$key->complexObjectArray;
180: } else {
181: $obj->$key = array($obj->$key->complexObjectArray);
182: }
183: $modifiedKeys[] = $key;
184: }
185: }
186: return $modifiedKeys;
187: }
188:
189: 190: 191: 192: 193: 194:
195: public function wsiArrayPacker($mixed)
196: {
197: if (is_array($mixed)) {
198: $arrKeys = array_keys($mixed);
199: $isDigit = false;
200: $isString = false;
201: foreach ($arrKeys as $key) {
202: if (is_int($key)) {
203: $isDigit = true;
204: break;
205: }
206: }
207: if ($isDigit) {
208: $mixed = $this->packArrayToObjec($mixed);
209: } else {
210: $mixed = (object) $mixed;
211: }
212: }
213: if (is_object($mixed) && isset($mixed->complexObjectArray)) {
214: foreach ($mixed->complexObjectArray as $k => $v) {
215: $mixed->complexObjectArray[$k] = $this->wsiArrayPacker($v);
216: }
217: }
218: return $mixed;
219: }
220:
221: 222: 223: 224: 225: 226:
227: public function packArrayToObjec(Array $arr)
228: {
229: $obj = new stdClass();
230: $obj->complexObjectArray = $arr;
231: return $obj;
232: }
233:
234: 235: 236: 237: 238: 239:
240: public function toArray(&$data)
241: {
242: if (is_object($data)) {
243: $data = get_object_vars($data);
244: }
245: if (is_array($data)) {
246: foreach ($data as &$value) {
247: if (is_array($value) or is_object($value)) {
248: $this->toArray($value);
249: }
250: }
251: }
252: }
253:
254: 255: 256: 257: 258: 259: 260:
261: public function parseFilters($filters, $fieldsMap = null)
262: {
263:
264: if (is_object($filters)) {
265: $parsedFilters = array();
266:
267: if (isset($filters->filter) && is_array($filters->filter)) {
268: foreach ($filters->filter as $field => $value) {
269: if (is_object($value) && isset($value->key) && isset($value->value)) {
270: $parsedFilters[$value->key] = $value->value;
271: } else {
272: $parsedFilters[$field] = $value;
273: }
274: }
275: }
276:
277: if (isset($filters->complex_filter) && is_array($filters->complex_filter)) {
278: if ($this->isComplianceWSI()) {
279:
280: foreach ($filters->complex_filter as $fieldName => $condition) {
281: if (is_object($condition) && isset($condition->key) && isset($condition->value)) {
282: $conditionName = $condition->key;
283: $conditionValue = $condition->value;
284: $this->formatFilterConditionValue($conditionName, $conditionValue);
285: $parsedFilters[$fieldName] = array($conditionName => $conditionValue);
286: }
287: }
288: } else {
289:
290: foreach ($filters->complex_filter as $value) {
291: if (is_object($value) && isset($value->key) && isset($value->value)) {
292: $fieldName = $value->key;
293: $condition = $value->value;
294: if (is_object($condition) && isset($condition->key) && isset($condition->value)) {
295: $this->formatFilterConditionValue($condition->key, $condition->value);
296: $parsedFilters[$fieldName] = array($condition->key => $condition->value);
297: }
298: }
299: }
300: }
301: }
302: $filters = $parsedFilters;
303: }
304:
305: if (!is_array($filters)) {
306: $filters = array();
307: }
308:
309: if (isset($fieldsMap) && is_array($fieldsMap)) {
310: foreach ($filters as $field => $value) {
311: if (isset($fieldsMap[$field])) {
312: unset($filters[$field]);
313: $field = $fieldsMap[$field];
314: $filters[$field] = $value;
315: }
316: }
317: }
318: return $filters;
319: }
320:
321: 322: 323: 324: 325: 326: 327: 328:
329: public function formatFilterConditionValue($conditionOperator, &$conditionValue)
330: {
331: if (is_string($conditionOperator) && in_array($conditionOperator, array('in', 'nin', 'finset'))
332: && is_string($conditionValue)
333: ) {
334: $delimiter = ',';
335: $conditionValue = explode($delimiter, $conditionValue);
336: }
337: }
338: }
339: