From 450caaeb480594560fd3d063a0b280a9644a6c70 Mon Sep 17 00:00:00 2001 From: Michael Wieland Date: Fri, 28 Oct 2016 09:33:48 +0200 Subject: [PATCH] Added placeholders classes --- .../phputils/placeholders/Method.php | 24 +++++ .../phputils/placeholders/Placeholder.php | 60 +++++++++++++ .../phputils/placeholders/Placeholders.php | 88 +++++++++++++++++++ .../InvalidPlaceholderNameException.php | 28 ++++++ .../exception/PlaceholderException.php | 8 ++ .../UndefinedPlaceholderException.php | 28 ++++++ .../phputils/placeholders/PlaceholderTest.php | 19 ++++ .../placeholders/PlaceholdersTest.php | 63 +++++++++++++ 8 files changed, 318 insertions(+) create mode 100644 src/main/php/com/selfcoders/phputils/placeholders/Method.php create mode 100644 src/main/php/com/selfcoders/phputils/placeholders/Placeholder.php create mode 100644 src/main/php/com/selfcoders/phputils/placeholders/Placeholders.php create mode 100644 src/main/php/com/selfcoders/phputils/placeholders/exception/InvalidPlaceholderNameException.php create mode 100644 src/main/php/com/selfcoders/phputils/placeholders/exception/PlaceholderException.php create mode 100644 src/main/php/com/selfcoders/phputils/placeholders/exception/UndefinedPlaceholderException.php create mode 100644 src/test/php/com/selfcoders/phputils/placeholders/PlaceholderTest.php create mode 100644 src/test/php/com/selfcoders/phputils/placeholders/PlaceholdersTest.php diff --git a/src/main/php/com/selfcoders/phputils/placeholders/Method.php b/src/main/php/com/selfcoders/phputils/placeholders/Method.php new file mode 100644 index 0000000..b3bfc52 --- /dev/null +++ b/src/main/php/com/selfcoders/phputils/placeholders/Method.php @@ -0,0 +1,24 @@ +object = $object; + $this->method = $method; + } +} \ No newline at end of file diff --git a/src/main/php/com/selfcoders/phputils/placeholders/Placeholder.php b/src/main/php/com/selfcoders/phputils/placeholders/Placeholder.php new file mode 100644 index 0000000..3b0fa86 --- /dev/null +++ b/src/main/php/com/selfcoders/phputils/placeholders/Placeholder.php @@ -0,0 +1,60 @@ +name = $name; + $this->value = $value; + } + + /** + * @param string $name + * @return bool + */ + public static function validate($name) + { + return (bool)preg_match(self::NAME_REGEX, self::NAME_PREFIX . $name . self::NAME_SUFFIX); + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @return mixed + */ + public function getValue() + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/main/php/com/selfcoders/phputils/placeholders/Placeholders.php b/src/main/php/com/selfcoders/phputils/placeholders/Placeholders.php new file mode 100644 index 0000000..ba536c2 --- /dev/null +++ b/src/main/php/com/selfcoders/phputils/placeholders/Placeholders.php @@ -0,0 +1,88 @@ +getName()) == strtolower($placeholder->getName())) { + $thisPlaceholder = $placeholder; + return; + } + } + + parent::append($placeholder); + } + + /** + * @param Placeholders $placeholders + */ + public function appendAll(Placeholders $placeholders) + { + /** + * @var $placeholder Placeholder + */ + foreach ($placeholders as $placeholder) { + $this->append($placeholder); + } + } + + /** + * @param string $name + * @return Placeholder|null + */ + public function getPlaceholder($name) + { + /** + * @var $thisPlaceholder Placeholder + */ + foreach ($this as $thisPlaceholder) { + if (strtolower($thisPlaceholder->getName()) == strtolower($name)) { + return $thisPlaceholder; + } + } + + return null; + } + + /** + * @param string $name + * @return mixed + * @throws UndefinedPlaceholderException + */ + public function getValueForPlaceholder($name) + { + $placeholder = $this->getPlaceholder($name); + if ($placeholder === null) { + throw new UndefinedPlaceholderException($name); + } + + $value = $placeholder->getValue(); + if ($value instanceof Method) { + return $value->object->{$value->method}(); + } else { + return $value; + } + } + + /** + * @param $string + * @return mixed + */ + public function replace($string) + { + return preg_replace_callback(Placeholder::NAME_REGEX, function ($matches) { + return $this->getValueForPlaceholder($matches[1]); + }, $string); + } +} \ No newline at end of file diff --git a/src/main/php/com/selfcoders/phputils/placeholders/exception/InvalidPlaceholderNameException.php b/src/main/php/com/selfcoders/phputils/placeholders/exception/InvalidPlaceholderNameException.php new file mode 100644 index 0000000..af8fd70 --- /dev/null +++ b/src/main/php/com/selfcoders/phputils/placeholders/exception/InvalidPlaceholderNameException.php @@ -0,0 +1,28 @@ +name = $name; + + parent::__construct("Invalid placeholder name: " . $name); + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } +} \ No newline at end of file diff --git a/src/main/php/com/selfcoders/phputils/placeholders/exception/PlaceholderException.php b/src/main/php/com/selfcoders/phputils/placeholders/exception/PlaceholderException.php new file mode 100644 index 0000000..891b444 --- /dev/null +++ b/src/main/php/com/selfcoders/phputils/placeholders/exception/PlaceholderException.php @@ -0,0 +1,8 @@ +placeholder = $placeholder; + + parent::__construct("Placeholder '" . $placeholder . "' is not defined"); + } + + /** + * @return string + */ + public function getPlaceholder() + { + return $this->placeholder; + } +} \ No newline at end of file diff --git a/src/test/php/com/selfcoders/phputils/placeholders/PlaceholderTest.php b/src/test/php/com/selfcoders/phputils/placeholders/PlaceholderTest.php new file mode 100644 index 0000000..29bd5a0 --- /dev/null +++ b/src/test/php/com/selfcoders/phputils/placeholders/PlaceholderTest.php @@ -0,0 +1,19 @@ +assertTrue(Placeholder::validate("FOO")); + $this->assertTrue(Placeholder::validate("bar")); + $this->assertTrue(Placeholder::validate("fooBar")); + $this->assertTrue(Placeholder::validate("foo-bar")); + $this->assertTrue(Placeholder::validate("foo_bar")); + $this->assertFalse(Placeholder::validate("foo+bar")); + $this->assertFalse(Placeholder::validate("foo~bar")); + $this->assertFalse(Placeholder::validate("Foo!Bar")); + } +} \ No newline at end of file diff --git a/src/test/php/com/selfcoders/phputils/placeholders/PlaceholdersTest.php b/src/test/php/com/selfcoders/phputils/placeholders/PlaceholdersTest.php new file mode 100644 index 0000000..84fa5dc --- /dev/null +++ b/src/test/php/com/selfcoders/phputils/placeholders/PlaceholdersTest.php @@ -0,0 +1,63 @@ +append(new Placeholder("word", "world")); + + $this->assertEquals("Hello world", $placeholders->replace("Hello {{word}}")); + } + + public function testMultiple() + { + $placeholders = new Placeholders; + + $placeholders->append(new Placeholder("word", "string")); + $placeholders->append(new Placeholder("firstWord", "This")); + + $this->assertEquals("This is a longer string", $placeholders->replace("{{firstWord}} is a longer {{word}}")); + } + + public function testGetValueForPlaceholder() + { + $placeholders = new Placeholders; + + $placeholders->append(new Placeholder("foo", "bar")); + $placeholders->append(new Placeholder("hello", "world")); + + $this->assertEquals("bar", $placeholders->getValueForPlaceholder("foo")); + $this->assertEquals("world", $placeholders->getValueForPlaceholder("hello")); + } + + public function testGetPlaceholder() + { + $fooPlaceholder = new Placeholder("foo", "bar"); + $helloPlaceholder = new Placeholder("hello", "world"); + + $placeholders = new Placeholders; + + $placeholders->append($fooPlaceholder); + $placeholders->append($helloPlaceholder); + + $this->assertEquals($fooPlaceholder, $placeholders->getPlaceholder("foo")); + $this->assertEquals($helloPlaceholder, $placeholders->getPlaceholder("hello")); + } + + /** + * @expectedException \com\selfcoders\phputils\placeholders\exception\UndefinedPlaceholderException + */ + public function testUndefinedPlaceholder() + { + $placeholders = new Placeholders; + + $placeholders->append(new Placeholder("foo", "bar")); + + $placeholders->replace("foo {{bar}}"); + } +} \ No newline at end of file