Skip to content

Commit

Permalink
fix: Collection->column() (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarlovi authored Jul 18, 2022
1 parent fc5e052 commit 615bb60
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
*/
interface Collection extends \ArrayAccess, \Countable, \IteratorAggregate
{
public function column(string $name): array;
}
35 changes: 17 additions & 18 deletions src/Collection/ReadOnlyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions tests/functional/site/config/packages/yassg_routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions tests/functional/site/fixtures/de/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Homepage</h1>

<h2>Locales</h2>
<ul>
<li>
Expand All @@ -28,7 +28,7 @@ <h2>Locales</h2>
<a href="/sub/dir/another/hr/">Hrvatski</a>
</li>
</ul>

<h2>Products linked to categories <code>slug: category1</code></h2>
<ul>
<li>
Expand All @@ -38,15 +38,13 @@ <h2>Products linked to categories <code>slug: category1</code></h2>
<a href="https://example.com/sub/dir/another/de/beischpiel1/">Beischpiel 1</a>
</li>
</ul>

<h2>Category via ID</h2>
<h3>Kategorie 1</h3>
<p></p>
<p>Bezeichnung 1</p>

<h2>Category via slug</h2>
<h3>Kategorie 1</h3>
<p></p>
<p>Bezeichnung 1</p>
</div>
</div>
Expand Down
8 changes: 3 additions & 5 deletions tests/functional/site/fixtures/en/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Homepage</h1>

<h2>Locales</h2>
<ul>
<li>
Expand All @@ -28,7 +28,7 @@ <h2>Locales</h2>
<a href="/sub/dir/another/hr/">Hrvatski</a>
</li>
</ul>

<h2>Products linked to categories <code>slug: category1</code></h2>
<ul>
<li>
Expand All @@ -38,15 +38,13 @@ <h2>Products linked to categories <code>slug: category1</code></h2>
<a href="https://example.com/sub/dir/another/en/example1/">Example 1</a>
</li>
</ul>

<h2>Category via ID</h2>
<h3>Category 1</h3>
<p></p>
<p>Description 1</p>

<h2>Category via slug</h2>
<h3>Category 1</h3>
<p></p>
<p>Description 1</p>
</div>
</div>
Expand Down
8 changes: 3 additions & 5 deletions tests/functional/site/fixtures/hr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Homepage</h1>

<h2>Locales</h2>
<ul>
<li>
Expand All @@ -28,7 +28,7 @@ <h2>Locales</h2>
<a href="/sub/dir/another/hr/">Hrvatski</a>
</li>
</ul>

<h2>Products linked to categories <code>slug: category1</code></h2>
<ul>
<li>
Expand All @@ -38,15 +38,13 @@ <h2>Products linked to categories <code>slug: category1</code></h2>
<a href="https://example.com/sub/dir/another/hr/primjer1/">Primjer 1</a>
</li>
</ul>

<h2>Category via ID</h2>
<h3>Kategorija 1</h3>
<p></p>
<p>Opis 1</p>

<h2>Category via slug</h2>
<h3>Kategorija 1</h3>
<p></p>
<p>Opis 1</p>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/site/fixtures/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1>Index</h1>
<dd><a href="/sub/dir/another/en/">/sub/dir/another/en/</a></dd>
<dt>article</dt>

<dd><a href="/sub/dir/another/en/a/hello-world/">/sub/dir/another/en/a/hello-world/</a></dd>
<dd><a href="/sub/dir/another/en/article/hello-world/">/sub/dir/another/en/article/hello-world/</a></dd>
<dt>product</dt>

<dd><a href="/sub/dir/another/en/nested-example/">/sub/dir/another/en/nested-example/</a></dd>
Expand Down
10 changes: 4 additions & 6 deletions tests/functional/site/templates/pages/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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') %}

Expand All @@ -16,7 +16,7 @@
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Homepage</h1>

<h2>Locales</h2>
<ul>
{% for locale in locales %}
Expand All @@ -26,7 +26,7 @@
</li>
{% endfor %}
</ul>

<h2>Products linked to categories <code>slug: category1</code></h2>
<ul>
{% for product in products %}
Expand All @@ -35,15 +35,13 @@
</li>
{% endfor %}
</ul>

<h2>Category via ID</h2>
<h3>{{ category1.name }}</h3>
<p>{{ category1.random() }}</p>
<p>{{ category1.getDescription() }}</p>

<h2>Category via slug</h2>
<h3>{{ category1_via_slug.name }}</h3>
<p>{{ category1_via_slug.random() }}</p>
<p>{{ category1_via_slug.getDescription() }}</p>
</div>
</div>
Expand Down

0 comments on commit 615bb60

Please sign in to comment.