Browse Source

added StringHelper::unite()

Fabian Peter Hammerle 7 years ago
parent
commit
7bdaf37193
4 changed files with 38 additions and 4 deletions
  1. 2 2
      DateTimeHelper.php
  2. 2 2
      Path.php
  3. 8 0
      StringHelper.php
  4. 26 0
      tests/StringHelperTest.php

+ 2 - 2
DateTimeHelper.php

@@ -124,11 +124,11 @@ class DateTimeHelper
                     sprintf("negative intervals are not supported\n%s", print_r($i, true))
                     );
             } else {
-                return StringHelper::prepend('P', StringHelper::implode('', [
+                return StringHelper::prepend('P', StringHelper::unite([
                     StringHelper::append($i->y ?: null, 'Y'),
                     StringHelper::append($i->m ?: null, 'M'),
                     StringHelper::append($i->d ?: null, 'D'),
-                    StringHelper::prepend('T', StringHelper::implode('', [
+                    StringHelper::prepend('T', StringHelper::unite([
                         StringHelper::append($i->h ?: null, 'H'),
                         StringHelper::append($i->i ?: null, 'M'),
                         StringHelper::append($i->s ?: null, 'S'),

+ 2 - 2
Path.php

@@ -40,9 +40,9 @@ class Path
      */
     public function getBasename()
     {
-        return StringHelper::implode('', [
+        return StringHelper::unite([
             $this->_filename,
-            isset($this->_extension) ? ('.' . $this->_extension) : null,
+            StringHelper::prepend('.', $this->_extension),
             ]);
     }
 

+ 8 - 0
StringHelper.php

@@ -72,6 +72,14 @@ class StringHelper
         }
     }
 
+    /**
+     * @return string|null
+     */
+    public static function unite(array $pieces = null)
+    {
+        return self::implode('', $pieces);
+    }
+
     /**
      * @throws InvalidArgumentException empty needle
      * @param array $needles

+ 26 - 0
tests/StringHelperTest.php

@@ -253,6 +253,32 @@ class StringHelperTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($expected, StringHelper::implode($glue, $pieces));
     }
 
+    public function uniteProvider()
+    {
+        return [
+            [['a', 'b', 'c', 'd'], 'abcd'],
+            [['a', 'b', '', 'd'], 'abd'],
+            [['a', 'b', null, 'd'], 'abd'],
+            [['a', 3, 'c', 'd'], 'a3cd'],
+            [[null, 'b', 'c', 'd'], 'bcd'],
+            [[2 => 'c'], 'c'],
+            [[2 => 'c', 0 => 'a'], 'ca'],
+            [[2 => 'c', 0 => 'a'], implode('', [3 => 'c', 0 => 'a'])],
+            [[2 => 'c', 0 => 'a', 1 => 'b'], 'cab'],
+            [[2 => 'c', 0 => 'a', 1 => 'b'], implode('', [2 => 'c', 0 => 'a', 1 => 'b'])],
+            [[null, null, null], null],
+            [null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider uniteProvider
+     */
+    public function testUnite($pieces, $expected)
+    {
+        $this->assertSame($expected, StringHelper::unite($pieces));
+    }
+
     public function containsAnyProvider()
     {
         return [