-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add list object for sorting, filtering and pagination
- Loading branch information
1 parent
f3c1607
commit 70b6bb3
Showing
16 changed files
with
524 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Qossmic\TwigDocBundle\Cache; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Qossmic\TwigDocBundle\Service\ComponentService; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
|
||
class ComponentsWarmer implements CacheWarmerInterface | ||
{ | ||
public function __construct(private readonly ContainerInterface $container) | ||
{ | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
{ | ||
$componentService ??= $this->container->get('twig_doc.service.component'); | ||
|
||
if ($componentService instanceof ComponentService) { | ||
$componentService->getComponents(); | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Qossmic\TwigDocBundle\Component; | ||
|
||
/** | ||
* @method ComponentItem[] getArrayCopy() | ||
*/ | ||
class ComponentItemList extends \ArrayObject | ||
{ | ||
public const SORT_ASC = 'asc'; | ||
public const SORT_DESC = 'desc'; | ||
|
||
private array $sortableFields = [ | ||
'name', | ||
'category', | ||
'title', | ||
]; | ||
|
||
/** | ||
* @param ComponentItem[] $items | ||
*/ | ||
public function __construct(array $items) | ||
{ | ||
parent::__construct($items); | ||
} | ||
|
||
/** | ||
* @return ComponentItem[] | ||
*/ | ||
public function paginate(int $start = 0, int $limit = 15): array | ||
{ | ||
return \array_slice($this->getArrayCopy(), $start, $limit); | ||
} | ||
|
||
public function sort(string $field, string $direction = self::SORT_ASC): void | ||
{ | ||
if (!\in_array($field, $this->sortableFields)) { | ||
throw new \InvalidArgumentException(sprintf('field "%s" is not sortable', $field)); | ||
} | ||
|
||
$method = sprintf('get%s', ucfirst($field)); | ||
|
||
$this->uasort(function (ComponentItem $item, ComponentItem $item2) use ($method, $direction) { | ||
if ($direction === self::SORT_DESC) { | ||
return \call_user_func([$item2, $method]) <=> \call_user_func([$item, $method]); | ||
} | ||
|
||
return \call_user_func([$item, $method]) <=> \call_user_func([$item2, $method]); | ||
}); | ||
} | ||
|
||
public function filter(string $query, ?string $type): self | ||
{ | ||
$components = []; | ||
switch ($type) { | ||
case 'category': | ||
$components = array_filter( | ||
$this->getArrayCopy(), | ||
function (ComponentItem $item) use ($query) { | ||
$category = $item->getCategory()->getName(); | ||
$parent = $item->getCategory()->getParent(); | ||
while ($parent !== null) { | ||
$category = $parent->getName(); | ||
$parent = $parent->getParent(); | ||
} | ||
|
||
return strtolower($category) === strtolower($query); | ||
} | ||
); | ||
|
||
break; | ||
case 'sub_category': | ||
$components = array_filter( | ||
$this->getArrayCopy(), | ||
fn (ComponentItem $item) => $item->getCategory()->getParent() !== null | ||
&& strtolower($item->getCategory()->getName()) === strtolower($query) | ||
); | ||
|
||
break; | ||
case 'tags': | ||
$tags = array_map('trim', explode(',', strtolower($query))); | ||
$components = array_filter($this->getArrayCopy(), function (ComponentItem $item) use ($tags) { | ||
return array_intersect($tags, array_map('strtolower', $item->getTags())) !== []; | ||
}); | ||
|
||
break; | ||
case 'name': | ||
$components = array_filter( | ||
$this->getArrayCopy(), | ||
fn (ComponentItem $item) => str_contains(strtolower($item->getName()), strtolower($query)) | ||
); | ||
|
||
break; | ||
default: | ||
foreach (['category', 'sub_category', 'tags', 'name'] as $type) { | ||
$components = array_merge($components, (array) $this->filter($query, $type)); | ||
} | ||
|
||
break; | ||
} | ||
|
||
return new self(array_unique($components, \SORT_REGULAR)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.