Skip to content

Commit

Permalink
release of version 3.0.0 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
smajti1 authored Mar 3, 2020
1 parent ad7ef53 commit 7ba3729
Show file tree
Hide file tree
Showing 29 changed files with 83 additions and 528 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.idea
/composer.lock
/composer.phar
/vendor
.phpunit.result.cache
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ 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).

## [3.0.0] - 2020-02-
### Changed
- minimum php version is now 7.2
### Removed
- `|call` twig filter
- Lambda, all files in `LeonAero\NodeExpression\*` (use original twig lambda)
- `==>`, `;` twig operator

## [2.1.0] - 2020-02-24
### Deprecated
- `|call` twig filter
Expand Down Expand Up @@ -34,6 +42,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `is any` and `is every` twig test
- `=>`, `;` twig operator

[3.0.0]: https://github.com/leonaero/twig-lambda/compare/v3.0.0...v2.1.0
[2.1.0]: https://github.com/leonaero/twig-lambda/compare/v2.0.0...v2.1.0
[2.0.0]: https://github.com/leonaero/twig-lambda/compare/v1.1.0...v2.0.0
[1.1.0]: https://github.com/leonaero/twig-lambda/compare/v1.0.0...v1.1.0
Expand Down
74 changes: 11 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# Twig Lambda
> Lambda expressions for Twig and filters that make use of them

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

| Version | Twig Version | Php Version |
|---- |----|----|
| ^3.0 | ^3.0 | ^7.2 |
| ^2.0 | ^2.10 | ^7.0 |
| ^1.0 | ^1.0 || 2.9.* | ^5.6 || ^7.0 |

<a name="examples"></a>
## Quick examples

Listing names of all authors ordered by age:
Listing names of all authors ordered by youngest:
```twig
{% for author in articles|map(v => v.author)|unique_by('===')|sort_by(v => v.age) %}
{% set articles = [
{author: {name: 'Bar', age: 55}, text: 'Text...'},
{author: {name: 'Bar', age: 65}, text: 'Text...'},
{author: {name: 'Foo', age: 45}, text: 'Text...'},
{author: {name: 'Foo', age: 45}, text: 'Text...'},
] %}
{% for author in articles|map(v => v.author)|unique_by('===')|sort(v => v.age)|reverse %}
* {{ author.name }}, {{ author.age }}
{% endfor %}
```
Expand Down Expand Up @@ -53,46 +60,7 @@ services:
----------------------------------------------------------------
## Usage
<a name="lambda"></a>
### ~~Lambda expression~~
##### deprecated since 1.1.0 and will be remove in 3.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
any variable from the outside. There are also two special
variables available:
* `_` (single underscore) - first argument,
* `__` (double underscore) - array of arguments counted
from zero.

```
==> _.name
==> _ * 2
==> _|first
==> 'foobar'
==> _ is even
==> __[0] + __[1]
```

To create lambda expression with list of arguments, add it
before `==>` operator. Separate multiple arguments with
semicolons. You can use brackets for readability.

