Browse Source

added StringHelper class

Fabian Peter Hammerle 8 years ago
parent
commit
2c4e712931
4 changed files with 351 additions and 11 deletions
  1. 67 0
      StringHelper.php
  2. 11 11
      composer.lock
  3. 272 0
      tests/StringHelperTest.php
  4. 1 0
      tests/autoload.php

+ 67 - 0
StringHelper.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace fphammerle\helpers;
+
+class StringHelper
+{
+    /**
+     * @return string
+     */
+    public static function prepend($prefix, $text)
+    {
+        if(is_array($text)) {
+            $result = [];
+            foreach($text as $key => $value) {
+                $result[$key] = self::prepend($prefix, $value);
+            }
+            return $result;
+        } else {
+            return ($text === null) ? null : ($prefix . $text);
+        }
+    }
+
+    /**
+     * @return string
+     */
+    public static function append($text, $postfix)
+    {
+        if(is_array($text)) {
+            $result = [];
+            foreach($text as $key => $value) {
+                $result[$key] = self::append($value, $postfix);
+            }
+            return $result;
+        } else {
+            return ($text === null) ? null : ($text . $postfix);
+        }
+    }
+
+    /**
+     * @return string
+     */
+    public static function embed($prefix, $text, $postfix)
+    {
+        return self::prepend($prefix, self::append($text, $postfix));
+    }
+
+    /**
+     * @return string
+     */
+    public static function embrace($brace, $text)
+    {
+        return self::embed($brace, $text, $brace);
+    }
+
+    /**
+     * @return string|null
+     */
+    public static function implode($glue, array $pieces)
+    {
+        $pieces = array_filter($pieces);
+        if(sizeof($pieces) == 0) {
+            return null;
+        } else {
+            return implode($glue, array_filter($pieces));
+        }
+    }
+}

+ 11 - 11
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "hash": "a6a395e303cc513418f8d0cd02353cfa",
+    "hash": "dcf5c9dd39d640b0b33a7ac09178fbf4",
     "content-hash": "20fde3ca91f87ab26fdb44c16588a1bb",
     "packages": [],
     "packages-dev": [
@@ -415,16 +415,16 @@
         },
         {
             "name": "phpunit/phpunit",
-            "version": "4.8.23",
+            "version": "4.8.24",
             "source": {
                 "type": "git",
                 "url": "https://github.com/sebastianbergmann/phpunit.git",
-                "reference": "6e351261f9cd33daf205a131a1ba61c6d33bd483"
+                "reference": "a1066c562c52900a142a0e2bbf0582994671385e"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/6e351261f9cd33daf205a131a1ba61c6d33bd483",
-                "reference": "6e351261f9cd33daf205a131a1ba61c6d33bd483",
+                "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a1066c562c52900a142a0e2bbf0582994671385e",
+                "reference": "a1066c562c52900a142a0e2bbf0582994671385e",
                 "shasum": ""
             },
             "require": {
@@ -483,7 +483,7 @@
                 "testing",
                 "xunit"
             ],
-            "time": "2016-02-11 14:56:33"
+            "time": "2016-03-14 06:16:08"
         },
         {
             "name": "phpunit/phpunit-mock-objects",
@@ -914,16 +914,16 @@
         },
         {
             "name": "symfony/yaml",
-            "version": "v3.0.3",
+            "version": "v3.0.4",
             "source": {
                 "type": "git",
                 "url": "https://github.com/symfony/yaml.git",
-                "reference": "b5ba64cd67ecd6887f63868fa781ca094bd1377c"
+                "reference": "0047c8366744a16de7516622c5b7355336afae96"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/symfony/yaml/zipball/b5ba64cd67ecd6887f63868fa781ca094bd1377c",
-                "reference": "b5ba64cd67ecd6887f63868fa781ca094bd1377c",
+                "url": "https://api.github.com/repos/symfony/yaml/zipball/0047c8366744a16de7516622c5b7355336afae96",
+                "reference": "0047c8366744a16de7516622c5b7355336afae96",
                 "shasum": ""
             },
             "require": {
@@ -959,7 +959,7 @@
             ],
             "description": "Symfony Yaml Component",
             "homepage": "https://symfony.com",
-            "time": "2016-02-23 15:16:06"
+            "time": "2016-03-04 07:55:57"
         }
     ],
     "aliases": [],

