-
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
13 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
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 KarolNet\RatingFilterBundle\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
|
||
class FrontController extends Controller | ||
{ | ||
/** | ||
* @Route("/", name="home") | ||
* @Template() | ||
*/ | ||
public function indexAction() | ||
{ | ||
return []; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace KarolNet\RatingFilterBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
/** | ||
* This is the class that validates and merges configuration from your app/config files | ||
* | ||
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$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>') | ||
->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace KarolNet\RatingFilterBundle\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
|
||
/** | ||
* This is the class that loads and manages your bundle configuration | ||
* | ||
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} | ||
*/ | ||
class RatingFilterExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$configuration = new Configuration(); | ||
$config = $this->processConfiguration($configuration, $configs); | ||
|
||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('services.yml'); | ||
$container->setParameter('rating_filter', $config); | ||
} | ||
} |
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,13 @@ | ||
Description | ||
-------- | ||
|
||
Karolnet RatingFilter is simple twig filter to generate stars rating from number | ||
|
||
Installation | ||
------------ | ||
|
||
composer require karolnet/rating-filter | ||
|
||
add bundle to AppKernel | ||
|
||
configure if you want |
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,9 @@ | ||
<?php | ||
|
||
namespace KarolNet\RatingFilterBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class RatingFilterBundle extends Bundle | ||
{ | ||
} |
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,3 @@ | ||
rating_filter_homepage: | ||
path: /hello/{name} | ||
defaults: { _controller: RatingFilterBundle:Default:index } |
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,14 @@ | ||
parameters: | ||
max_rating: 10; | ||
star_full_template: '<i class="fa fa-star fa-2xx" style="color: #f6d20b"></i>' | ||
set_star_half_empty_template: '<i class="fa fa-star-half-o fa-2xx" style="color: #f6d20b "></i>' | ||
set_star_empty: '<i class="fa fa-star fa-2xx" style="color: #FFFF80"></i>' | ||
|
||
services: | ||
karol_net.twig_extension.rating: | ||
class: KarolNet\RatingFilterBundle\Twig\RatingExtension | ||
public: false | ||
arguments: | ||
- "%rating_filter%" | ||
tags: | ||
- { name: twig.extension } |
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,11 @@ | ||
<?xml version="1.0"?> | ||
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> | ||
<file source-language="en" datatype="plaintext" original="file.ext"> | ||
<body> | ||
<trans-unit id="1"> | ||
<source>Symfony2 is great</source> | ||
<target>J'aime Symfony2</target> | ||
</trans-unit> | ||
</body> | ||
</file> | ||
</xliff> |
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,5 @@ | ||
{% extends '::base.html.twig' %} | ||
|
||
{% block body %} | ||
{{ 2 | rating }} | ||
{% endblock %} |
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 @@ | ||
<i class="fa fa-star fa-2xx" style="color: #FFFF80"></i> |
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 @@ | ||
<i class="fa fa-star fa-2xx" style="color: #f6d20b"></i> |
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 @@ | ||
<i class="fa fa-star-half-o fa-2xx" style="color: #f6d20b "></i> |
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 KarolNet\RatingFilterBundle\Twig; | ||
|
||
class RatingExtension extends \Twig_Extension | ||
{ | ||
/** @var string */ | ||
private $starFull; | ||
/** @var string */ | ||
private $starHalfEmpty; | ||
/** @var string */ | ||
private $starEmpty; | ||
/** @var int */ | ||
private $maxRate; | ||
|
||
public function __construct($configuration) | ||
{ | ||
$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']; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
new \Twig_SimpleFilter( | ||
'rating', | ||
[$this, 'showRating'], | ||
['is_safe' => ['html']] | ||
), | ||
]; | ||
} | ||
|
||
public function showRating($rating) | ||
{ | ||
$output = ''; | ||
|
||
for($count = 0; $count < $this->maxRate; $count++) { | ||
|
||
if ($rating < $count) { | ||
$output = $output . $this->starEmpty; | ||
} elseif($rating == $count) { | ||
$output = $output . $this->starHalfEmpty; | ||
}else { | ||
$output = $output . $this->starFull; | ||
} | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'rating_extension'; | ||
} | ||
} |