```
x ==> x + 1
(book) ==> book.author
arg1; arg2 ==> arg1 ~ arg2
(a; b; c) ==> a + b - c
```
Note that if you use list of arguments, `_` variable is not
longer available.
----------------------------------------------------------------
Below is a list of available filters and tests. All works
with arrays and any Traversable object and preserve it keys.
Below is a list of available filters. All works with arrays and any Traversable object and preserve it keys.
----------------------------------------------------------------
Expand Down Expand Up @@ -199,23 +167,3 @@ Returns true if lambda returns true for every element from an array.
{{ [1, 2, 3]|is_every(v => v > 0) ? "All elements in the array are positive." }}
{# prints 'All elements in the array are positive.' #}
```

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

<a name="call"></a>
### ~~call()~~
##### deprecated since 2.0.0
**Signature:** `call(lambda [, arguments:array])`

Calls lambda and returns its result. You can provide array
of arguments.

This function is provided to allow creating twig macros taking
lambda as an argument.

```twig
{{ call(v => v * 2, [10]) }}
{# prints '20' #}
{{ call(v => v.foo, [{foo: 12}]) }}
{# prints '12' #}
```
2 changes: 1 addition & 1 deletion Tests/Fixtures/filters/any.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{ data|is_any(v => v > 6) ? 'YES':'NO' }}
{{ data|is_any(v => v is even) ? 'YES':'NO' }}
{{ data|is_any(v => v < 0) ? 'YES':'NO' }}
{{ data|is_any((v; i) ==> i is same as('foo')) ? 'YES':'NO' }}
{{ data|is_any((v, i) => i is same as('foo')) ? 'YES':'NO' }}
--DATA--
return [ 'data' => [1, 2, 3, 4, 5, 'foo' => 6] ];
--EXPECT--
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/filters/count_by.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
"count_by" filter
--TEMPLATE--
{% for key, value in data|count_by(v ==> v.foo ) %}
{% for key, value in data|count_by(v => v.foo ) %}
{{ key }}: {{ value }}
{% endfor %}
--DATA--
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/filters/count_by_key.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
"count_by" filter counted with array key
--TEMPLATE--
{% for key, value in data|count_by((v; key) ==> key|first ) %}
{% for key, value in data|count_by((v, key) => key|first ) %}
{{ key }}: {{ value }}
{% endfor %}
--DATA--
Expand Down
8 changes: 4 additions & 4 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(v ==> v > 6) ? 'YES':'NO' }}
{{ data|is_every(v ==> v is even) ? 'YES':'NO' }}
{{ data|is_every(v ==> v < 12) ? 'YES':'NO' }}
{{ data|is_every((v; i) ==> i >= 0) ? 'YES':'NO' }}
{{ data|is_every(v => v > 6) ? 'YES':'NO' }}
{{ data|is_every(v => v is even) ? 'YES':'NO' }}
{{ data|is_every(v => v < 12) ? 'YES':'NO' }}
{{ data|is_every((v, i) => i >= 0) ? 'YES':'NO' }}
--DATA--
return [ 'data' => [] ];
--EXPECT--
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/filters/group_by.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
"group_by" filter
--TEMPLATE--
{% for key,items in data|group_by(v ==> v|first ) %}
{% for key,items in data|group_by(v => v|first ) %}
= {{ key }}
{% for i in items %}
* {{ i }}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/filters/group_by_key.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
"group_by" filter grouping with array key
--TEMPLATE--
{% for key,items in data|group_by((v; i) ==> i|first ) %}
{% for key,items in data|group_by((v, i) => i|first ) %}
= {{ key }}
{% for i in items %}
* {{ i }}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/filters/group_by_object.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
"group_by" filter can group by object
--TEMPLATE--
{% for category, articles in array|group_by(v ==> v.category) %}
{% for category, articles in array|group_by(v => v.category) %}
= {{ category.name }}
{% for article in articles %}
* {{ article.name }}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/filters/unique.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
"unique_by" filter
--TEMPLATE--
{% for i in data|unique_by((i1; i2) ==> i1 == i2) %}
{% for i in data|unique_by((i1, i2) => i1 == i2) %}
* {{ i }}
{% endfor %}
--DATA--
Expand Down
18 changes: 18 additions & 0 deletions Tests/Fixtures/filters/unique_by.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
"unique_by" filter
--TEMPLATE--
{% set articles = [
{author: {name: 'Bar', age: 55}, text: 'Text...'},
{author: {name: 'Bar', age: 65}, text: 'Text...'},
{author: {name: 'Foo', age: 45}, text: 'Text...'},
{author: {name: 'Foo', age: 45}, text: 'Text...'},
] %}
{% for author in articles|map(v => v.author)|unique_by('===')|sort(v => v.age)|reverse %}
* {{ author.name }}, {{ author.age }}
{% endfor %}
--DATA--
return [];
--EXPECT--
* Bar, 55
* Foo, 45
* Bar, 65
2 changes: 1 addition & 1 deletion Tests/Fixtures/filters/unique_object.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
"unique_by" filter works with list of objects
--TEMPLATE--
{% for i in list|unique_by((i1; i2) ==> i1 is same as(i2)) %}
{% for i in list|unique_by((i1, i2) => i1 is same as(i2)) %}
* {{ i.data }}
{% endfor %}
--DATA--
Expand Down
12 changes: 0 additions & 12 deletions Tests/Fixtures/lambda/assigned_to_var.test

This file was deleted.

15 changes: 0 additions & 15 deletions Tests/Fixtures/lambda/call_passed_function.test

This file was deleted.

16 changes: 0 additions & 16 deletions Tests/Fixtures/lambda/does_not_bleed.test

This file was deleted.

12 changes: 0 additions & 12 deletions Tests/Fixtures/lambda/mutiple_arguments.test

This file was deleted.

12 changes: 0 additions & 12 deletions Tests/Fixtures/lambda/no_arguments.test

This file was deleted.

48 changes: 0 additions & 48 deletions Tests/Fixtures/lambda/operator_precedence.test

This file was deleted.

19 changes: 0 additions & 19 deletions Tests/Fixtures/lambda/passed_to_macro.test

This file was deleted.

16 changes: 0 additions & 16 deletions Tests/Fixtures/lambda/preserve_context.test

This file was deleted.

Loading

0 comments on commit 7ba3729

Please sign in to comment.