Skip to content

Commit

Permalink
Added placeholders classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Programie committed Oct 28, 2016
1 parent 07d4dae commit 450caae
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/php/com/selfcoders/phputils/placeholders/Method.php
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 src/main/php/com/selfcoders/phputils/placeholders/Placeholder.php
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 src/main/php/com/selfcoders/phputils/placeholders/Placeholders.php
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);
}
}
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;
}
}
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
{
}
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;
}
}
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"));
}
}
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}}");
}
}

0 comments on commit 450caae

Please sign in to comment.