Browse Source

Merge pull request #13437 from andrii-pukhalevych/patch-1

StaticConfigTrait::getConfig($key) return type
Mark Story 6 years ago
parent
commit
8b5d7fa35c

+ 1 - 1
src/Core/StaticConfigTrait.php

@@ -110,7 +110,7 @@ trait StaticConfigTrait
      * Reads existing configuration.
      *
      * @param string $key The name of the configuration.
-     * @return array|null Array of configuration data.
+     * @return mixed Configuration data.
      */
     public static function getConfig($key)
     {

+ 10 - 10
src/Utility/Hash.php

@@ -928,8 +928,8 @@ class Hash
      *
      * ### Sort directions
      *
-     * - `asc` Sort ascending.
-     * - `desc` Sort descending.
+     * - `asc` or `\SORT_ASC` Sort ascending.
+     * - `desc` or `\SORT_DESC` Sort descending.
      *
      * ### Sort types
      *
@@ -951,7 +951,7 @@ class Hash
      *
      * @param array $data An array of data to sort
      * @param string $path A Set-compatible path to the array value
-     * @param string $dir See directions above. Defaults to 'asc'.
+     * @param string|int $dir See directions above. Defaults to 'asc'.
      * @param array|string $type See direction types above. Defaults to 'regular'.
      * @return array Sorted array of data
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::sort
@@ -985,7 +985,13 @@ class Hash
         $keys = static::extract($result, '{n}.id');
         $values = static::extract($result, '{n}.value');
 
-        $dir = strtolower($dir);
+        if (is_string($dir)) {
+            $dir = strtolower($dir);
+        }
+        if (!in_array($dir, [\SORT_ASC, \SORT_DESC], true)) {
+            $dir = ($dir === 'asc') ? \SORT_ASC : \SORT_DESC;
+        }
+
         $ignoreCase = false;
 
         // $type can be overloaded for case insensitive sort
@@ -995,12 +1001,6 @@ class Hash
             $type = $type['type'];
         }
         $type = strtolower($type);
-
-        if ($dir === 'asc') {
-            $dir = \SORT_ASC;
-        } else {
-            $dir = \SORT_DESC;
-        }
         if ($type === 'numeric') {
             $type = \SORT_NUMERIC;
         } elseif ($type === 'string') {

+ 2 - 1
src/Utility/composer.json

@@ -30,7 +30,8 @@
     },
     "suggest": {
         "ext-intl": "To use Text::transliterate() or Text::slug()",
-        "lib-ICU": "To use Text::transliterate() or Text::slug()"
+        "lib-ICU": "To use Text::transliterate() or Text::slug()",
+        "ext-simplexml": "To use Xml::build(), Xml::loadHtml(), Xml::fromArray(), or Xml::toArray() with default options",
     },
     "autoload": {
         "psr-4": {

+ 24 - 0
tests/TestCase/Utility/HashTest.php

@@ -1849,6 +1849,30 @@ class HashTest extends TestCase
     }
 
     /**
+     * test sorting direction by constants value.
+     *
+     * @return void
+     */
+    public function testSortWithDirectionConst()
+    {
+        $data = [
+            ['class' => 510, 'test' => 2],
+            ['class' => 500, 'test' => 1],
+            ['class' => 600, 'test' => 2],
+            ['class' => 625, 'test' => 4],
+            ['class' => 605, 'test' => 3],
+        ];
+
+        $expected = Hash::sort($data, '{n}.test', 'asc');
+        $result = Hash::sort($data, '{n}.test', \SORT_ASC);
+        $this->assertSame($expected, $result);
+
+        $expected = Hash::sort($data, '{n}.test', 'desc');
+        $result = Hash::sort($data, '{n}.test', \SORT_DESC);
+        $this->assertSame($expected, $result);
+    }
+
+    /**
      * test sorting with string keys.
      *
      * @return void