class-wp-list-util.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * WordPress List utility class
  4. *
  5. * @package WordPress
  6. * @since 4.7.0
  7. */
  8. /**
  9. * List utility.
  10. *
  11. * Utility class to handle operations on an array of objects.
  12. *
  13. * @since 4.7.0
  14. */
  15. class WP_List_Util {
  16. /**
  17. * The input array.
  18. *
  19. * @since 4.7.0
  20. * @var array
  21. */
  22. private $input = array();
  23. /**
  24. * The output array.
  25. *
  26. * @since 4.7.0
  27. * @var array
  28. */
  29. private $output = array();
  30. /**
  31. * Temporary arguments for sorting.
  32. *
  33. * @since 4.7.0
  34. * @var array
  35. */
  36. private $orderby = array();
  37. /**
  38. * Constructor.
  39. *
  40. * Sets the input array.
  41. *
  42. * @since 4.7.0
  43. *
  44. * @param array $input Array to perform operations on.
  45. */
  46. public function __construct( $input ) {
  47. $this->output = $input;
  48. $this->input = $input;
  49. }
  50. /**
  51. * Returns the original input array.
  52. *
  53. * @since 4.7.0
  54. *
  55. * @return array The input array.
  56. */
  57. public function get_input() {
  58. return $this->input;
  59. }
  60. /**
  61. * Returns the output array.
  62. *
  63. * @since 4.7.0
  64. *
  65. * @return array The output array.
  66. */
  67. public function get_output() {
  68. return $this->output;
  69. }
  70. /**
  71. * Filters the list, based on a set of key => value arguments.
  72. *
  73. * @since 4.7.0
  74. *
  75. * @param array $args Optional. An array of key => value arguments to match
  76. * against each object. Default empty array.
  77. * @param string $operator Optional. The logical operation to perform. 'AND' means
  78. * all elements from the array must match. 'OR' means only
  79. * one element needs to match. 'NOT' means no elements may
  80. * match. Default 'AND'.
  81. * @return array Array of found values.
  82. */
  83. public function filter( $args = array(), $operator = 'AND' ) {
  84. if ( empty( $args ) ) {
  85. return $this->output;
  86. }
  87. $operator = strtoupper( $operator );
  88. if ( ! in_array( $operator, array( 'AND', 'OR', 'NOT' ), true ) ) {
  89. return array();
  90. }
  91. $count = count( $args );
  92. $filtered = array();
  93. foreach ( $this->output as $key => $obj ) {
  94. $to_match = (array) $obj;
  95. $matched = 0;
  96. foreach ( $args as $m_key => $m_value ) {
  97. if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] ) {
  98. $matched++;
  99. }
  100. }
  101. if (
  102. ( 'AND' == $operator && $matched == $count ) ||
  103. ( 'OR' == $operator && $matched > 0 ) ||
  104. ( 'NOT' == $operator && 0 == $matched )
  105. ) {
  106. $filtered[ $key ] = $obj;
  107. }
  108. }
  109. $this->output = $filtered;
  110. return $this->output;
  111. }
  112. /**
  113. * Plucks a certain field out of each object in the list.
  114. *
  115. * This has the same functionality and prototype of
  116. * array_column() (PHP 5.5) but also supports objects.
  117. *
  118. * @since 4.7.0
  119. *
  120. * @param int|string $field Field from the object to place instead of the entire object
  121. * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
  122. * Default null.
  123. * @return array Array of found values. If `$index_key` is set, an array of found values with keys
  124. * corresponding to `$index_key`. If `$index_key` is null, array keys from the original
  125. * `$list` will be preserved in the results.
  126. */
  127. public function pluck( $field, $index_key = null ) {
  128. $newlist = array();
  129. if ( ! $index_key ) {
  130. /*
  131. * This is simple. Could at some point wrap array_column()
  132. * if we knew we had an array of arrays.
  133. */
  134. foreach ( $this->output as $key => $value ) {
  135. if ( is_object( $value ) ) {
  136. $newlist[ $key ] = $value->$field;
  137. } else {
  138. $newlist[ $key ] = $value[ $field ];
  139. }
  140. }
  141. $this->output = $newlist;
  142. return $this->output;
  143. }
  144. /*
  145. * When index_key is not set for a particular item, push the value
  146. * to the end of the stack. This is how array_column() behaves.
  147. */
  148. foreach ( $this->output as $value ) {
  149. if ( is_object( $value ) ) {
  150. if ( isset( $value->$index_key ) ) {
  151. $newlist[ $value->$index_key ] = $value->$field;
  152. } else {
  153. $newlist[] = $value->$field;
  154. }
  155. } else {
  156. if ( isset( $value[ $index_key ] ) ) {
  157. $newlist[ $value[ $index_key ] ] = $value[ $field ];
  158. } else {
  159. $newlist[] = $value[ $field ];
  160. }
  161. }
  162. }
  163. $this->output = $newlist;
  164. return $this->output;
  165. }
  166. /**
  167. * Sorts the list, based on one or more orderby arguments.
  168. *
  169. * @since 4.7.0
  170. *
  171. * @param string|array $orderby Optional. Either the field name to order by or an array
  172. * of multiple orderby fields as $orderby => $order.
  173. * @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby
  174. * is a string.
  175. * @param bool $preserve_keys Optional. Whether to preserve keys. Default false.
  176. * @return array The sorted array.
  177. */
  178. public function sort( $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
  179. if ( empty( $orderby ) ) {
  180. return $this->output;
  181. }
  182. if ( is_string( $orderby ) ) {
  183. $orderby = array( $orderby => $order );
  184. }
  185. foreach ( $orderby as $field => $direction ) {
  186. $orderby[ $field ] = 'DESC' === strtoupper( $direction ) ? 'DESC' : 'ASC';
  187. }
  188. $this->orderby = $orderby;
  189. if ( $preserve_keys ) {
  190. uasort( $this->output, array( $this, 'sort_callback' ) );
  191. } else {
  192. usort( $this->output, array( $this, 'sort_callback' ) );
  193. }
  194. $this->orderby = array();
  195. return $this->output;
  196. }
  197. /**
  198. * Callback to sort the list by specific fields.
  199. *
  200. * @since 4.7.0
  201. *
  202. * @see WP_List_Util::sort()
  203. *
  204. * @param object|array $a One object to compare.
  205. * @param object|array $b The other object to compare.
  206. * @return int 0 if both objects equal. -1 if second object should come first, 1 otherwise.
  207. */
  208. private function sort_callback( $a, $b ) {
  209. if ( empty( $this->orderby ) ) {
  210. return 0;
  211. }
  212. $a = (array) $a;
  213. $b = (array) $b;
  214. foreach ( $this->orderby as $field => $direction ) {
  215. if ( ! isset( $a[ $field ] ) || ! isset( $b[ $field ] ) ) {
  216. continue;
  217. }
  218. if ( $a[ $field ] == $b[ $field ] ) {
  219. continue;
  220. }
  221. $results = 'DESC' === $direction ? array( 1, -1 ) : array( -1, 1 );
  222. if ( is_numeric( $a[ $field ] ) && is_numeric( $b[ $field ] ) ) {
  223. return ( $a[ $field ] < $b[ $field ] ) ? $results[0] : $results[1];
  224. }
  225. return 0 > strcmp( $a[ $field ], $b[ $field ] ) ? $results[0] : $results[1];
  226. }
  227. return 0;
  228. }
  229. }