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

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

https://github.com/JackoPlane/magento2
PHP | 359 lines | 229 code | 18 blank | 112 comment | 74 complexity | b9bcd3afee357a8e128520c71b5dceb9 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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) 2013 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  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. /**
  36. * Go thru a WSI args array and turns it to correct state.
  37. *
  38. * @param Object $obj - Link to Object
  39. * @return Object
  40. */
  41. public function wsiArrayUnpacker(&$obj)
  42. {
  43. if (is_object($obj)) {
  44. $modifiedKeys = $this->clearWsiFootprints($obj);
  45. foreach ($obj as $key => $value) {
  46. if (is_object($value)) {
  47. $this->wsiArrayUnpacker($value);
  48. }
  49. if (is_array($value)) {
  50. foreach ($value as &$val) {
  51. if (is_object($val)) {
  52. $this->wsiArrayUnpacker($val);
  53. }
  54. }
  55. }
  56. }
  57. foreach ($modifiedKeys as $arrKey) {
  58. $this->associativeArrayUnpack($obj->$arrKey);
  59. }
  60. }
  61. }
  62. /**
  63. * Go thru an object parameters and unpak associative object to array.
  64. *
  65. * @param Object $obj - Link to Object
  66. * @return Object
  67. */
  68. public function v2AssociativeArrayUnpacker(&$obj)
  69. {
  70. if (is_object($obj)
  71. && property_exists($obj, 'key')
  72. && property_exists($obj, 'value')
  73. ) {
  74. if (count(array_keys(get_object_vars($obj))) == 2) {
  75. $obj = array($obj->key => $obj->value);
  76. return true;
  77. }
  78. } elseif (is_array($obj)) {
  79. $arr = array();
  80. $needReplacement = true;
  81. foreach ($obj as $key => &$value) {
  82. $isAssoc = $this->v2AssociativeArrayUnpacker($value);
  83. if ($isAssoc) {
  84. foreach ($value as $aKey => $aVal) {
  85. $arr[$aKey] = $aVal;
  86. }
  87. } else {
  88. $needReplacement = false;
  89. }
  90. }
  91. if ($needReplacement) {
  92. $obj = $arr;
  93. }
  94. } elseif (is_object($obj)) {
  95. $objectKeys = array_keys(get_object_vars($obj));
  96. foreach ($objectKeys as $key) {
  97. $this->v2AssociativeArrayUnpacker($obj->$key);
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Go thru mixed and turns it to a correct look.
  104. *
  105. * @param Mixed $mixed A link to variable that may contain associative array.
  106. */
  107. public function associativeArrayUnpack(&$mixed)
  108. {
  109. if (is_array($mixed)) {
  110. $tmpArr = array();
  111. foreach ($mixed as $key => $value) {
  112. if (is_object($value)) {
  113. $value = get_object_vars($value);
  114. if (count($value) == 2 && isset($value['key']) && isset($value['value'])) {
  115. $tmpArr[$value['key']] = $value['value'];
  116. }
  117. }
  118. }
  119. if (count($tmpArr)) {
  120. $mixed = $tmpArr;
  121. }
  122. }
  123. if (is_object($mixed)) {
  124. $numOfVals = count(get_object_vars($mixed));
  125. if ($numOfVals == 2 && isset($mixed->key) && isset($mixed->value)) {
  126. $mixed = get_object_vars($mixed);
  127. /*
  128. * Processing an associative arrays.
  129. * $mixed->key = '2'; $mixed->value = '3'; turns to array(2 => '3');
  130. */
  131. $mixed = array($mixed['key'] => $mixed['value']);
  132. }
  133. }
  134. }
  135. /**
  136. * Corrects data representation.
  137. *
  138. * @param Object $obj - Link to Object
  139. * @return Object
  140. */
  141. public function clearWsiFootprints(&$obj)
  142. {
  143. $modifiedKeys = array();
  144. $objectKeys = array_keys(get_object_vars($obj));
  145. foreach ($objectKeys as $key) {
  146. if (is_object($obj->$key) && isset($obj->$key->complexObjectArray)) {
  147. if (is_array($obj->$key->complexObjectArray)) {
  148. $obj->$key = $obj->$key->complexObjectArray;
  149. } else { // for one element array
  150. $obj->$key = array($obj->$key->complexObjectArray);
  151. }
  152. $modifiedKeys[] = $key;
  153. }
  154. }
  155. return $modifiedKeys;
  156. }
  157. /**
  158. * For the WSI, generates an response object.
  159. *
  160. * @param mixed $mixed - Link to Object
  161. * @return mixed
  162. */
  163. public function wsiArrayPacker($mixed)
  164. {
  165. if (is_array($mixed)) {
  166. $arrKeys = array_keys($mixed);
  167. $isDigit = false;
  168. $isString = false;
  169. foreach ($arrKeys as $key) {
  170. if (is_int($key)) {
  171. $isDigit = true;
  172. break;
  173. }
  174. }
  175. if ($isDigit) {
  176. $mixed = $this->packArrayToObjec($mixed);
  177. } else {
  178. $mixed = (object)$mixed;
  179. }
  180. }
  181. if (is_object($mixed) && isset($mixed->complexObjectArray)) {
  182. foreach ($mixed->complexObjectArray as $k => $v) {
  183. $mixed->complexObjectArray[$k] = $this->wsiArrayPacker($v);
  184. }
  185. }
  186. return $mixed;
  187. }
  188. /**
  189. * For response to the WSI, generates an object from array.
  190. *
  191. * @param Array $arr - Link to Object
  192. * @return Object
  193. */
  194. public function packArrayToObjec(Array $arr)
  195. {
  196. $obj = new stdClass();
  197. $obj->complexObjectArray = $arr;
  198. return $obj;
  199. }
  200. /**
  201. * Convert objects and arrays to array recursively
  202. *
  203. * @param array|object $data
  204. * @return void
  205. */
  206. public function toArray(&$data)
  207. {
  208. if (is_object($data)) {
  209. $data = get_object_vars($data);
  210. }
  211. if (is_array($data)) {
  212. foreach ($data as &$value) {
  213. if (is_array($value) or is_object($value)) {
  214. $this->toArray($value);
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. * Parse filters and format them to be applicable for collection filtration
  221. *
  222. * @param null|object|array $filters
  223. * @param array $fieldsMap Map of field names in format: array('field_name_in_filter' => 'field_name_in_db')
  224. * @return array
  225. */
  226. public function parseFilters($filters, $fieldsMap = null)
  227. {
  228. // if filters are used in SOAP they must be represented in array format to be used for collection filtration
  229. if (is_object($filters)) {
  230. $parsedFilters = array();
  231. // parse simple filter
  232. if (isset($filters->filter) && is_array($filters->filter)) {
  233. foreach ($filters->filter as $field => $value) {
  234. if (is_object($value) && isset($value->key) && isset($value->value)) {
  235. $parsedFilters[$value->key] = $value->value;
  236. } else {
  237. $parsedFilters[$field] = $value;
  238. }
  239. }
  240. }
  241. // parse complex filter
  242. if (isset($filters->complex_filter) && is_array($filters->complex_filter)) {
  243. if ($this->isWsiCompliant()) {
  244. // WS-I compliance mode
  245. foreach ($filters->complex_filter as $fieldName => $condition) {
  246. if (is_object($condition) && isset($condition->key) && isset($condition->value)) {
  247. $conditionName = $condition->key;
  248. $conditionValue = $condition->value;
  249. $this->formatFilterConditionValue($conditionName, $conditionValue);
  250. $parsedFilters[$fieldName] = array($conditionName => $conditionValue);
  251. }
  252. }
  253. } else {
  254. // non WS-I compliance mode
  255. foreach ($filters->complex_filter as $value) {
  256. if (is_object($value) && isset($value->key) && isset($value->value)) {
  257. $fieldName = $value->key;
  258. $condition = $value->value;
  259. if (is_object($condition) && isset($condition->key) && isset($condition->value)) {
  260. $this->formatFilterConditionValue($condition->key, $condition->value);
  261. $parsedFilters[$fieldName] = array($condition->key => $condition->value);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. $filters = $parsedFilters;
  268. }
  269. // make sure that method result is always array
  270. if (!is_array($filters)) {
  271. $filters = array();
  272. }
  273. // apply fields mapping
  274. if (isset($fieldsMap) && is_array($fieldsMap)) {
  275. foreach ($filters as $field => $value) {
  276. if (isset($fieldsMap[$field])) {
  277. unset($filters[$field]);
  278. $field = $fieldsMap[$field];
  279. $filters[$field] = $value;
  280. }
  281. }
  282. }
  283. return $filters;
  284. }
  285. /**
  286. * Check if API is working in SOAP WS-I compliant mode.
  287. *
  288. * @return bool
  289. */
  290. public function isWsiCompliant()
  291. {
  292. $pathInfo = Mage::app()->getRequest()->getPathInfo();
  293. $pathParts = explode('/', trim($pathInfo, '/'));
  294. $controllerPosition = 1;
  295. if (isset($pathParts[$controllerPosition]) && $pathParts[$controllerPosition] == 'soap_wsi') {
  296. $isWsiCompliant = true;
  297. } else {
  298. $isWsiCompliant = false;
  299. }
  300. return $isWsiCompliant;
  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. /**
  320. * Check if attribute is allowed to be used.
  321. *
  322. * @param string $attributeCode
  323. * @param string $type
  324. * @param array $ignoredAttributes
  325. * @param array $attributes
  326. * @return bool
  327. */
  328. public function isAttributeAllowed($attributeCode, $type, $ignoredAttributes, array $attributes = null)
  329. {
  330. if (!empty($attributes) && !(in_array($attributeCode, $attributes))) {
  331. return false;
  332. }
  333. if (isset($ignoredAttributes['global']) && in_array($attributeCode, $ignoredAttributes['global'])) {
  334. return false;
  335. }
  336. if (isset($ignoredAttributes[$type]) && in_array($attributeCode, $ignoredAttributes[$type])) {
  337. return false;
  338. }
  339. return true;
  340. }
  341. }