Skip to content

Commit

Permalink
1.1.0 (#1)
Browse files Browse the repository at this point in the history
release of version 1.1.0
  • Loading branch information
smajti1 authored Feb 12, 2020
1 parent 9138b85 commit 5f35abd
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 53 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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


57 changes: 39 additions & 18 deletions LambdaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -38,7 +47,7 @@ public function getOperators()
]
];
}

public function getFunctions()
{
return [
Expand Down Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -242,7 +263,7 @@ public static function countBy($array, $callback)
}
return $result;
}

public static function every($array, $callback)
{
if (!is_callable($callback)) {
Expand All @@ -263,7 +284,7 @@ public static function every($array, $callback)

return true;
}

public static function any($array, $callback)
{
if (!is_callable($callback)) {
Expand Down Expand Up @@ -297,4 +318,4 @@ public function getName()
{
return 'dpolac_lambda_extension';
}
}
}
74 changes: 56 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
```
Expand All @@ -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:**
Expand All @@ -50,9 +50,11 @@ services:
## Usage
<a name="lambda"></a>
### 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,
Expand Down Expand Up @@ -91,30 +93,32 @@ All lambdas are called with two arguments: element and key.
----------------------------------------------------------------
<a name="map"></a>
### |map
### ~~|map~~
**Alias:** `|select`<br>
**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 %}
```

----------------------------------------------------------------

<a name="filter"></a>
### |filter
### ~~|filter~~
**Alias:** `|where`<br>
**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 %}
```
Expand Down Expand Up @@ -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 }}
Expand All @@ -176,12 +180,13 @@ will produce
<a name="sort_by"></a>
### |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 %}
```
Expand All @@ -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 %}
```
Expand All @@ -213,32 +218,65 @@ will produce
----------------------------------------------------------------

<a name="any"></a>
### 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.

**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.' #}
```

----------------------------------------------------------------

<a name="is_any"></a>
### |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.' #}
```

----------------------------------------------------------------

<a name="every"></a>
### 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.

**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.' #}
```

----------------------------------------------------------------

<a name="is_every"></a>
### |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.' #}
```

Expand All @@ -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' #}
```
10 changes: 5 additions & 5 deletions Tests/Fixtures/filters/any.test
Original file line number Diff line number Diff line change
@@ -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--
Expand Down Expand Up @@ -32,4 +32,4 @@ return [ 'data' => [0, 0, 1, 3] ];
NO
YES
NO
NO
NO
10 changes: 5 additions & 5 deletions Tests/Fixtures/filters/every.test
Original file line number Diff line number Diff line change
@@ -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--
Expand Down Expand Up @@ -43,4 +43,4 @@ return [ 'data' => [
NO
NO
YES
NO
NO
Loading

0 comments on commit 5f35abd

Please sign in to comment.