PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Utils/Array.php

https://github.com/michaelmcandrew/ste
PHP | 348 lines | 179 code | 35 blank | 134 comment | 38 complexity | b59ede9e906cf2ce6a3096977ca5a85b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. class CRM_Utils_Array {
  35. /**
  36. * if the key exists in the list returns the associated value
  37. *
  38. * @access public
  39. *
  40. * @param array $list the array to be searched
  41. * @param string $key the key value
  42. *
  43. * @return value if exists else null
  44. * @static
  45. * @access public
  46. *
  47. */
  48. static function value( $key, &$list, $default = null ) {
  49. if ( is_array( $list ) ) {
  50. return array_key_exists( $key, $list ) ? $list[$key] : $default;
  51. }
  52. return $default;
  53. }
  54. /**
  55. * Given a parameter array and a key to search for,
  56. * search recursively for that key's value.
  57. *
  58. * @param array $values The parameter array
  59. * @param string $key The key to search for
  60. * @return mixed The value of the key, or null.
  61. * @access public
  62. * @static
  63. */
  64. static function retrieveValueRecursive(&$params, $key)
  65. {
  66. if (! is_array($params)) {
  67. return null;
  68. } else if ($value = CRM_Utils_Array::value($key, $params)) {
  69. return $value;
  70. } else {
  71. foreach ($params as $subParam) {
  72. if ( is_array( $subParam ) &&
  73. $value = self::retrieveValueRecursive( $subParam, $key ) ) {
  74. return $value;
  75. }
  76. }
  77. }
  78. return null;
  79. }
  80. /**
  81. * if the value exists in the list returns the associated key
  82. *
  83. * @access public
  84. *
  85. * @param list the array to be searched
  86. * @param value the search value
  87. *
  88. * @return key if exists else null
  89. * @static
  90. * @access public
  91. *
  92. */
  93. static function key( $value, &$list ) {
  94. if ( is_array( $list ) ) {
  95. $key = array_search( $value, $list );
  96. // array_search returns key if found, false otherwise
  97. // it may return values like 0 or empty string which
  98. // evaluates to false
  99. // hence we must use identical comparison operator
  100. return ($key === false) ? null : $key;
  101. }
  102. return null;
  103. }
  104. static function &xml( &$list, $depth = 1, $seperator = "\n" ) {
  105. $xml = '';
  106. foreach( $list as $name => $value ) {
  107. $xml .= str_repeat( ' ', $depth * 4 );
  108. if ( is_array( $value ) ) {
  109. $xml .= "<{$name}>{$seperator}";
  110. $xml .= self::xml( $value, $depth + 1, $seperator );
  111. $xml .= str_repeat( ' ', $depth * 4 );
  112. $xml .= "</{$name}>{$seperator}";
  113. } else {
  114. // make sure we escape value
  115. $value = self::escapeXML( $value );
  116. $xml .= "<{$name}>$value</{$name}>{$seperator}";
  117. }
  118. }
  119. return $xml;
  120. }
  121. static function escapeXML( $value ) {
  122. static $src = null;
  123. static $dst = null;
  124. if ( ! $src ) {
  125. $src = array( '&' , '<' , '>' , '' );
  126. $dst = array( '&amp;', '&lt;', '&gt;', ',' );
  127. }
  128. return str_replace( $src, $dst, $value );
  129. }
  130. static function flatten( &$list, &$flat, $prefix = '', $seperator = "." ) {
  131. foreach( $list as $name => $value ) {
  132. $newPrefix = ( $prefix ) ? $prefix . $seperator . $name : $name;
  133. if ( is_array( $value ) ) {
  134. self::flatten( $value, $flat, $newPrefix, $seperator );
  135. } else {
  136. if ( ! empty( $value ) ) {
  137. $flat[$newPrefix] = $value;
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Funtion to merge to two arrays recursively
  144. *
  145. * @param array $a1
  146. * @param array $a2
  147. *
  148. * @return $a3
  149. * @static
  150. */
  151. static function crmArrayMerge( $a1, $a2 )
  152. {
  153. if ( empty($a1) ) {
  154. return $a2;
  155. }
  156. if ( empty( $a2 ) ) {
  157. return $a1;
  158. }
  159. $a3 = array( );
  160. foreach ( $a1 as $key => $value) {
  161. if ( array_key_exists($key, $a2) &&
  162. is_array($a2[$key]) && is_array($a1[$key]) ) {
  163. $a3[$key] = array_merge($a1[$key], $a2[$key]);
  164. } else {
  165. $a3[$key] = $a1[$key];
  166. }
  167. }
  168. foreach ( $a2 as $key => $value) {
  169. if ( array_key_exists($key, $a1) ) {
  170. // already handled in above loop
  171. continue;
  172. }
  173. $a3[$key] = $a2[$key];
  174. }
  175. return $a3;
  176. }
  177. static function isHierarchical( &$list ) {
  178. foreach ( $list as $n => $v ) {
  179. if ( is_array( $v ) ) {
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. /**
  186. * Array deep copy
  187. *
  188. * @params array $array
  189. * @params int $maxdepth
  190. * @params int $depth
  191. *
  192. * @return array copy of the array
  193. *
  194. * @static
  195. * @access public
  196. */
  197. static function array_deep_copy( &$array, $maxdepth=50, $depth=0 )
  198. {
  199. if( $depth > $maxdepth ) {
  200. return $array;
  201. }
  202. $copy = array();
  203. foreach( $array as $key => $value ) {
  204. if( is_array( $value ) ) {
  205. array_deep_copy( $value, $copy[$key], $maxdepth, ++$depth);
  206. } else {
  207. $copy[$key] = $value;
  208. }
  209. }
  210. return $copy;
  211. }
  212. /**
  213. * Array splice function that preserves associative keys
  214. * defauly php array_splice function doesnot preserve keys
  215. * So specify start and end of the array that you want to remove
  216. *
  217. * @param array $params array to slice
  218. * @param Integer $start
  219. * @param Integer $end
  220. *
  221. * @return void
  222. * @static
  223. */
  224. static function crmArraySplice( &$params, $start, $end )
  225. {
  226. // verify start and end date
  227. if ( $start < 0 ) $start = 0;
  228. if ( $end > count( $params ) ) $end = count( $params );
  229. $i = 0;
  230. // procees unset operation
  231. foreach ( $params as $key => $value ) {
  232. if ( $i >= $start && $i < $end ) {
  233. unset( $params[$key] );
  234. }
  235. $i++;
  236. }
  237. }
  238. /**
  239. * Function for case insensitive in_array search
  240. *
  241. * @param $value value or search string
  242. * @param $params array that need to be searched
  243. * @param $caseInsensitive boolean true or false
  244. *
  245. * @static
  246. */
  247. static function crmInArray( $value, $params, $caseInsensitive = true )
  248. {
  249. foreach ( $params as $item) {
  250. if ( is_array($item) ) {
  251. $ret = crmInArray( $value, $item, $caseInsensitive );
  252. } else {
  253. $ret = ($caseInsensitive) ? strtolower($item) == strtolower($value) : $item == $value;
  254. if ( $ret ) {
  255. return $ret;
  256. }
  257. }
  258. }
  259. return false;
  260. }
  261. /**
  262. * This function is used to convert associative array names to values
  263. * and vice-versa.
  264. *
  265. * This function is used by both the web form layer and the api. Note that
  266. * the api needs the name => value conversion, also the view layer typically
  267. * requires value => name conversion
  268. */
  269. static function lookupValue( &$defaults, $property, $lookup, $reverse )
  270. {
  271. $id = $property . '_id';
  272. $src = $reverse ? $property : $id;
  273. $dst = $reverse ? $id : $property;
  274. if ( ! array_key_exists( strtolower($src), array_change_key_case( $defaults, CASE_LOWER )) ) {
  275. return false;
  276. }
  277. $look = $reverse ? array_flip( $lookup ) : $lookup;
  278. //trim lookup array, ignore . ( fix for CRM-1514 ), eg for prefix/suffix make sure Dr. and Dr both are valid
  279. $newLook = array( );
  280. foreach( $look as $k => $v) {
  281. $newLook[trim($k, ".")] = $v;
  282. }
  283. $look = $newLook;
  284. if(is_array($look)) {
  285. if ( ! array_key_exists( trim(strtolower( $defaults[strtolower($src)] ),'.'), array_change_key_case( $look, CASE_LOWER )) ) {
  286. return false;
  287. }
  288. }
  289. $tempLook = array_change_key_case( $look ,CASE_LOWER);
  290. $defaults[$dst] = $tempLook[trim(strtolower( $defaults[strtolower($src)] ),'.')];
  291. return true;
  292. }
  293. /**
  294. * Function to check if give array is empty
  295. * @param array $array array that needs to be check for empty condition
  296. *
  297. * @return boolean true is array is empty else false
  298. * @static
  299. */
  300. static function crmIsEmptyArray( $array = array( ) ) {
  301. if ( !is_array( $array ) ) return true;
  302. foreach ( $array as $element ) {
  303. if ( is_array( $element ) ) {
  304. if ( !self::crmIsEmptyArray($element) ) {
  305. return false;
  306. }
  307. } elseif ( isset( $element ) ) {
  308. return false;
  309. }
  310. }
  311. return true;
  312. }
  313. }