PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/acidel/buykoala
PHP | 239 lines | 144 code | 16 blank | 79 comment | 35 complexity | f415b3df91e8537c0229d69f5be748ba MD5 | raw file
  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) 2011 Magento 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. const XML_PATH_API_WSI = 'api/config/compliance_wsi';
  36. /**
  37. * @return boolean
  38. */
  39. public function isComplianceWSI()
  40. {
  41. return Mage::getStoreConfig(self::XML_PATH_API_WSI);
  42. }
  43. /**
  44. * Go thru a WSI args array and turns it to correct state.
  45. *
  46. * @param Object $obj - Link to Object
  47. * @return Object
  48. */
  49. public function wsiArrayUnpacker(&$obj)
  50. {
  51. if (is_object($obj)) {
  52. $modifiedKeys = $this->clearWsiFootprints($obj);
  53. foreach ($obj as $key => $value) {
  54. if (is_object($value)) {
  55. $this->wsiArrayUnpacker($value);
  56. }
  57. if (is_array($value)) {
  58. foreach ($value as &$val) {
  59. if (is_object($val)) {
  60. $this->wsiArrayUnpacker($val);
  61. }
  62. }
  63. }
  64. }
  65. foreach ($modifiedKeys as $arrKey) {
  66. $this->associativeArrayUnpack($obj->$arrKey);
  67. }
  68. }
  69. }
  70. /**
  71. * Go thru an object parameters and unpak associative object to array.
  72. *
  73. * @param Object $obj - Link to Object
  74. * @return Object
  75. */
  76. public function v2AssociativeArrayUnpacker(&$obj)
  77. {
  78. if (is_object($obj)
  79. && property_exists($obj, 'key')
  80. && property_exists($obj, 'value')
  81. ) {
  82. if (count(array_keys(get_object_vars($obj))) == 2) {
  83. $obj = array($obj->key => $obj->value);
  84. return true;
  85. }
  86. } elseif (is_array($obj)) {
  87. $arr = array();
  88. $needReplacement = true;
  89. foreach ($obj as $key => &$value) {
  90. $isAssoc = $this->v2AssociativeArrayUnpacker($value);
  91. if ($isAssoc) {
  92. foreach ($value as $aKey => $aVal) {
  93. $arr[$aKey] = $aVal;
  94. }
  95. } else {
  96. $needReplacement = false;
  97. }
  98. }
  99. if ($needReplacement) {
  100. $obj = $arr;
  101. }
  102. } elseif (is_object($obj)) {
  103. $objectKeys = array_keys(get_object_vars($obj));
  104. foreach ($objectKeys as $key) {
  105. $this->v2AssociativeArrayUnpacker($obj->$key);
  106. }
  107. }
  108. return false;
  109. }
  110. /**
  111. * Go thru mixed and turns it to a correct look.
  112. *
  113. * @param Mixed $mixed A link to variable that may contain associative array.
  114. */
  115. public function associativeArrayUnpack(&$mixed)
  116. {
  117. if (is_array($mixed)) {
  118. $tmpArr = array();
  119. foreach ($mixed as $key => $value) {
  120. if (is_object($value)) {
  121. $value = get_object_vars($value);
  122. if (count($value) == 2 && isset($value['key']) && isset($value['value'])) {
  123. $tmpArr[$value['key']] = $value['value'];
  124. }
  125. }
  126. }
  127. if (count($tmpArr)) {
  128. $mixed = $tmpArr;
  129. }
  130. }
  131. if (is_object($mixed)) {
  132. $numOfVals = count(get_object_vars($mixed));
  133. if ($numOfVals == 2 && isset($mixed->key) && isset($mixed->value)) {
  134. $mixed = get_object_vars($mixed);
  135. /*
  136. * Processing an associative arrays.
  137. * $mixed->key = '2'; $mixed->value = '3'; turns to array(2 => '3');
  138. */
  139. $mixed = array($mixed['key'] => $mixed['value']);
  140. }
  141. }
  142. }
  143. /**
  144. * Corrects data representation.
  145. *
  146. * @param Object $obj - Link to Object
  147. * @return Object
  148. */
  149. public function clearWsiFootprints(&$obj)
  150. {
  151. $modifiedKeys = array();
  152. $objectKeys = array_keys(get_object_vars($obj));
  153. foreach ($objectKeys as $key) {
  154. if (is_object($obj->$key) && isset($obj->$key->complexObjectArray)) {
  155. $obj->$key = $obj->$key->complexObjectArray;
  156. $modifiedKeys[] = $key;
  157. }
  158. }
  159. return $modifiedKeys;
  160. }
  161. /**
  162. * For the WSI, generates an response object.
  163. *
  164. * @param mixed $mixed - Link to Object
  165. * @return mixed
  166. */
  167. public function wsiArrayPacker($mixed)
  168. {
  169. if (is_array($mixed)) {
  170. $arrKeys = array_keys($mixed);
  171. $isDigit = false;
  172. $isString = false;
  173. foreach ($arrKeys as $key) {
  174. if (is_int($key)) {
  175. $isDigit = true;
  176. break;
  177. }
  178. }
  179. if ($isDigit) {
  180. $mixed = $this->packArrayToObjec($mixed);
  181. } else {
  182. $mixed = (object) $mixed;
  183. }
  184. }
  185. if (is_object($mixed) && isset($mixed->complexObjectArray)) {
  186. foreach ($mixed->complexObjectArray as $k => $v) {
  187. $mixed->complexObjectArray[$k] = $this->wsiArrayPacker($v);
  188. }
  189. }
  190. return $mixed;
  191. }
  192. /**
  193. * For response to the WSI, generates an object from array.
  194. *
  195. * @param Array $arr - Link to Object
  196. * @return Object
  197. */
  198. public function packArrayToObjec(Array $arr)
  199. {
  200. $obj = new stdClass();
  201. $obj->complexObjectArray = $arr;
  202. return $obj;
  203. }
  204. /**
  205. * Convert objects and arrays to array recursively
  206. *
  207. * @param array|object $data
  208. * @return void
  209. */
  210. public function toArray(&$data)
  211. {
  212. if (is_object($data)) {
  213. $data = get_object_vars($data);
  214. }
  215. if (is_array($data)) {
  216. foreach ($data as &$value) {
  217. if (is_array($value) or is_object($value)) {
  218. $this->toArray($value);
  219. }
  220. }
  221. }
  222. }
  223. } // Class Mage_Api_Helper_Data End