Skip to content

Commit

Permalink
Added the ability to configure multiple templates
Browse files Browse the repository at this point in the history
  • Loading branch information
KarolNet committed Oct 19, 2015
1 parent e43702b commit 529379b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
24 changes: 11 additions & 13 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@ public function getConfigTreeBuilder()
$rootNode = $treeBuilder->root('rating_filter');

$rootNode

->children()
->integerNode('max_rating')
->defaultValue(5)
->end()
->scalarNode('star_full_template')
->defaultValue('<i class="fa fa-star fa-2xx" style="color: #f6d20b"></i>')
->end()
->scalarNode('set_star_half_empty_template')
->defaultValue('<i class="fa fa-star-half-o fa-2xx" style="color: #f6d20b "></i>')
->end()
->scalarNode('set_star_empty')
->defaultValue('<i class="fa fa-star fa-2xx" style="color: #FFFF80"></i>')
->arrayNode('templates')
->prototype('array')
->children('default')
->scalarNode('max_rating')->defaultValue(5)->end()
->scalarNode('star_full_template')->defaultValue('<i class="fa fa-star fa-2xx" style="color: #f6d20b"></i>')->end()
->scalarNode('set_star_half_empty_template')->defaultValue('<i class="fa fa-star fa-2xx" style="color: #f6d20b"></i>')->end()
->scalarNode('set_star_empty')->defaultValue('<i class="fa fa-star fa-2xx" style="color: #FFFF80"></i>')->end()
->end()
->end()
->end()
->end()
;
->end();

return $treeBuilder;
}
Expand Down
8 changes: 8 additions & 0 deletions Exception/RatingFilterConfigurationNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace KarolNet\TwigRatingFilterBundle\Exception;

class RatingFilterConfigurationNotFoundException extends \Exception
{

}
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ new \KarolNet\TwigRatingFilterBundle\KarolNetTwigRatingFilterBundle()
configure if you want:
```
karol_net_twig_rating_filter:
max_rating: 5
star_full_template: '<img src="/ico.png">'
set_star_half_empty_template: ''
set_star_empty: ''
templates:
default:
red:
star_full_template: '<i class="fa fa-star fa-2xx" style="color: red"></i>'
```

Usage:
```
{{ 3 | rating }}. {# default template #}
{{ 4 | rating('red') }} {# red template #}
```
12 changes: 11 additions & 1 deletion Tests/Twig/RatingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class RatingExtensionTest extends \PHPUnit_Framework_TestCase
{
public function testRating()
{
$configuration = [
$configuration['templates']['default'] = [
'max_rating' => 20,
'star_full_template' => 'ful_',
'set_star_half_empty_template' => 'half_',
Expand All @@ -20,4 +20,14 @@ public function testRating()
$result = $ratingFilter->showRating($rating);
$this->assertEquals($result, $exampleSuccessResult);
}

/**
* @expectedException KarolNet\TwigRatingFilterBundle\Exception\RatingFilterConfigurationNotFoundException
*/
public function testEmptyConfiguration()
{
$configuration['templates'] = [];
$ratingFilter = new RatingExtension($configuration);
$ratingFilter->showRating(5);
}
}
31 changes: 25 additions & 6 deletions Twig/RatingExtension.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

namespace KarolNet\TwigRatingFilterBundle\Twig;
use KarolNet\TwigRatingFilterBundle\Exception\RatingFilterConfigurationNotFoundException;

class RatingExtension extends \Twig_Extension
{
/** @var array */
private $config;
/** @var string */
private $starFull;
/** @var string */
Expand All @@ -13,12 +16,10 @@ class RatingExtension extends \Twig_Extension
/** @var int */
private $maxRate;

public function __construct($configuration)
public function __construct($config)
{
$this->maxRate = $configuration['max_rating'];
$this->starFull = $configuration['star_full_template'];
$this->starHalfEmpty = $configuration['set_star_half_empty_template'];
$this->starEmpty = $configuration['set_star_empty'];
$this->config = $config['templates'];
$this->setConfiguration('default');
}

public function getFilters()
Expand All @@ -32,8 +33,12 @@ public function getFilters()
];
}

public function showRating($rating)
public function showRating($rating, $template = 'default')
{
if ($template != 'default') {
$this->setConfiguration($template);
}

$output = '';

for($count = 0; $count < $this->maxRate; $count++) {
Expand All @@ -48,6 +53,20 @@ public function showRating($rating)
return $output;
}

private function setConfiguration($template)
{
if (!array_key_exists($template, $this->config)) {
throw new RatingFilterConfigurationNotFoundException('Configuration for: "' . $template . '" not found');
}

$configuration = $this->config[$template];

$this->maxRate = $configuration['max_rating'];
$this->starFull = $configuration['star_full_template'];
$this->starHalfEmpty = $configuration['set_star_half_empty_template'];
$this->starEmpty = $configuration['set_star_empty'];
}

/**
* @return string
*/
Expand Down

0 comments on commit 529379b

Please sign in to comment.