From d9fd1ae3e2ff5df53ff63e8c6feaf7dd46bf3c74 Mon Sep 17 00:00:00 2001 From: Karol Grochowalski Date: Tue, 30 Jun 2015 20:21:04 +0200 Subject: [PATCH] init --- Controller/FrontController.php | 19 ++++++ DependencyInjection/Configuration.php | 42 +++++++++++++ DependencyInjection/RatingFilterExtension.php | 29 +++++++++ README.md | 13 ++++ RatingFilterBundle.php | 9 +++ Resources/config/routing.yml | 3 + Resources/config/services.yml | 14 +++++ Resources/translations/messages.fr.xlf | 11 ++++ Resources/views/Front/index.html.twig | 5 ++ Resources/views/Rating/starEmpty.html.twig | 1 + Resources/views/Rating/starFull.html.twig | 1 + .../views/Rating/starHalfEmpty.html.twig | 1 + Twig/RatingExtension.php | 60 +++++++++++++++++++ 13 files changed, 208 insertions(+) create mode 100644 Controller/FrontController.php create mode 100644 DependencyInjection/Configuration.php create mode 100644 DependencyInjection/RatingFilterExtension.php create mode 100644 README.md create mode 100644 RatingFilterBundle.php create mode 100644 Resources/config/routing.yml create mode 100644 Resources/config/services.yml create mode 100644 Resources/translations/messages.fr.xlf create mode 100644 Resources/views/Front/index.html.twig create mode 100644 Resources/views/Rating/starEmpty.html.twig create mode 100644 Resources/views/Rating/starFull.html.twig create mode 100644 Resources/views/Rating/starHalfEmpty.html.twig create mode 100644 Twig/RatingExtension.php diff --git a/Controller/FrontController.php b/Controller/FrontController.php new file mode 100644 index 0000000..c473437 --- /dev/null +++ b/Controller/FrontController.php @@ -0,0 +1,19 @@ +root('rating_filter'); + + $rootNode + ->children() + ->integerNode('max_rating') + ->defaultValue(5) + ->end() + ->scalarNode('star_full_template') + ->defaultValue('') + ->end() + ->scalarNode('set_star_half_empty_template') + ->defaultValue('') + ->end() + ->scalarNode('set_star_empty') + ->defaultValue('') + ->end() + ->end() + ; + + return $treeBuilder; + } +} diff --git a/DependencyInjection/RatingFilterExtension.php b/DependencyInjection/RatingFilterExtension.php new file mode 100644 index 0000000..e8fd5fa --- /dev/null +++ b/DependencyInjection/RatingFilterExtension.php @@ -0,0 +1,29 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + $container->setParameter('rating_filter', $config); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..2ee9422 --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/RatingFilterBundle.php b/RatingFilterBundle.php new file mode 100644 index 0000000..0f010cd --- /dev/null +++ b/RatingFilterBundle.php @@ -0,0 +1,9 @@ +' + set_star_half_empty_template: '' + set_star_empty: '' + +services: + karol_net.twig_extension.rating: + class: KarolNet\RatingFilterBundle\Twig\RatingExtension + public: false + arguments: + - "%rating_filter%" + tags: + - { name: twig.extension } diff --git a/Resources/translations/messages.fr.xlf b/Resources/translations/messages.fr.xlf new file mode 100644 index 0000000..fd59e6c --- /dev/null +++ b/Resources/translations/messages.fr.xlf @@ -0,0 +1,11 @@ + + + + + + Symfony2 is great + J'aime Symfony2 + + + + diff --git a/Resources/views/Front/index.html.twig b/Resources/views/Front/index.html.twig new file mode 100644 index 0000000..12aa636 --- /dev/null +++ b/Resources/views/Front/index.html.twig @@ -0,0 +1,5 @@ +{% extends '::base.html.twig' %} + +{% block body %} + {{ 2 | rating }} +{% endblock %} \ No newline at end of file diff --git a/Resources/views/Rating/starEmpty.html.twig b/Resources/views/Rating/starEmpty.html.twig new file mode 100644 index 0000000..7052f4f --- /dev/null +++ b/Resources/views/Rating/starEmpty.html.twig @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Resources/views/Rating/starFull.html.twig b/Resources/views/Rating/starFull.html.twig new file mode 100644 index 0000000..4b7c560 --- /dev/null +++ b/Resources/views/Rating/starFull.html.twig @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Resources/views/Rating/starHalfEmpty.html.twig b/Resources/views/Rating/starHalfEmpty.html.twig new file mode 100644 index 0000000..fef7e5c --- /dev/null +++ b/Resources/views/Rating/starHalfEmpty.html.twig @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Twig/RatingExtension.php b/Twig/RatingExtension.php new file mode 100644 index 0000000..e850fe2 --- /dev/null +++ b/Twig/RatingExtension.php @@ -0,0 +1,60 @@ +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'; + } +} \ No newline at end of file