Remove illuminate/collections
This commit is contained in:
parent
275103e149
commit
419d1cec14
6 changed files with 89 additions and 260 deletions
42
src/Presentation/Collection/AbstractCollection.php
Normal file
42
src/Presentation/Collection/AbstractCollection.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Presentation\Collection;
|
||||
|
||||
use ArrayIterator;
|
||||
use Countable;
|
||||
use Iterator;
|
||||
use IteratorAggregate;
|
||||
|
||||
abstract class AbstractCollection implements Countable, IteratorAggregate
|
||||
{
|
||||
public function __construct(protected array $items = [])
|
||||
{
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->items);
|
||||
}
|
||||
|
||||
public function filter(callable $callback): self
|
||||
{
|
||||
return new static(
|
||||
array_filter(
|
||||
array: $this->items,
|
||||
callback: $callback,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function getIterator(): Iterator
|
||||
{
|
||||
return new ArrayIterator($this->items);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
}
|
9
src/Presentation/Collection/EventCollection.php
Normal file
9
src/Presentation/Collection/EventCollection.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Presentation\Collection;
|
||||
|
||||
final class EventCollection extends AbstractCollection
|
||||
{
|
||||
}
|
34
src/Presentation/Collection/PresentationCollection.php
Normal file
34
src/Presentation/Collection/PresentationCollection.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Presentation\Collection;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
||||
|
||||
final class PresentationCollection extends AbstractCollection
|
||||
{
|
||||
public function getAllEvents(): EventCollection
|
||||
{
|
||||
$events = array_reduce(
|
||||
array: $this->items,
|
||||
callback: function (array $events, ProxySourceItem $presentation): array {
|
||||
$events[] = $presentation->data()->get('events');
|
||||
|
||||
return $events;
|
||||
},
|
||||
initial: [],
|
||||
);
|
||||
|
||||
return new EventCollection(array_merge(...$events));
|
||||
}
|
||||
|
||||
public function getPastEvents(): EventCollection
|
||||
{
|
||||
$today = new DateTimeImmutable('today');
|
||||
|
||||
return $this->getAllEvents()
|
||||
->filter(fn(array $event) => $event['date'] < $today->getTimestamp());
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Presentation\TwigExtension;
|
||||
|
||||
use App\Presentation\Collection\PresentationCollection;
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
@ -22,17 +23,8 @@ class PresentationTwigExtension extends AbstractExtension
|
|||
|
||||
public function getPresentationCount(array $presentations): int
|
||||
{
|
||||
$today = (new \DateTime('today'))->getTimestamp();
|
||||
$presentationCollection = new PresentationCollection($presentations);
|
||||
|
||||
return collect($presentations)
|
||||
->flatMap(fn (ProxySourceItem $presentation) => $presentation->data()->get('events'))
|
||||
->filter(
|
||||
function (array $event) use ($today): bool {
|
||||
assert(array_key_exists(array: $event, key: 'date'));
|
||||
|
||||
return $event['date'] < $today;
|
||||
}
|
||||
)
|
||||
->count();
|
||||
return $presentationCollection->getPastEvents()->count();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue