Няма описание

class-wp-list-util.php 6.8KB

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