Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove property filter as string to promote expressions #102

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions doc/loading-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,17 @@ $articles = $contentManager->getContents(
When passing multiple requirements, all must be met:

```php
$myDrafts = $this->manager->getContents(
$myDrafts = $contentManager->getContents(
Article::class,
null,
['author' => 'ogizanagi', 'draft' => true]
);
```

#### A property name (string)

```php
// Equivalent to ['active' => true]
$users = $contentManager->getContents(User::class, 'active');
```

#### A custom callable supported by the PHP [usort](https://www.php.net/manual/fr/function.usort.php) function

```php
$tagedMobileArticles = $this->manager->getContents(
$tagedMobileArticles = $contentManager->getContents(
Article::class,
null,
fn (Article $article): bool => in_array('mobile', $article->tags)
Expand All @@ -213,14 +206,21 @@ $tagedMobileArticles = $this->manager->getContents(
```php
use function Stenope\Bundle\ExpressionLanguage\expr;

$tagedMobileArticles = $this->manager->getContents(
$tagedMobileArticles = $contentManager->getContents(
Article::class,
null,
expr('"mobile" in _.tags')
);
```

// expr() and exprOr() are optional syntax sugar,
// but you can also provide the expression as a string directly:
$activeUsers = $contentManager->getContents(User::class, '_.active'); // Equivalent to ['active' => true]
$activeDevUsers = $contentManager->getContents(User::class, '_.active and _.dev');
```

!!! Note
`expr` accepts multiple expressions it'll combine using `and`.
Use `exprOr` to combine expressions using `or`.

See the [ExpressionLanguage syntax](https://symfony.com/doc/current/components/expression_language/syntax.html).
You may also want to extend the expression language capabilities for your own contents by [registering a custom expression provider](https://symfony.com/doc/current/components/expression_language/extending.html#using-expression-providers) tagged with `stenope.expression_language_provider`.
Expand All @@ -235,10 +235,6 @@ Built-in functions are:
- starts_with
- ends_with

!!! Note
`expr` accepts multiple expressions it'll combine using `and`.
Use `exprOr` to combine expressions using `or`.

## Debug

See [CLI - Debug](./cli.md#debug)
Expand Down
8 changes: 8 additions & 0 deletions doc/twig.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ Get all active job offers:
<!-- ... -->
{% endfor %}
```

using an expression filter:

```twig
{% for offer in content_list('App\\Model\\JobOffer', { date: true }, '_.active') %}
<!-- ... -->
{% endfor %}
```
12 changes: 4 additions & 8 deletions src/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public function __construct(
*
* @param class-string<T> $type Model FQCN e.g. "App/Model/Article"
* @param string|array|callable $sortBy String, array or callable
* @param string|array|callable|Expression $filterBy String, array, callable or an {@link Expression} instance to filter out
* with an expression using the ExpressionLanguage component.
* @param string|array|callable|Expression $filterBy Array, callable or an {@link Expression} instance / string
* to filter out with an expression using the ExpressionLanguage
* component.
*
* @return array<string,T> List of decoded contents with their slug as key
*/
Expand Down Expand Up @@ -287,19 +288,14 @@ private function getFilterFunction($filterBy): ?callable
return null;
}

if ($filterBy instanceof Expression) {
if ($filterBy instanceof Expression || \is_string($filterBy)) {
return fn ($data) => $this->expressionLanguage->evaluate($filterBy, [
'data' => $data,
'd' => $data,
'_' => $data,
]);
}

if (\is_string($filterBy)) {
// Check if the property passed as a string is true-ish:
return $this->getFilterFunction([$filterBy => true]);
}

if (\is_callable($filterBy)) {
return $filterBy;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/ContentManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public function testGetContents(): void
self::assertSame([
'foo2' => 'Foo 2',
], $getResults($manager->getContents('App\Foo', null, expr('_.content === "Foo 2"'))), 'filtered using an expression');

self::assertSame([
'foo2' => 'Foo 2',
], $getResults($manager->getContents('App\Foo', null, '_.content === "Foo 2"')), 'filtered using an expression directly provided as string');
}

public function testReverseContent(): void
Expand Down