PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Api/Helper/Data.php

https://bitbucket.org/kdms/sh-magento
PHP | 338 lines | 213 code | 19 blank | 106 comment | 64 complexity | 0544d7175056d7848a522d57b0430ea1 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Api
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Web service api main helper
  28. *
  29. * @category Mage
  30. * @package Mage_Api
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Api_Helper_Data extends Mage_Core_Helper_Abstract
  34. {
  35. const XML_PATH_API_WSI = 'api/config/compliance_wsi';
  36. /**
  37. * Method to find adapter code depending on WS-I compatibility setting
  38. *
  39. * @return string
  40. */
  41. public function getV2AdapterCode()
  42. {
  43. return $this->isComplianceWSI() ? 'soap_wsi' : 'soap_v2';
  44. }
  45. /**
  46. * @return boolean
  47. */
  48. public function isComplianceWSI()
  49. {
  50. return Mage::getStoreConfig(self::XML_PATH_API_WSI);
  51. }
  52. /**
  53. * Go thru a WSI args array and turns it to correct state.
  54. *
  55. * @param Object $obj - Link to Object
  56. * @return Object
  57. */
  58. public function wsiArrayUnpacker(&$obj)
  59. {
  60. if (is_object($obj)) {
  61. $modifiedKeys = $this->clearWsiFootprints($obj);
  62. foreach ($obj as $key => $value) {
  63. if (is_object($value)) {
  64. $this->wsiArrayUnpacker($value);
  65. }
  66. if (is_array($value)) {
  67. foreach ($value as &$val) {
  68. if (is_object($val)) {
  69. $this->wsiArrayUnpacker($val);
  70. }
  71. }
  72. }
  73. }
  74. foreach ($modifiedKeys as $arrKey) {
  75. $this->associativeArrayUnpack($obj->$arrKey);
  76. }
  77. }
  78. }
  79. /**
  80. * Go thru an object parameters and unpak associative object to array.
  81. *
  82. * @param Object $obj - Link to Object
  83. * @return Object
  84. */
  85. public function v2AssociativeArrayUnpacker(&$obj)
  86. {
  87. if (is_object($obj)
  88. && property_exists($obj, 'key')
  89. && property_exists($obj, 'value')
  90. ) {
  91. if (count(array_keys(get_object_vars($obj))) == 2) {
  92. $obj = array($obj->key => $obj->value);
  93. return true;
  94. }
  95. } elseif (is_array($obj)) {
  96. $arr = array();
  97. $needReplacement = true;
  98. foreach ($obj as $key => &$value) {
  99. $isAssoc = $this->v2AssociativeArrayUnpacker($value);
  100. if ($isAssoc) {
  101. foreach ($value as $aKey => $aVal) {
  102. $arr[$aKey] = $aVal;
  103. }
  104. } else {
  105. $needReplacement = false;
  106. }
  107. }
  108. if ($needReplacement) {
  109. $obj = $arr;
  110. }
  111. } elseif (is_object($obj)) {
  112. $objectKeys = array_keys(get_object_vars($obj));
  113. foreach ($objectKeys as $key) {
  114. $this->v2AssociativeArrayUnpacker($obj->$key);
  115. }
  116. }
  117. return false;
  118. }
  119. /**
  120. * Go thru mixed and turns it to a correct look.
  121. *
  122. * @param Mixed $mixed A link to variable that may contain associative array.
  123. */
  124. public function associativeArrayUnpack(&$mixed)
  125. {
  126. if (is_array($mixed)) {
  127. $tmpArr = array();
  128. foreach ($mixed as $key => $value) {
  129. if (is_object($value)) {
  130. $value = get_object_vars($value);
  131. if (count($value) == 2 && isset($value['key']) && isset($value['value'])) {
  132. $tmpArr[$value['key']] = $value['value'];
  133. }
  134. }
  135. }
  136. if (count($tmpArr)) {
  137. $mixed = $tmpArr;
  138. }
  139. }
  140. if (is_object($mixed)) {
  141. $numOfVals = count(get_object_vars($mixed));
  142. if ($numOfVals == 2 && isset($mixed->key) && isset($mixed->value)) {
  143. $mixed = get_object_vars($mixed);
  144. /*
  145. * Processing an associative arrays.
  146. * $mixed->key = '2'; $mixed->value = '3'; turns to array(2 => '3');
  147. */
  148. $mixed = array($mixed['key'] => $mixed['value']);
  149. }
  150. }
  151. }
  152. /**
  153. * Corrects data representation.
  154. *
  155. * @param Object $obj - Link to Object
  156. * @return Object
  157. */
  158. public function clearWsiFootprints(&$obj)
  159. {
  160. $modifiedKeys = array();
  161. $objectKeys = array_keys(get_object_vars($obj));
  162. foreach ($objectKeys as $key) {
  163. if (is_object($obj->$key) && isset($obj->$key->complexObjectArray)) {
  164. if (is_array($obj->$key->complexObjectArray)) {
  165. $obj->$key = $obj->$key->complexObjectArray;
  166. } else { // for one element array
  167. $obj->$key = array($obj->$key->complexObjectArray);
  168. }
  169. $modifiedKeys[] = $key;
  170. }
  171. }
  172. return $modifiedKeys;
  173. }
  174. /**
  175. * For the WSI, generates an response object.
  176. *
  177. * @param mixed $mixed - Link to Object
  178. * @return mixed
  179. */
  180. public function wsiArrayPacker($mixed)
  181. {
  182. if (is_array($mixed)) {
  183. $arrKeys = array_keys($mixed);
  184. $isDigit = false;
  185. $isString = false;
  186. foreach ($arrKeys as $key) {
  187. if (is_int($key)) {
  188. $isDigit = true;
  189. break;
  190. }
  191. }
  192. if ($isDigit) {
  193. $mixed = $this->packArrayToObjec($mixed);
  194. } else {
  195. $mixed = (object) $mixed;
  196. }
  197. }
  198. if (is_object($mixed) && isset($mixed->complexObjectArray)) {
  199. foreach ($mixed->complexObjectArray as $k => $v) {
  200. $mixed->complexObjectArray[$k] = $this->wsiArrayPacker($v);
  201. }
  202. }
  203. return $mixed;
  204. }
  205. /**
  206. * For response to the WSI, generates an object from array.
  207. *
  208. * @param Array $arr - Link to Object
  209. * @return Object
  210. */
  211. public function packArrayToObjec(Array $arr)
  212. {
  213. $obj = new stdClass();
  214. $obj->complexObjectArray = $arr;
  215. return $obj;
  216. }
  217. /**
  218. * Convert objects and arrays to array recursively
  219. *
  220. * @param array|object $data
  221. * @return void
  222. */
  223. public function toArray(&$data)
  224. {
  225. if (is_object($data)) {
  226. $data = get_object_vars($data);
  227. }
  228. if (is_array($data)) {
  229. foreach ($data as &$value) {
  230. if (is_array($value) or is_object($value)) {
  231. $this->toArray($value);
  232. }
  233. }
  234. }
  235. }
  236. /**
  237. * Parse filters and format them to be applicable for collection filtration
  238. *
  239. * @param null|object|array $filters
  240. * @param array $fieldsMap Map of field names in format: array('field_name_in_filter' => 'field_name_in_db')
  241. * @return array
  242. */
  243. public function parseFilters($filters, $fieldsMap = null)
  244. {
  245. // if filters are used in SOAP they must be represented in array format to be used for collection filtration
  246. if (is_object($filters)) {
  247. $parsedFilters = array();
  248. // parse simple filter
  249. if (isset($filters->filter) && is_array($filters->filter)) {
  250. foreach ($filters->filter as $field => $value) {
  251. if (is_object($value) && isset($value->key) && isset($value->value)) {
  252. $parsedFilters[$value->key] = $value->value;
  253. } else {
  254. $parsedFilters[$field] = $value;
  255. }
  256. }
  257. }
  258. // parse complex filter
  259. if (isset($filters->complex_filter) && is_array($filters->complex_filter)) {
  260. if ($this->isComplianceWSI()) {
  261. // WS-I compliance mode
  262. foreach ($filters->complex_filter as $fieldName => $condition) {
  263. if (is_object($condition) && isset($condition->key) && isset($condition->value)) {
  264. $conditionName = $condition->key;
  265. $conditionValue = $condition->value;
  266. $this->formatFilterConditionValue($conditionName, $conditionValue);
  267. $parsedFilters[$fieldName] = array($conditionName => $conditionValue);
  268. }
  269. }
  270. } else {
  271. // non WS-I compliance mode
  272. foreach ($filters->complex_filter as $value) {
  273. if (is_object($value) && isset($value->key) && isset($value->value)) {
  274. $fieldName = $value->key;
  275. $condition = $value->value;
  276. if (is_object($condition) && isset($condition->key) && isset($condition->value)) {
  277. $this->formatFilterConditionValue($condition->key, $condition->value);
  278. $parsedFilters[$fieldName] = array($condition->key => $condition->value);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. $filters = $parsedFilters;
  285. }
  286. // make sure that method result is always array
  287. if (!is_array($filters)) {
  288. $filters = array();
  289. }
  290. // apply fields mapping
  291. if (isset($fieldsMap) && is_array($fieldsMap)) {
  292. foreach ($filters as $field => $value) {
  293. if (isset($fieldsMap[$field])) {
  294. unset($filters[$field]);
  295. $field = $fieldsMap[$field];
  296. $filters[$field] = $value;
  297. }
  298. }
  299. }
  300. return $filters;
  301. }
  302. /**
  303. * Convert condition value from the string into the array
  304. * for the condition operators that require value to be an array.
  305. * Condition value is changed by reference
  306. *
  307. * @param string $conditionOperator
  308. * @param string $conditionValue
  309. */
  310. public function formatFilterConditionValue($conditionOperator, &$conditionValue)
  311. {
  312. if (is_string($conditionOperator) && in_array($conditionOperator, array('in', 'nin', 'finset'))
  313. && is_string($conditionValue)
  314. ) {
  315. $delimiter = ',';
  316. $conditionValue = explode($delimiter, $conditionValue);
  317. }
  318. }
  319. }