Skip to content

Commit

Permalink
feat: Reorg storage naming
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSkroblin committed Jan 15, 2024
1 parent 930530d commit 986b0a4
Show file tree
Hide file tree
Showing 43 changed files with 1,014 additions and 990 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use Shopware\Storage\Common\Document\Document;
use Shopware\Storage\Common\Document\Documents;
use Shopware\Storage\Common\KeyValue\KeyValueStorage;
use Shopware\Storage\Common\KeyValue\KeyAware;
use Shopware\Storage\Common\Storage;

class ArrayKeyValueStorage implements KeyValueStorage
class ArrayKeyStorage implements KeyAware, Storage
{
/**
* @var array<string, Document>
Expand Down
57 changes: 43 additions & 14 deletions src/Array/ArrayFilterStorage.php → src/Array/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use Shopware\Storage\Common\Document\Document;
use Shopware\Storage\Common\Document\Documents;
use Shopware\Storage\Common\Filter\FilterStorage;
use Shopware\Storage\Common\Filter\FilterCriteria;
use Shopware\Storage\Common\Filter\FilterResult;
use Shopware\Storage\Common\Filter\FilterAware;
use Shopware\Storage\Common\Filter\Criteria;
use Shopware\Storage\Common\Filter\Paging\Limit;
use Shopware\Storage\Common\Filter\Result;
use Shopware\Storage\Common\Filter\Operator\AndOperator;
use Shopware\Storage\Common\Filter\Operator\NandOperator;
use Shopware\Storage\Common\Filter\Operator\NorOperator;
Expand All @@ -25,12 +26,15 @@
use Shopware\Storage\Common\Filter\Type\Not;
use Shopware\Storage\Common\Filter\Type\Prefix;
use Shopware\Storage\Common\Filter\Type\Suffix;
use Shopware\Storage\Common\KeyValue\KeyAware;
use Shopware\Storage\Common\Schema\FieldType;
use Shopware\Storage\Common\Schema\Schema;
use Shopware\Storage\Common\Schema\SchemaUtil;
use Shopware\Storage\Common\Storage;
use Shopware\Storage\Common\StorageContext;
use Shopware\Storage\Common\Total;

class ArrayFilterStorage implements FilterStorage
class ArrayStorage extends ArrayKeyStorage implements FilterAware
{
/**
* @var array<string, Document>
Expand All @@ -44,6 +48,26 @@ public function setup(): void
$this->storage = [];
}

public function get(string $key): ?Document
{
return $this->storage[$key] ?? null;
}

public function mget(array $keys): Documents
{
$documents = new Documents();

foreach ($keys as $key) {
if (!isset($this->storage[$key])) {
continue;
}

$documents->set($key, $this->storage[$key]);
}

return $documents;
}

public function remove(array $keys): void
{
foreach ($keys as $key) {
Expand All @@ -58,12 +82,12 @@ public function store(Documents $documents): void
}
}

public function filter(FilterCriteria $criteria, StorageContext $context): FilterResult
public function filter(Criteria $criteria, StorageContext $context): Result
{
$filtered = $this->storage;

if ($criteria->keys) {
$filtered = array_filter($filtered, fn($key) => in_array($key, $criteria->keys, true), ARRAY_FILTER_USE_KEY);
if ($criteria->primaries) {
$filtered = array_filter($filtered, fn($key) => in_array($key, $criteria->primaries, true), ARRAY_FILTER_USE_KEY);
}

if ($criteria->filters) {
Expand All @@ -77,16 +101,21 @@ public function filter(FilterCriteria $criteria, StorageContext $context): Filte
$total = count($filtered);

if ($criteria->paging instanceof Page) {
$filtered = array_slice($filtered, ($criteria->paging->page - 1) * $criteria->limit);
$filtered = array_slice($filtered, ($criteria->paging->page - 1) * $criteria->paging->limit, $criteria->paging->limit);
} elseif ($criteria->paging instanceof Limit) {
$filtered = array_slice($filtered, 0, $criteria->paging->limit);
}

if ($criteria->limit) {
$filtered = array_slice($filtered, 0, $criteria->limit);
switch ($criteria->total) {
case Total::NONE:
$total = null;
break;
case Total::EXACT:
case Total::MORE:
break;
}

$total = $criteria->total ? $total : null;

return new FilterResult(elements: $filtered, total: $total);
return new Result(elements: $filtered, total: $total);
}

/**
Expand Down Expand Up @@ -311,7 +340,7 @@ private function resolveAccessor(string $accessor, mixed $value): mixed
* @param Document[] $filtered
* @return Document[]
*/
private function sort(array $filtered, FilterCriteria $criteria, StorageContext $context): array
private function sort(array $filtered, Criteria $criteria, StorageContext $context): array
{
$filtered = array_values($filtered);

Expand Down
10 changes: 0 additions & 10 deletions src/Common/Aggregation/AggregateStorage.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/Common/Aggregation/Aggregation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Shopware\Storage\Common\Aggregation;

interface Aggregation
{
public function name(): string;
}
21 changes: 21 additions & 0 deletions src/Common/Aggregation/AggregationAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Shopware\Storage\Common\Aggregation;

use Shopware\Storage\Common\Filter\Criteria;
use Shopware\Storage\Common\StorageContext;

interface AggregationAware
{
/**
* @param Aggregation[] $aggregations
* @param Criteria $criteria
* @param StorageContext $context
* @return Aggregations
*/
public function aggregate(
array $aggregations,
Criteria $criteria,
StorageContext $context
): Aggregations;
}
21 changes: 0 additions & 21 deletions src/Common/Aggregation/AggregationCriteria.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
use Shopware\Storage\Common\Filter\Operator\Operator;
use Shopware\Storage\Common\Filter\Paging\Paging;
use Shopware\Storage\Common\Filter\Type\Filter;
use Shopware\Storage\Common\Search\SearchTerm;
use Shopware\Storage\Common\Total;

class FilterCriteria
class Criteria
{
/**
* @param array<string> $keys
* @param array<string> $primaries
* @param array<Sorting> $sorting
* @param array<Operator|Filter> $filters
*/
public function __construct(
public ?SearchTerm $term = null,
public ?Paging $paging = null,
public ?int $limit = null,
public ?array $keys = null,
public bool $total = false,
public ?array $primaries = null,
public Total $total = Total::NONE,
public array $sorting = [],
public array $filters = []
) {}
Expand Down
10 changes: 10 additions & 0 deletions src/Common/Filter/FilterAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Shopware\Storage\Common\Filter;

use Shopware\Storage\Common\StorageContext;

interface FilterAware
{
public function filter(Criteria $criteria, StorageContext $context): Result;
}
16 changes: 0 additions & 16 deletions src/Common/Filter/FilterStorage.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Common/Filter/Paging/Limit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Shopware\Storage\Common\Filter\Paging;

class Limit implements Paging
{
public function __construct(
public int $limit
) {}
}
5 changes: 4 additions & 1 deletion src/Common/Filter/Paging/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

class Page implements Paging
{
public function __construct(public int $page) {}
public function __construct(
public int $page,
public int $limit
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Shopware\Storage\Common\Document\Documents;

class FilterResult extends Documents
class Result extends Documents
{
public function __construct(array $elements, public ?int $total = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Shopware\Storage\Common\Document\Documents;
use Shopware\Storage\Common\Storage;

interface KeyValueStorage extends Storage
interface KeyAware
{
/**
* @param array<string> $keys
Expand Down
5 changes: 3 additions & 2 deletions src/Common/Schema/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ class Field
{
/**
* @param array<Field> $fields
* @param array{translated?: bool, searchable?: bool, sortable?: bool, filterable?: bool} $options
*/
public function __construct(
public string $name,
public string $type,
public bool $translated = false,
public array $fields = []
public array $options = [],
public array $fields = [],
) {
// map fields to use the field name as array key
$this->fields = array_combine(
Expand Down
9 changes: 2 additions & 7 deletions src/Common/Schema/SchemaUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function translated(Schema $schema, string $accessor): bool
{
$schema = self::fieldSchema(schema: $schema, accessor: $accessor);

return $schema->translated;
return $schema->options['translated'] ?? false;
}

public static function type(Schema $schema, string $accessor): string
Expand All @@ -72,12 +72,7 @@ public static function fieldSchema(Schema $schema, string $accessor): Field
$property = self::property(accessor: $accessor);

if ($property === 'key') {
return new Field(
name: 'key',
type: FieldType::STRING,
translated: false,
fields: [],
);
return new Field(name: 'key', type: FieldType::STRING);
}

$field = $schema->fields[$property] ?? null;
Expand Down
8 changes: 0 additions & 8 deletions src/Common/Search/InterpretedTerm.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Common/Search/SearchAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Shopware\Storage\Common\Search;

use Shopware\Storage\Common\Filter\Criteria;
use Shopware\Storage\Common\Filter\Result;
use Shopware\Storage\Common\StorageContext;

interface SearchAware
{
public function search(Criteria $criteria, StorageContext $context): Result;
}
32 changes: 0 additions & 32 deletions src/Common/Search/SearchCriteria.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/Common/Search/SearchStorage.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Common/Search/SearchTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class SearchTerm
{
/**
* @param InterpretedTerm[] $terms
* @param string[] $terms
*/
public function __construct(public array $terms) {}
}
Loading

0 comments on commit 986b0a4

Please sign in to comment.