+ 272 - 0
tests/StringHelperTest.php

@@ -0,0 +1,272 @@
+<?php
+
+namespace fphammerle\helpers\tests;
+
+use fphammerle\helpers\StringHelper;
+
+class StringHelperTest extends \PHPUnit_Framework_TestCase
+{
+    public function testPrepend()
+    {
+        $this->assertEquals(
+            'prefixstring',
+            StringHelper::prepend('prefix', 'string')
+            );
+    }
+
+    public function testPrependToEmpty()
+    {
+        $this->assertEquals(
+            'prefix',
+            StringHelper::prepend('prefix', '')
+            );
+    }
+
+    public function testPrependToNull()
+    {
+        $this->assertEquals(
+            null,
+            StringHelper::prepend('prefix', null)
+            );
+    }
+
+    public function testPrependNull()
+    {
+        $this->assertEquals(
+            'string',
+            StringHelper::prepend(null, 'string')
+            );
+    }
+
+    public function testPrependToList()
+    {
+        $this->assertEquals(
+            ['prefix1', 'prefix2', 'prefix3'],
+            StringHelper::prepend('prefix', ['1', '2', '3'])
+            );
+    }
+
+    public function testPrependToDict()
+    {
+        $this->assertEquals(
+            ['a' => 'prefix1', 'b' => 'prefix2', 'prefix3'],
+            StringHelper::prepend('prefix', ['a' => '1', 'b' => '2', '3'])
+            );
+    }
+
+    public function testPrependToDictWithNull()
+    {
+        $this->assertEquals(
+            ['a' => 'prefix1', 'b' => null, 'prefix3'],
+            StringHelper::prepend('prefix', ['a' => '1', 'b' => null, '3'])
+            );
+    }
+
+    public function testAppend()
+    {
+        $this->assertEquals(
+            'stringsuffix',
+            StringHelper::append('string', 'suffix')
+            );
+    }
+
+    public function testAppendToEmpty()
+    {
+        $this->assertEquals(
+            'suffix',
+            StringHelper::append('', 'suffix')
+            );
+    }
+
+    public function testAppendToNull()
+    {
+        $this->assertEquals(
+            null,
+            StringHelper::append(null, 'suffix')
+            );
+    }
+
+    public function testAppendNull()
+    {
+        $this->assertEquals(
+            'string',
+            StringHelper::append('string', null)
+            );
+    }
+
+    public function testAppendToList()
+    {
+        $this->assertEquals(
+            ['1postfix', '2postfix', '3postfix'],
+            StringHelper::append(['1', '2', '3'], 'postfix')
+            );
+    }
+
+    public function testAppendToDict()
+    {
+        $this->assertEquals(
+            ['a' => '1postfix', 'b' => '2postfix', '3postfix'],
+            StringHelper::append(['a' => '1', 'b' => '2', '3'], 'postfix')
+            );
+    }
+
+    public function testAppendToDictWithNull()
+    {
+        $this->assertEquals(
+            ['a' => '1postfix', 'b' => null, '3postfix'],
+            StringHelper::append(['a' => '1', 'b' => null, '3'], 'postfix')
+            );
+    }
+
+    public function testEmbed()
+    {
+        $this->assertEquals(
+            'prefixstringsuffix',
+            StringHelper::embed('prefix', 'string', 'suffix')
+            );
+    }
+
+    public function testEmbedToEmpty()
+    {
+        $this->assertEquals(
+            'prefixsuffix',
+            StringHelper::embed('prefix', '', 'suffix')
+            );
+    }
+
+    public function testEmbedToNull()
+    {
+        $this->assertEquals(
+            null,
+            StringHelper::embed('prefix', null, 'suffix')
+            );
+    }
+
+    public function testEmbedNull()
+    {
+        $this->assertEquals(
+            'string',
+            StringHelper::embed(null, 'string', null)
+            );
+    }
+
+    public function testEmbedToList()
+    {
+        $this->assertEquals(
+            ['prefix1postfix', 'prefix2postfix', 'prefix3postfix'],
+            StringHelper::embed('prefix', ['1', '2', '3'], 'postfix')
+            );
+    }
+
+    public function testEmbedToDict()
+    {
+        $this->assertEquals(
+            ['a' => 'prefix1postfix', 'b' => 'prefix2postfix', 'prefix3postfix'],
+            StringHelper::embed('prefix', ['a' => '1', 'b' => '2', '3'], 'postfix')
+            );
+    }
+
+    public function testEmbedToDictWithNull()
+    {
+        $this->assertEquals(
+            ['a' => 'prefix1postfix', 'b' => null, 'prefix3postfix'],
+            StringHelper::embed('prefix', ['a' => '1', 'b' => null, '3'], 'postfix')
+            );
+    }
+
+    public function testEmbrace()
+    {
+        $this->assertEquals(
+            'bracestringbrace',
+            StringHelper::embrace('brace', 'string')
+            );
+    }
+
+    public function testEmbraceToEmpty()
+    {
+        $this->assertEquals(
+            'bracebrace',
+            StringHelper::embrace('brace', '')
+            );
+    }
+
+    public function testEmbraceToNull()
+    {
+        $this->assertEquals(
+            null,
+            StringHelper::embrace('brace', null)
+            );
+    }
+
+    public function testEmbraceNull()
+    {
+        $this->assertEquals(
+            'string',
+            StringHelper::embrace(null, 'string')
+            );
+    }
+
+    public function testEmbraceToList()
+    {
+        $this->assertEquals(
+            ['brace1brace', 'brace2brace', 'brace3brace'],
+            StringHelper::embrace('brace', ['1', '2', '3'])
+            );
+    }
+
+    public function testEmbraceToDict()
+    {
+        $this->assertEquals(
+            ['a' => 'brace1brace', 'b' => 'brace2brace', 'brace3brace'],
+            StringHelper::embrace('brace', ['a' => '1', 'b' => '2', '3'])
+            );
+    }
+
+    public function testEmbraceToDictWithNull()
+    {
+        $this->assertEquals(
+            ['a' => 'brace1brace', 'b' => null, 'brace3brace'],
+            StringHelper::embrace('brace', ['a' => '1', 'b' => null, '3'])
+            );
+    }
+
+    public function testImplode()
+    {
+        $this->assertEquals(
+            'a,b,c,d',
+            StringHelper::implode(',', ['a', 'b', 'c', 'd'])
+            );
+    }
+
+    public function testImplodeWithNull()
+    {
+        $this->assertEquals(
+            'a,b,d',
+            StringHelper::implode(',', ['a', 'b', null, 'd'])
+            );
+    }
+
+    public function testImplodeEmpty()
+    {
+        $this->assertEquals(
+            'acd',
+            StringHelper::implode('', ['a', '', 'c', 'd'])
+            );
+    }
+
+    public function testImplodeByEmpty()
+    {
+        $this->assertEquals(
+            'abcd',
+            StringHelper::implode('', ['a', 'b', 'c', 'd'])
+            );
+    }
+
+    public function testImplodeNothing()
+    {
+        $this->assertEquals(
+            null,
+            StringHelper::implode(',', [null, null, null])
+            );
+    }
+}

+ 1 - 0
tests/autoload.php

@@ -4,3 +4,4 @@
 error_reporting(-1);
 
 require_once(__DIR__ . '/../DateTimeHelper.php');
+require_once(__DIR__ . '/../StringHelper.php');