From 615bb6060231ed8412139d5bd0a8347924af630a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Mon, 18 Jul 2022 16:24:22 +0200 Subject: [PATCH] fix: Collection->column() (#140) --- src/Collection.php | 1 + src/Collection/ReadOnlyCollection.php | 35 +++++++++---------- .../site/config/packages/yassg_routes.yaml | 8 ++--- tests/functional/site/fixtures/de/index.html | 8 ++--- .../en/{a => article}/hello-world/index.html | 0 tests/functional/site/fixtures/en/index.html | 8 ++--- tests/functional/site/fixtures/hr/index.html | 8 ++--- tests/functional/site/fixtures/index.html | 2 +- .../site/templates/pages/homepage.html.twig | 10 +++--- 9 files changed, 36 insertions(+), 44 deletions(-) rename tests/functional/site/fixtures/en/{a => article}/hello-world/index.html (100%) diff --git a/src/Collection.php b/src/Collection.php index 820a81f..37d21dc 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -22,4 +22,5 @@ */ interface Collection extends \ArrayAccess, \Countable, \IteratorAggregate { + public function column(string $name): array; } diff --git a/src/Collection/ReadOnlyCollection.php b/src/Collection/ReadOnlyCollection.php index e2ade1f..4331955 100644 --- a/src/Collection/ReadOnlyCollection.php +++ b/src/Collection/ReadOnlyCollection.php @@ -33,20 +33,31 @@ public function __construct(ExpressionLanguage $expressionLanguage, array $names $this->data = $data; } - public function __get(string $name): array + public function __get(string $name): object { - return $this->map($name); + return $this->data[$name]; + } + + public function column(string $name): array + { + $expression = $this->expressionLanguage->parse($name, $this->names); + + $values = []; + foreach ($this->data as $id => $item) { + $values[$id] = $this->expressionLanguage->evaluate($expression, (array) $item); + } + + return $values; } public function offsetExists(mixed $offset): bool { - return isset($this->names[$offset]); + return isset($this->data[$offset]); } - /** @phpstan-ignore-next-line */ - public function offsetGet(mixed $offset): array + public function offsetGet(mixed $offset): object { - return $this->map($offset); + return $this->data[$offset]; } public function offsetSet(mixed $offset, mixed $value): void @@ -68,16 +79,4 @@ public function getIterator(): Traversable { return new \ArrayIterator($this->data); } - - private function map(string $name): array - { - $expression = $this->expressionLanguage->parse($name, $this->names); - - $values = []; - foreach ($this->data as $id => $item) { - $values[$id] = $this->expressionLanguage->evaluate($expression, (array) $item); - } - - return $values; - } } diff --git a/tests/functional/site/config/packages/yassg_routes.yaml b/tests/functional/site/config/packages/yassg_routes.yaml index 69d7af3..7a77da8 100644 --- a/tests/functional/site/config/packages/yassg_routes.yaml +++ b/tests/functional/site/config/packages/yassg_routes.yaml @@ -12,16 +12,16 @@ sigwin_yassg: path: /{_locale} catalog: # map results as an array - _locale: "yassg_find_all('locale')['isoCode']" + _locale: "yassg_find_all('locale').column('isoCode')" article: - path: /{_locale}/a/{slug} + path: /{_locale}/article/{slug} catalog: - slug: "yassg_find_all('articles').slug" + slug: "yassg_find_all('articles').column('slug')" product: path: /{_locale}/{slug} catalog: # map results as a property - slug: "yassg_find_all('products').slug" + slug: "yassg_find_all('products').column('slug')" product_with_route_default: path: /{_locale}/product_with_alias/{slug} defaults: diff --git a/tests/functional/site/fixtures/de/index.html b/tests/functional/site/fixtures/de/index.html index 2a71b09..d567018 100644 --- a/tests/functional/site/fixtures/de/index.html +++ b/tests/functional/site/fixtures/de/index.html @@ -15,7 +15,7 @@

Homepage

- +

Locales

- +

Products linked to categories slug: category1

  • @@ -38,15 +38,13 @@

    Products linked to categories slug: category1

    Beischpiel 1
- +

Category via ID

Kategorie 1

-

Bezeichnung 1

Category via slug

Kategorie 1

-

Bezeichnung 1

diff --git a/tests/functional/site/fixtures/en/a/hello-world/index.html b/tests/functional/site/fixtures/en/article/hello-world/index.html similarity index 100% rename from tests/functional/site/fixtures/en/a/hello-world/index.html rename to tests/functional/site/fixtures/en/article/hello-world/index.html diff --git a/tests/functional/site/fixtures/en/index.html b/tests/functional/site/fixtures/en/index.html index 0d8f3bc..518ac0c 100644 --- a/tests/functional/site/fixtures/en/index.html +++ b/tests/functional/site/fixtures/en/index.html @@ -15,7 +15,7 @@

Homepage

- +

Locales

- +

Products linked to categories slug: category1

  • @@ -38,15 +38,13 @@

    Products linked to categories slug: category1

    Example 1
- +

Category via ID

Category 1

-

Description 1

Category via slug

Category 1

-

Description 1

diff --git a/tests/functional/site/fixtures/hr/index.html b/tests/functional/site/fixtures/hr/index.html index 46203b6..fda2803 100644 --- a/tests/functional/site/fixtures/hr/index.html +++ b/tests/functional/site/fixtures/hr/index.html @@ -15,7 +15,7 @@

Homepage

- +

Locales

- +

Products linked to categories slug: category1

  • @@ -38,15 +38,13 @@

    Products linked to categories slug: category1

    Primjer 1
- +

Category via ID

Kategorija 1

-

Opis 1

Category via slug

Kategorija 1

-

Opis 1

diff --git a/tests/functional/site/fixtures/index.html b/tests/functional/site/fixtures/index.html index 42dc523..7f55625 100644 --- a/tests/functional/site/fixtures/index.html +++ b/tests/functional/site/fixtures/index.html @@ -34,7 +34,7 @@

Index

/sub/dir/another/en/
article
-
/sub/dir/another/en/a/hello-world/
+
/sub/dir/another/en/article/hello-world/
product
/sub/dir/another/en/nested-example/
diff --git a/tests/functional/site/templates/pages/homepage.html.twig b/tests/functional/site/templates/pages/homepage.html.twig index faecc4a..06fd9d7 100644 --- a/tests/functional/site/templates/pages/homepage.html.twig +++ b/tests/functional/site/templates/pages/homepage.html.twig @@ -3,7 +3,7 @@ {# query the database #} {% set locales = yassg_find_all('locale', {sort: {'item.name': 'asc'}}) %} -{% set products = yassg_find_all('products', {condition: '"category1" in item.categories.slug', sort: {'item.name': 'desc'}}) %} +{% set products = yassg_find_all('products', {condition: '"category1" in item.categories.column("slug")', sort: {'item.name': 'desc'}}) %} {% set category1 = yassg_get('categories', '/category1.yaml') %} @@ -16,7 +16,7 @@

Homepage

- +

Locales

    {% for locale in locales %} @@ -26,7 +26,7 @@ {% endfor %}
- +

Products linked to categories slug: category1

    {% for product in products %} @@ -35,15 +35,13 @@ {% endfor %}
- +

Category via ID

{{ category1.name }}

-

{{ category1.random() }}

{{ category1.getDescription() }}

Category via slug

{{ category1_via_slug.name }}

-

{{ category1_via_slug.random() }}

{{ category1_via_slug.getDescription() }}