| | 1 | <?php |
| | 2 | /** |
| | 3 | * WordPress List utility class |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @since 4.7.0 |
| | 7 | */ |
| | 8 | |
| | 9 | /** |
| | 10 | * List utility. |
| | 11 | * |
| | 12 | * Utility class to handle operations on an array of objects. |
| | 13 | * |
| | 14 | * @since 4.7.0 |
| | 15 | */ |
| | 16 | class WP_List_Util { |
| | 17 | /** |
| | 18 | * The input array. |
| | 19 | * |
| | 20 | * @since 4.7.0 |
| | 21 | * @access private |
| | 22 | * @var array |
| | 23 | */ |
| | 24 | private $input = array(); |
| | 25 | |
| | 26 | /** |
| | 27 | * The output array. |
| | 28 | * |
| | 29 | * @since 4.7.0 |
| | 30 | * @access private |
| | 31 | * @var array |
| | 32 | */ |
| | 33 | private $output = array(); |
| | 34 | |
| | 35 | /** |
| | 36 | * Temporary arguments for sorting. |
| | 37 | * |
| | 38 | * @since 4.7.0 |
| | 39 | * @access private |
| | 40 | * @var array |
| | 41 | */ |
| | 42 | private $orderby = array(); |
| | 43 | |
| | 44 | /** |
| | 45 | * Constructor. |
| | 46 | * |
| | 47 | * Sets the input array. |
| | 48 | * |
| | 49 | * @since 4.7.0 |
| | 50 | * |
| | 51 | * @param array $input Array to perform operations on. |
| | 52 | */ |
| | 53 | public function __construct( $input ) { |
| | 54 | $this->output = $this->input = $input; |
| | 55 | } |
| | 56 | |
| | 57 | /** |
| | 58 | * Returns the original input array. |
| | 59 | * |
| | 60 | * @since 4.7.0 |
| | 61 | * @access public |
| | 62 | * |
| | 63 | * @return array The input array. |
| | 64 | */ |
| | 65 | public function get_input() { |
| | 66 | return $this->input; |
| | 67 | } |
| | 68 | |
| | 69 | /** |
| | 70 | * Returns the output array. |
| | 71 | * |
| | 72 | * @since 4.7.0 |
| | 73 | * @access public |
| | 74 | * |
| | 75 | * @return array The output array. |
| | 76 | */ |
| | 77 | public function get_output() { |
| | 78 | return $this->output; |
| | 79 | } |
| | 80 | |
| | 81 | /** |
| | 82 | * Filters the list, based on a set of key => value arguments. |
| | 83 | * |
| | 84 | * @since 4.7.0 |
| | 85 | * |
| | 86 | * @param array $args Optional. An array of key => value arguments to match |
| | 87 | * against each object. Default empty array. |
| | 88 | * @param string $operator Optional. The logical operation to perform. 'AND' means |
| | 89 | * all elements from the array must match. 'OR' means only |
| | 90 | * one element needs to match. 'NOT' means no elements may |
| | 91 | * match. Default 'AND'. |
| | 92 | * @return array Array of found values. |
| | 93 | */ |
| | 94 | public function filter( $args = array(), $operator = 'AND' ) { |
| | 95 | if ( empty( $args ) ) { |
| | 96 | return $this->output; |
| | 97 | } |
| | 98 | |
| | 99 | $operator = strtoupper( $operator ); |
| | 100 | $count = count( $args ); |
| | 101 | $filtered = array(); |
| | 102 | |
| | 103 | foreach ( $this->output as $key => $obj ) { |
| | 104 | $to_match = (array) $obj; |
| | 105 | |
| | 106 | $matched = 0; |
| | 107 | foreach ( $args as $m_key => $m_value ) { |
| | 108 | if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] ) { |
| | 109 | $matched++; |
| | 110 | } |
| | 111 | } |
| | 112 | |
| | 113 | if ( ( 'AND' == $operator && $matched == $count ) |
| | 114 | || ( 'OR' == $operator && $matched > 0 ) |
| | 115 | || ( 'NOT' == $operator && 0 == $matched ) ) { |
| | 116 | $filtered[$key] = $obj; |
| | 117 | } |
| | 118 | } |
| | 119 | |
| | 120 | $this->output = $filtered; |
| | 121 | |
| | 122 | return $this->output; |
| | 123 | } |
| | 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 | if ( ! $index_key ) { |
| | 142 | /* |
| | 143 | * This is simple. Could at some point wrap array_column() |
| | 144 | * if we knew we had an array of arrays. |
| | 145 | */ |
| | 146 | foreach ( $this->output as $key => $value ) { |
| | 147 | if ( is_object( $value ) ) { |
| | 148 | $this->output[ $key ] = $value->$field; |
| | 149 | } else { |
| | 150 | $this->output[ $key ] = $value[ $field ]; |
| | 151 | } |
| | 152 | } |
| | 153 | return $this->output; |
| | 154 | } |
| | 155 | |
| | 156 | /* |
| | 157 | * When index_key is not set for a particular item, push the value |
| | 158 | * to the end of the stack. This is how array_column() behaves. |
| | 159 | */ |
| | 160 | $newlist = array(); |
| | 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 | |
| | 177 | $this->output = $newlist; |
| | 178 | |
| | 179 | return $this->output; |
| | 180 | } |
| | 181 | |
| | 182 | /** |
| | 183 | * Sorts the list, based on one or more orderby arguments. |
| | 184 | * |
| | 185 | * @since 4.7.0 |
| | 186 | * |
| | 187 | * @param string|array $orderby Optional. Either the field name to order by or an array |
| | 188 | * of multiple orderby fields as $orderby => $order. |
| | 189 | * @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby |
| | 190 | * is a string. |
| | 191 | * @return array The sorted array. |
| | 192 | */ |
| | 193 | public function sort( $orderby = array(), $order = 'ASC' ) { |
| | 194 | if ( empty( $orderby ) ) { |
| | 195 | return $this->output; |
| | 196 | } |
| | 197 | |
| | 198 | if ( is_string( $orderby ) ) { |
| | 199 | $orderby = array( $orderby => $order ); |
| | 200 | } |
| | 201 | |
| | 202 | foreach ( $orderby as $field => $direction ) { |
| | 203 | $orderby[ $field ] = 'DESC' === strtoupper( $direction ) ? 'DESC' : 'ASC'; |
| | 204 | } |
| | 205 | |
| | 206 | $this->orderby = $orderby; |
| | 207 | |
| | 208 | usort( $this->output, array( $this, 'sort_callback' ) ); |
| | 209 | |
| | 210 | $this->orderby = array(); |
| | 211 | |
| | 212 | return $this->output; |
| | 213 | } |
| | 214 | |
| | 215 | /** |
| | 216 | * Callback to sort the list by specific fields. |
| | 217 | * |
| | 218 | * @since 4.7.0 |
| | 219 | * @access private |
| | 220 | * |
| | 221 | * @see WP_List_Util::sort() |
| | 222 | * |
| | 223 | * @param object|array $a One object to compare. |
| | 224 | * @param object|array $b The other object to compare. |
| | 225 | * @return int 0 if both objects equal. -1 if second object should come first, 1 otherwise. |
| | 226 | */ |
| | 227 | private function sort_callback( $a, $b ) { |
| | 228 | if ( empty( $this->orderby ) ) { |
| | 229 | return 0; |
| | 230 | } |
| | 231 | |
| | 232 | $a = (array) $a; |
| | 233 | $b = (array) $b; |
| | 234 | |
| | 235 | foreach ( $this->orderby as $field => $direction ) { |
| | 236 | if ( ! isset( $a[ $field ] ) || ! isset( $b[ $field ] ) ) { |
| | 237 | continue; |
| | 238 | } |
| | 239 | |
| | 240 | if ( $a[ $field ] == $b[ $field ] ) { |
| | 241 | continue; |
| | 242 | } |
| | 243 | |
| | 244 | $results = 'DESC' === $direction ? array( 1, -1 ) : array( -1, 1 ); |
| | 245 | |
| | 246 | if ( is_numeric( $a[ $field ] ) && is_numeric( $b[ $field ] ) ) { |
| | 247 | return ( $a[ $field ] < $b[ $field ] ) ? $results[0] : $results[1]; |
| | 248 | } |
| | 249 | |
| | 250 | return 0 > strcmp( $a[ $field ], $b[ $field ] ) ? $results[0] : $results[1]; |
| | 251 | } |
| | 252 | |
| | 253 | return 0; |
| | 254 | } |
| | 255 | } |