From 5f35abd89d4c8b5c1716d106cc9be2340001de10 Mon Sep 17 00:00:00 2001 From: smajti1 Date: Wed, 12 Feb 2020 12:42:32 +0100 Subject: [PATCH] 1.1.0 (#1) release of version 1.1.0 --- CHANGELOG.md | 18 ++++++++ LambdaExtension.php | 57 ++++++++++++++++-------- README.md | 74 +++++++++++++++++++++++-------- Tests/Fixtures/filters/any.test | 10 ++--- Tests/Fixtures/filters/every.test | 10 ++--- composer.json | 8 +--- 6 files changed, 124 insertions(+), 53 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a53e7c2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,18 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## [1.1.0] - 2020-02-12 +### Added +- `|is_any`, `|is_every` twig filters +- `==>` twig operator (only for migration) +### Deprecated +- `|map`, `|filter` and `|sort_by` twig filters +- `is any` and `is every` twig test +- `=>`, `;` twig operator + +[1.1.0]: https://github.com/leonaero/twig-lambda/compare/v1.0.0...v1.1.0 + + diff --git a/LambdaExtension.php b/LambdaExtension.php index a62b05f..0f71938 100644 --- a/LambdaExtension.php +++ b/LambdaExtension.php @@ -19,17 +19,26 @@ public function getOperators() { return [ [ - '=>' => [ - 'precedence' => 0, - 'class' => '\DPolac\TwigLambda\NodeExpression\SimpleLambda' - ], + '=>' => [ + 'precedence' => 0, + 'class' => '\DPolac\TwigLambda\NodeExpression\SimpleLambda' + ], + '==>' => [ + 'precedence' => 0, + 'class' => '\DPolac\TwigLambda\NodeExpression\SimpleLambda' + ], ], [ - '=>' => [ - 'precedence' => 0, - 'class' => '\DPolac\TwigLambda\NodeExpression\LambdaWithArguments', - 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT - ], + '=>' => [ + 'precedence' => 0, + 'class' => '\DPolac\TwigLambda\NodeExpression\LambdaWithArguments', + 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT + ], + '==>' => [ + 'precedence' => 0, + 'class' => '\DPolac\TwigLambda\NodeExpression\LambdaWithArguments', + 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT + ], ';' => [ 'precedence' => 5, 'class' => '\DPolac\TwigLambda\NodeExpression\Arguments', @@ -38,7 +47,7 @@ public function getOperators() ] ]; } - + public function getFunctions() { return [ @@ -67,16 +76,22 @@ public function getFilters() new \Twig_SimpleFilter('group_by', '\DPolac\TwigLambda\LambdaExtension::groupBy'), new \Twig_SimpleFilter('sort_by', '\DPolac\TwigLambda\LambdaExtension::sortBy'), new \Twig_SimpleFilter('count_by', '\DPolac\TwigLambda\LambdaExtension::countBy'), + + new \Twig_SimpleFilter('is_every', '\DPolac\TwigLambda\LambdaExtension::every'), + new \Twig_SimpleFilter('is_any', '\DPolac\TwigLambda\LambdaExtension::any'), ]; } - - public static function map($array, $callback) + + /** + * @deprecated since v1.1.0 use native twig function + */ + public static function map($array, $callback) { if (!is_callable($callback)) { throw new \Twig_Error_Runtime(sprintf( 'Second argument of "map" must be callable, but is "%s".', gettype($callback))); } - + if (is_array($array)) { $array = array_map($callback, $array, array_keys($array)); } elseif ($array instanceof \Traversable) { @@ -89,10 +104,13 @@ public static function map($array, $callback) throw new \Twig_Error_Runtime(sprintf( 'First argument of "map" must be array or Traversable, but is "%s".', gettype($array))); } - + return $array; } + /** + * @deprecated since v1.1.0 use native twig function + */ public static function filter($array, $callback) { if (!is_callable($callback)) { @@ -143,7 +161,7 @@ public static function uniqueBy($array, $callback) } else { $result = []; } - + foreach ($array as $i => $item) { foreach ($array as $j => $previous) { if ($i === $j) { @@ -187,6 +205,9 @@ public static function groupBy($array, $callback) return $results; } + /** + * @deprecated since v1.1.0 use native twig function + */ public static function sortBy($array, $callback, $direction = 'ASC') { if (!is_callable($callback)) { @@ -242,7 +263,7 @@ public static function countBy($array, $callback) } return $result; } - + public static function every($array, $callback) { if (!is_callable($callback)) { @@ -263,7 +284,7 @@ public static function every($array, $callback) return true; } - + public static function any($array, $callback) { if (!is_callable($callback)) { @@ -297,4 +318,4 @@ public function getName() { return 'dpolac_lambda_extension'; } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 618c537..8f0fa84 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,14 @@ Listing names of all authors ordered by age: ```twig -{% for author in articles|map(=> _.author)|unique_by('===')|sort_by(=> _.age) %} +{% for author in articles|map(==> _.author)|unique_by('===')|sort_by(==> _.age) %} * {{ author.name }}, {{ author.age }} {% endfor %} ``` Counting elements starting from specified letter: ```twig -{% for key, count in ['foo', 'bar', 'foobar']|countBy(=> _|first|capitalize) %} +{% for key, count in ['foo', 'bar', 'foobar']|count_by(==> _|first|capitalize) %} * {{ count }} elements start from {{ key }}. {% endfor %} ``` @@ -27,7 +27,7 @@ Counting elements starting from specified letter: **Install via Composer:** ```bash -composer require dpolac/twig-lambda +composer require leonaero/twig-lambda ``` **Add the extension to Twig:** @@ -50,9 +50,11 @@ services: ## Usage -### Lambda expression +### ~~Lambda expression~~ +##### deprecated since 1.1.0 and will be remove in 2.0.0 use original twig lambda To create lambda expression prepend any valid Twig expression -with `=>` operator. Inside of the lambda expression you can use +with `=>` operator (For the time of migration to twig 2.10 has been added `==>` operator start from v1.1.0). +Inside of the lambda expression you can use any variable from the outside. There are also two special variables available: * `_` (single underscore) - first argument, @@ -91,15 +93,16 @@ All lambdas are called with two arguments: element and key. ---------------------------------------------------------------- -### |map +### ~~|map~~ **Alias:** `|select`
**Signature:** `array|map(lambda)` +##### deprecated since 1.1.0 and will be remove in 2.0.0 use original twig filter Applies a given function to each element and returns array of results in the same order. ```twig -{% for i in [1, 2, 3, 4]|map(=> _ * 2) %} +{% for i in [1, 2, 3, 4]|map(==> _ * 2) %} {{ i }} {# prints '2 4 6 8' #} {% endfor %} ``` @@ -107,14 +110,15 @@ array of results in the same order. ---------------------------------------------------------------- -### |filter +### ~~|filter~~ **Alias:** `|where`
**Signature:** `array|filter(lambda)` +##### deprecated since 1.1.0 and will be remove in 2.0.0 use original twig filter Returns array of elements that passes a test specified by lambda. ```twig -{% for i in [1, 2, 3, 4, 5, 6]|filter(=> _ is even) %} +{% for i in [1, 2, 3, 4, 5, 6]|filter(==> _ is even) %} {{ i }} {# prints '2 4 6' #} {% endfor %} ``` @@ -154,7 +158,7 @@ equivalent Sorts an array into groups by the result of lambda. ```twig -{% for key, group in ['foo', 'bar', 'foobar', 'barbar']|group_by(=> _|first|capitalize) %} +{% for key, group in ['foo', 'bar', 'foobar', 'barbar']|group_by(==> _|first|capitalize) %} = {{ key }} {% for i in group %} * {{ i }} @@ -176,12 +180,13 @@ will produce ### |sort_by **Signature:** `array|sort_by(lambda[, direction = 'ASC'])` +##### deprecated since 1.1.0 and will be replaced in 2.0.0 by is_any function Sorts array by values returned by lambda. Direction can be 'ASC' or 'DESC'. ```twig -{% for i in ['bar', 'fo', 'foobar', 'foob']|sort_by(=> _|length, 'DESC') %} +{% for i in ['bar', 'fo', 'foobar', 'foob']|sort_by(==> _|length, 'DESC') %} {{ i }} {# prints 'foobar foob bar fo' #} {% endfor %} ``` @@ -200,7 +205,7 @@ string 'true', 'false' or 'null'. Float will be converted to integer. ```twig -{% for key, count in ['foo', 'bar', 'foobar']|count_by(=> _|first|capitalize) %} +{% for key, count in ['foo', 'bar', 'foobar']|count_by(==> _|first|capitalize) %} * {{ count }} elements start from {{ key }}. {% endfor %} ``` @@ -213,8 +218,9 @@ will produce ---------------------------------------------------------------- -### is any +### ~~is any~~ **Signature:** `array is any(lambda)` +##### deprecated since 1.1.0 and will be replaced in 2.0.0 by is_any function Returns true if lambda returns true for any element from an array. @@ -222,15 +228,31 @@ an array. **Returns false if array is empty.** ```twig -{{ [1, 2, 3] is any(=> _ is even) ? "There is even element in the array." }} +{{ [1, 2, 3] is any(==> _ is even) ? "There is even element in the array." }} +{# prints 'There is even element in the array.' #} +``` + +---------------------------------------------------------------- + + +### |is_any +**Signature:** `array|is_any(lambda)` + +Returns true if lambda returns true for any element from an array. + +**Returns false if array is empty.** + +```twig +{{ [1, 2, 3]|is_any(==> _ is even) ? "There is even element in the array." }} {# prints 'There is even element in the array.' #} ``` ---------------------------------------------------------------- -### is every +### ~~is every~~ **Signature:** `array is every(lambda)` +##### deprecated since 1.1.0 and will be replaced in 2.0.0 by is_every function Returns true if lambda returns true for every element from an array. @@ -238,7 +260,23 @@ an array. **Returns true if array is empty.** ```twig -{{ [1, 2, 3] is every(=> _ > 0) ? "All elements in the array are positive." }} +{{ [1, 2, 3] is every(==> _ > 0) ? "All elements in the array are positive." }} +{# prints 'All elements in the array are positive.' #} +``` + +---------------------------------------------------------------- + + +### |is_every +**Signature:** `array|is_every(lambda)` +##### deprecated since 1.1.0 and will be replaced in 2.0.0 by is_every function + +Returns true if lambda returns true for every element from an array. + +**Returns true if array is empty.** + +```twig +{{ [1, 2, 3]|is_every(==> _ > 0) ? "All elements in the array are positive." }} {# prints 'All elements in the array are positive.' #} ``` @@ -255,8 +293,8 @@ This function is provided to allow creating twig macros taking lambda as an argument. ```twig -{{ call(=> _ * 2, [10]) }} +{{ call(==> _ * 2, [10]) }} {# prints '20' #} -{{ call(=> _.foo, [{foo: 12}]) }} +{{ call(==> _.foo, [{foo: 12}]) }} {# prints '12' #} ``` diff --git a/Tests/Fixtures/filters/any.test b/Tests/Fixtures/filters/any.test index bbef1de..ff880dc 100644 --- a/Tests/Fixtures/filters/any.test +++ b/Tests/Fixtures/filters/any.test @@ -1,10 +1,10 @@ --TEST-- "any" test --TEMPLATE-- -{{ data is any(=> _ > 6) ? 'YES':'NO' }} -{{ data is any(=> _ is even) ? 'YES':'NO' }} -{{ data is any(=> _ < 0) ? 'YES':'NO' }} -{{ data is any((_;i) => i is same as('foo')) ? 'YES':'NO' }} +{{ data|is_any(=> _ > 6) ? 'YES':'NO' }} +{{ data|is_any(=> _ is even) ? 'YES':'NO' }} +{{ data|is_any(=> _ < 0) ? 'YES':'NO' }} +{{ data|is_any((_;i) => i is same as('foo')) ? 'YES':'NO' }} --DATA-- return [ 'data' => [1, 2, 3, 4, 5, 'foo' => 6] ]; --EXPECT-- @@ -32,4 +32,4 @@ return [ 'data' => [0, 0, 1, 3] ]; NO YES NO -NO \ No newline at end of file +NO diff --git a/Tests/Fixtures/filters/every.test b/Tests/Fixtures/filters/every.test index 4387619..dc30a7e 100644 --- a/Tests/Fixtures/filters/every.test +++ b/Tests/Fixtures/filters/every.test @@ -1,10 +1,10 @@ --TEST-- "all" test --TEMPLATE-- -{{ data is every(=> _ > 6) ? 'YES':'NO' }} -{{ data is every(=> _ is even) ? 'YES':'NO' }} -{{ data is every(=> _ < 12) ? 'YES':'NO' }} -{{ data is every((_;i) => i >= 0) ? 'YES':'NO' }} +{{ data|is_every(=> _ > 6) ? 'YES':'NO' }} +{{ data|is_every(=> _ is even) ? 'YES':'NO' }} +{{ data|is_every(=> _ < 12) ? 'YES':'NO' }} +{{ data|is_every((_;i) => i >= 0) ? 'YES':'NO' }} --DATA-- return [ 'data' => [] ]; --EXPECT-- @@ -43,4 +43,4 @@ return [ 'data' => [ NO NO YES -NO \ No newline at end of file +NO diff --git a/composer.json b/composer.json index 56b0471..065467f 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "dpolac/twig-lambda", + "name": "leonaero/twig-lambda", "type": "library", "description": "Lambda expressions for Twig and filters that make use of them", "keywords": [ @@ -14,12 +14,6 @@ ], "minimum-stability": "dev", "license": "MIT", - "authors": [ - { - "name": "Damian Polac", - "email": "damian.polac.111@gmail.com" - } - ], "require": { "php": "^5.6 || ^7.0", "twig/twig": "^1.0 || ^2.0",