diff --git a/doc/loading-content.md b/doc/loading-content.md index 3c761217..2dd67555 100644 --- a/doc/loading-content.md +++ b/doc/loading-content.md @@ -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) @@ -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`. @@ -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) diff --git a/doc/twig.md b/doc/twig.md index fd07d138..ba886cc5 100644 --- a/doc/twig.md +++ b/doc/twig.md @@ -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 %} +``` diff --git a/src/ContentManager.php b/src/ContentManager.php index c0c34640..17dbcb7a 100644 --- a/src/ContentManager.php +++ b/src/ContentManager.php @@ -82,8 +82,9 @@ public function __construct( * * @param class-string $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 List of decoded contents with their slug as key */ @@ -287,7 +288,7 @@ 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, @@ -295,11 +296,6 @@ private function getFilterFunction($filterBy): ?callable ]); } - 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; } diff --git a/tests/Unit/ContentManagerTest.php b/tests/Unit/ContentManagerTest.php index cede4315..51585a6c 100644 --- a/tests/Unit/ContentManagerTest.php +++ b/tests/Unit/ContentManagerTest.php @@ -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