PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/helper/array.php

https://github.com/craigulliott/oAuth2-server
PHP | 144 lines | 59 code | 14 blank | 71 comment | 8 complexity | 61a2864298c787721740449fae2928bf MD5 | raw file
  1. <?php
  2. /**
  3. * This method is heavily used throughout the framework, the purpose of it is to
  4. * target and return a value in an array, removing the need for lots of calls to
  5. * isset(), empty() etc. Basically it takes the array, a key as a pointer
  6. * to an element (key can be an array of keys to target an element in a nested array)
  7. * and an optional default which will be returned if the key doesnt exist
  8. *
  9. * @author Craig Ulliott
  10. */
  11. function array_val($array, $key, $default = null) {
  12. // an array of keys can be used to look in a multidimensional array
  13. if( is_array($key) ){
  14. $k = array_shift($key);
  15. //if $key is now empty then were at the end
  16. if( empty($key) ){
  17. return isset($array[$k]) ? $array[$k] : $default;
  18. }
  19. //resursive call
  20. return isset($array[$k]) ? array_val($array[$k], $key, $default) : $default;
  21. }
  22. //key is not an array
  23. return isset($array[$key]) ? $array[$key] : $default;
  24. }
  25. /**
  26. * This is the same functionality as array_val(), except if the key doesnt exist or the value is NULL then an exception is thrown
  27. *
  28. * @author Craig Ulliott
  29. */
  30. function array_val_required($array, $key) {
  31. // safely take the value out of the array
  32. $return = array_val($array, $key);
  33. // we are satisfied by any value other than NULL
  34. if( $return === NULL ){
  35. throw new exception("$key is a required param");
  36. }
  37. return $return;
  38. }
  39. /**
  40. * a wrapper for array_val which explodes the value to an array from a simple csv
  41. * this method is not aware of escaped characters, its intended use is for simple data
  42. * such as a comma seperated list of ids
  43. *
  44. * @author Craig Ulliott
  45. */
  46. function array_val_csv(array $array, $key, $default = array()) {
  47. // try and get the value out of the array
  48. if( $val = array_val($array, $key) ){
  49. // split it into an array, by the comma
  50. $val_r = explode(',', $val);
  51. // recursively trim the resulting array
  52. $val_r = trim_r($val_r);
  53. // return it
  54. return $val_r;
  55. }
  56. return $default;
  57. }
  58. /**
  59. * a wrapper for make_bool that safely takes a value out of an array
  60. *
  61. * @author Craig Ulliott
  62. */
  63. function array_val_bool(array $array, $key, $default = false) {
  64. return make_bool(array_val($array, $key, $default));
  65. }
  66. /**
  67. * jsondecode an array from an array value, this is a helper method around array_val()
  68. *
  69. * @param array $array
  70. * @param string $key
  71. * @param string $default
  72. * @return void
  73. * @author Craig Ulliott
  74. */
  75. function array_val_json(array $array, $key, $default = array()) {
  76. // get the value out of the array if it exists
  77. if( $value = array_val($array, $key) ){
  78. $arr = json_decode($value, 1);
  79. // if it didnt fail, return it
  80. if( $arr !== false ){
  81. // json decode succeded, return the result
  82. return $arr;
  83. }
  84. }
  85. // return the default
  86. return $default;
  87. }
  88. /**
  89. * recursively trim the elements in an array
  90. *
  91. * @param array $array
  92. * @param string $to_trim
  93. * @return void
  94. * @author Craig Ulliott
  95. */
  96. function trim_r(array $array, $to_trim=' ') {
  97. foreach ( $array as $key => $val ) {
  98. if ( is_array($val) ) {
  99. $array[$key] = trim_r($val);
  100. }
  101. else {
  102. $array[$key] = trim($val, $to_trim);
  103. }
  104. }
  105. return $array;
  106. }
  107. /**
  108. * creates a new array, from the first nested value of each element
  109. * array(array(1),array(2),array(3)) becomes array(1,2,3)
  110. *
  111. * @param string $array
  112. * @return array
  113. * @author Craig Ulliott
  114. */
  115. function array_suck($array){
  116. return array_map("reset", $array);
  117. }
  118. /**
  119. * Convert an array into a CSV string
  120. *
  121. * @param array $array
  122. * @return string
  123. */
  124. function array2csv(array $array) {
  125. $lines = array();
  126. foreach ( $array as $val ) {
  127. $lines[] = is_array($val) ? array2csv($val) : '"' . str_replace('"', '""', $val) . '"';
  128. }
  129. return implode(",", $lines);
  130. }