Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/StringTemplate/AbstractEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* //Prints "This is b and these are d and e"
* </code>
*/
abstract class AbstractEngine
abstract class AbstractEngine implements EngineInterface
{
protected $left;
protected $right;
Expand All @@ -43,4 +43,4 @@ public function __construct($left = '{', $right = '}')
* @return string The rendered template
*/
abstract public function render($template, $value);
}
}
40 changes: 40 additions & 0 deletions src/StringTemplate/EngineFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace StringTemplate;

/**
* EngineFactory
*/
abstract class EngineFactory
{
/**
* @param string $name
*
* @return EngineInterface
*
* @throws \InvalidArgumentException
*/
public static function create($name = 'regular')
{
$name = (string) $name;
$mapping = static::getMapping();

if (!isset($mapping[$name])) {
throw new \InvalidArgumentException(sprintf('Unrecognized "%s" engine', $name));
}

$className = $mapping[$name];
return new $className();
}

/**
* @return array
*/
protected static function getMapping()
{
return array(
'regular' => 'StringTemplate\Engine',
'sprintf' => 'StringTemplate\SprintfEngine'
);
}
}
17 changes: 17 additions & 0 deletions src/StringTemplate/EngineInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace StringTemplate;

/**
* EngineInterface
*/
interface EngineInterface
{
/**
* @param string $template The template string
* @param string|array $value The value the template will be rendered with
*
* @return string The rendered template
*/
public function render($template, $value);
}
34 changes: 34 additions & 0 deletions tests/StringTemplate/EngineFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace StringTemplate\Test;

use StringTemplate\EngineFactory;

/**
* Unit tests for the factory
*/
class EngineFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \InvalidArgumentException
*/
public function testUnrecognizedEngine()
{
EngineFactory::create('test');
}

public function testDefault()
{
$this->assertInstanceOf('StringTemplate\Engine', EngineFactory::create());
}

public function testRegular()
{
$this->assertInstanceOf('StringTemplate\Engine', EngineFactory::create('regular'));
}

public function testSprintf()
{
$this->assertInstanceOf('StringTemplate\SprintfEngine', EngineFactory::create('sprintf'));
}
}