Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Component / Utility / SortArray.php
1 <?php
2
3 namespace Drupal\Component\Utility;
4
5 /**
6  * Provides generic array sorting helper methods.
7  *
8  * @ingroup utility
9  */
10 class SortArray {
11
12   /**
13    * Sorts a structured array by the 'weight' element.
14    *
15    * Note that the sorting is by the 'weight' array element, not by the render
16    * element property '#weight'.
17    *
18    * Callback for uasort().
19    *
20    * @param array $a
21    *   First item for comparison. The compared items should be associative
22    *   arrays that optionally include a 'weight' element. For items without a
23    *   'weight' element, a default value of 0 will be used.
24    * @param array $b
25    *   Second item for comparison.
26    *
27    * @return int
28    *   The comparison result for uasort().
29    */
30   public static function sortByWeightElement(array $a, array $b) {
31     return static::sortByKeyInt($a, $b, 'weight');
32   }
33
34   /**
35    * Sorts a structured array by '#weight' property.
36    *
37    * Callback for uasort().
38    *
39    * @param array $a
40    *   First item for comparison. The compared items should be associative
41    *   arrays that optionally include a '#weight' key.
42    * @param array $b
43    *   Second item for comparison.
44    *
45    * @return int
46    *   The comparison result for uasort().
47    */
48   public static function sortByWeightProperty($a, $b) {
49     return static::sortByKeyInt($a, $b, '#weight');
50   }
51
52   /**
53    * Sorts a structured array by 'title' key (no # prefix).
54    *
55    * Callback for uasort().
56    *
57    * @param array $a
58    *   First item for comparison. The compared items should be associative arrays
59    *   that optionally include a 'title' key.
60    * @param array $b
61    *   Second item for comparison.
62    *
63    * @return int
64    *   The comparison result for uasort().
65    */
66   public static function sortByTitleElement($a, $b) {
67     return static::sortByKeyString($a, $b, 'title');
68   }
69
70   /**
71    * Sorts a structured array by '#title' property.
72    *
73    * Callback for uasort().
74    *
75    * @param array $a
76    *   First item for comparison. The compared items should be associative arrays
77    *   that optionally include a '#title' key.
78    * @param array $b
79    *   Second item for comparison.
80    *
81    * @return int
82    *   The comparison result for uasort().
83    */
84   public static function sortByTitleProperty($a, $b) {
85     return static::sortByKeyString($a, $b, '#title');
86   }
87
88   /**
89    * Sorts a string array item by an arbitrary key.
90    *
91    * @param array $a
92    *   First item for comparison.
93    * @param array $b
94    *   Second item for comparison.
95    * @param string $key
96    *   The key to use in the comparison.
97    *
98    * @return int
99    *   The comparison result for uasort().
100    */
101   public static function sortByKeyString($a, $b, $key) {
102     $a_title = (is_array($a) && isset($a[$key])) ? $a[$key] : '';
103     $b_title = (is_array($b) && isset($b[$key])) ? $b[$key] : '';
104
105     return strnatcasecmp($a_title, $b_title);
106   }
107
108   /**
109    * Sorts an integer array item by an arbitrary key.
110    *
111    * @param array $a
112    *   First item for comparison.
113    * @param array $b
114    *   Second item for comparison.
115    * @param string $key
116    *   The key to use in the comparison.
117    *
118    * @return int
119    *   The comparison result for uasort().
120    */
121   public static function sortByKeyInt($a, $b, $key) {
122     $a_weight = (is_array($a) && isset($a[$key])) ? $a[$key] : 0;
123     $b_weight = (is_array($b) && isset($b[$key])) ? $b[$key] : 0;
124
125     if ($a_weight == $b_weight) {
126       return 0;
127     }
128
129     return ($a_weight < $b_weight) ? -1 : 1;
130   }
131
132 }