diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6ddd33f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/vendor/ +composer.lock +phpunit.xml +/coverage/ +.php_cs.cache diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..f003be0 --- /dev/null +++ b/.php_cs @@ -0,0 +1,18 @@ +setRiskyAllowed(true) + ->setRules(array( + '@PSR2' => true, + 'psr4' => true, + 'ordered_class_elements' => true, + 'ordered_imports' => true, + 'phpdoc_add_missing_param_annotation' => true, + 'phpdoc_order' => true, + )) + ->setFinder( + PhpCsFixer\Finder::create() + ->exclude('tests/Fixtures') + ->exclude('vendor') + ->in(__DIR__) + ); \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2a1394c --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "phpol/string", + "description": "PHP Object Library String Component", + "type": "library", + "license": "MIT", + "minimum-stability": "stable", + "require": {}, + "autoload": { + "psr-4": { + "OL\\String\\": "src/" + } + }, + "require-dev": { + "phpunit/phpunit": "^6.0", + "friendsofphp/php-cs-fixer": "^2.0" + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..c5cce58 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,24 @@ + + + + + ./tests + + + + + ./src + + + + + + + \ No newline at end of file diff --git a/src/Contracts/Str.php b/src/Contracts/Str.php new file mode 100644 index 0000000..b273eb6 --- /dev/null +++ b/src/Contracts/Str.php @@ -0,0 +1,46 @@ +payload = (string) $payload; + } + + /** + * Str objects should should be able to be cast as string. + * + * @return string The actual string + */ + public function __toString() + { + return $this->contents(); + } + + /** + * Public method to retrieve the raw string contents of an object. + * + * @return string + */ + public function contents() + { + return $this->payload; + } + + /** + * Replace one or more entries from the given object with a given value + * + * @param array|string $search What to search for + * @param string $replace Replace with? + * + * @return StrContract Object instance with the replaced text. + */ + public function replace($search, string $replace) + { + return $this->factory(str_replace($search, $replace, $this)); + } + + /** + * Factories a new StrContract object. + * + * @param string $str Instance to be factory from + * + * @return StrContract + */ + protected function factory(string $str) + { + return new self($str); + } +} diff --git a/tests/StrTest.php b/tests/StrTest.php new file mode 100644 index 0000000..bfb358e --- /dev/null +++ b/tests/StrTest.php @@ -0,0 +1,87 @@ +assertInstanceOf(StrContract::class, $str); + } + + /** + * This test should determine if an object of Str can be cast as a string. + */ + public function test_object_can_be_cast_as_string() + { + // create a new instance + $str = new Str('foo bar baz'); + + // assert the cast value of the object is equals the initially given string. + $this->assertEquals('foo bar baz', (string) $str); + } + + /** + * This test should prove an Str object can be created from another + * Str object. + */ + public function test_instantiable_with_str_object() + { + // create an initial instance + $initialStr = new Str('foo bar'); + + // create an instance passing not directly a string but an StrContract object. + $str = new Str($initialStr); + + // assets contract implementation. + $this->assertInstanceOf(StrContract::class, $str); + // assets the contents are correctly passed between instances. + $this->assertEquals('foo bar', $str->contents()); + } + + /** + * Test replacement of parts of text, using a single string + */ + public function test_replace_single_string() + { + $str = new Str('foo bar'); + + $replaced = $str->replace('bar', 'baz'); + + $this->assertEquals('foo baz', $replaced); + } + + /** + * Test replacement of parts of text, using a multiple search strings + */ + public function test_replace_multiple_string() + { + $str = new Str('foo bar baz'); + + $replaced = $str->replace(['bar', 'baz'], 'foo'); + + $this->assertEquals('foo foo foo', $replaced); + } + + /** + * Test replacement of parts of text, using a multiple search strings and objects + */ + public function test_replace_multiple_string_with_objects() + { + $str = new Str('foo bar baz'); + + $replaced = $str->replace([new Str('bar'), 'baz'], 'foo'); + + $this->assertEquals('foo foo foo', $replaced); + } +}