Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
KarolNet committed Jun 30, 2015
1 parent 223366d commit f4ca720
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Controller/FrontController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace KarolNet\TwigRatingFilterBundle\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 [];
}
}
42 changes: 42 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace KarolNet\TwigRatingFilterBundle\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;
}
}
29 changes: 29 additions & 0 deletions DependencyInjection/KarolNetTwigRatingFilterExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace KarolNet\TwigRatingFilterBundle\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 KarolNetTwigRatingFilterExtension 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);
}
}
9 changes: 9 additions & 0 deletions KarolNetTwigRatingFilterBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace KarolNet\TwigRatingFilterBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class KarolNetTwigRatingFilterBundle extends Bundle
{
}
13 changes: 13 additions & 0 deletions README.md
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
14 changes: 14 additions & 0 deletions Resources/config/services.yml
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\TwigRatingFilterBundle\Twig\RatingExtension
public: false
arguments:
- "%rating_filter%"
tags:
- { name: twig.extension }
11 changes: 11 additions & 0 deletions Resources/translations/messages.fr.xlf
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>
5 changes: 5 additions & 0 deletions Resources/views/Front/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends '::base.html.twig' %}

{% block body %}
{{ 2 | rating }}
{% endblock %}
1 change: 1 addition & 0 deletions Resources/views/Rating/starEmpty.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<i class="fa fa-star fa-2xx" style="color: #FFFF80"></i>
1 change: 1 addition & 0 deletions Resources/views/Rating/starFull.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<i class="fa fa-star fa-2xx" style="color: #f6d20b"></i>
1 change: 1 addition & 0 deletions Resources/views/Rating/starHalfEmpty.html.twig
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>
60 changes: 60 additions & 0 deletions Twig/RatingExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace KarolNet\TwigRatingFilterBundle\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';
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"php": ">=5.3.3"
},
"autoload": {
"psr-4": { "KarolNet\\RatingFilterBundle\\": "" }
"psr-4": { "KarolNet\\TwigRatingFilterBundle\\": "" }
}
}

0 comments on commit f4ca720

Please sign in to comment.