From 862bf3cac53b3d8552ff5138cd2df5c489770655 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 2 Dec 2016 11:46:42 +0700 Subject: [PATCH] Add filter "filter" for CmsTwigExtension. --- src/Extension/CmsTwigExtension.php | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Extension/CmsTwigExtension.php b/src/Extension/CmsTwigExtension.php index 5a2175b..80219c2 100644 --- a/src/Extension/CmsTwigExtension.php +++ b/src/Extension/CmsTwigExtension.php @@ -96,7 +96,8 @@ public function getFunctions() public function getFilters() { return [ - new \Twig_SimpleFilter('content', [$this, 'content']) + new \Twig_SimpleFilter('content', [$this, 'content']), + new \Twig_SimpleFilter('filter', [$this, 'listFilter']) ]; } @@ -173,7 +174,7 @@ public function isEditable() $this->editable && $session->has('api_token') && $cmsRestClient->validateToken($session->get('api_token')) - ; + ; } /** @@ -208,4 +209,24 @@ public function cmsAsset($resource) { return $this->cmsBaseUrl . '/' . $resource; } -} \ No newline at end of file + + /** + * @param array $list + * @param array $filters + * @return array + */ + public function listFilter(array $list, array $filters) + { + return array_filter($list, function ($item) use ($filters) { + foreach ($filters as $filter) { + foreach ($filter as $filterName => $filterValue) { + if (!isset($item[$filterName]) || $item[$filterName] != $filterValue) { + return false; + } + + return true; + } + } + }); + } +}