-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/php/com/selfcoders/phputils/placeholders/Method.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
namespace com\selfcoders\phputils\placeholders; | ||
|
||
class Method | ||
{ | ||
/** | ||
* @var object | ||
*/ | ||
public $object; | ||
/** | ||
* @var string | ||
*/ | ||
public $method; | ||
|
||
/** | ||
* @param object $object | ||
* @param string $method | ||
*/ | ||
public function __construct($object, $method) | ||
{ | ||
$this->object = $object; | ||
$this->method = $method; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/php/com/selfcoders/phputils/placeholders/Placeholder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
namespace com\selfcoders\phputils\placeholders; | ||
|
||
use com\selfcoders\phputils\placeholders\exception\InvalidPlaceholderNameException; | ||
|
||
class Placeholder | ||
{ | ||
const NAME_REGEX = "/\\{\\{([a-zA-Z0-9_\-]+)\\}\\}/"; | ||
const NAME_PREFIX = "{{"; | ||
const NAME_SUFFIX = "}}"; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
/** | ||
* @var mixed | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
* @throws InvalidPlaceholderNameException | ||
*/ | ||
public function __construct($name, $value) | ||
{ | ||
if (!self::validate($name)) { | ||
throw new InvalidPlaceholderNameException($name); | ||
} | ||
|
||
$this->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; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/php/com/selfcoders/phputils/placeholders/Placeholders.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
namespace com\selfcoders\phputils\placeholders; | ||
|
||
use ArrayObject; | ||
use com\selfcoders\phputils\placeholders\exception\UndefinedPlaceholderException; | ||
|
||
class Placeholders extends ArrayObject | ||
{ | ||
/** | ||
* @param Placeholder $placeholder | ||
*/ | ||
public function append($placeholder) | ||
{ | ||
/** | ||
* @var $thisPlaceholder Placeholder | ||
*/ | ||
foreach ($this as &$thisPlaceholder) { | ||
if (strtolower($thisPlaceholder->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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...in/php/com/selfcoders/phputils/placeholders/exception/InvalidPlaceholderNameException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
namespace com\selfcoders\phputils\placeholders\exception; | ||
|
||
class InvalidPlaceholderNameException extends PlaceholderException | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function __construct($name) | ||
{ | ||
$this->name = $name; | ||
|
||
parent::__construct("Invalid placeholder name: " . $name); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/php/com/selfcoders/phputils/placeholders/exception/PlaceholderException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
namespace com\selfcoders\phputils\placeholders\exception; | ||
|
||
use Exception; | ||
|
||
class PlaceholderException extends Exception | ||
{ | ||
} |
28 changes: 28 additions & 0 deletions
28
...main/php/com/selfcoders/phputils/placeholders/exception/UndefinedPlaceholderException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
namespace com\selfcoders\phputils\placeholders\exception; | ||
|
||
class UndefinedPlaceholderException extends PlaceholderException | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $placeholder; | ||
|
||
/** | ||
* @param string $placeholder | ||
*/ | ||
public function __construct($placeholder) | ||
{ | ||
$this->placeholder = $placeholder; | ||
|
||
parent::__construct("Placeholder '" . $placeholder . "' is not defined"); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPlaceholder() | ||
{ | ||
return $this->placeholder; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/php/com/selfcoders/phputils/placeholders/PlaceholderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
namespace com\selfcoders\phputils\placeholders; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
|
||
class PlaceholderTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testValidate() | ||
{ | ||
$this->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")); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/test/php/com/selfcoders/phputils/placeholders/PlaceholdersTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
namespace com\selfcoders\phputils\placeholders; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
|
||
class PlaceholdersTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testSingle() | ||
{ | ||
$placeholders = new Placeholders; | ||
|
||
$placeholders->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}}"); | ||
} | ||
} |