class-wp-list-util.php 6.2 KB

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