- <template name="vo_collection" value="/** * @var $ITEM_CLASS$[] */ private $items; public static function fromArray(array $items): self { return new self(...array_map(function ($TYPE$ $item) { return $ITEM_CLASS$::from$TYPE_METHOD$($item); }, $items)); } public static function fromItems($ITEM_CLASS$ ...$items): self { return new self(...$items); } public static function emptyList(): self { return new self([]); } private function __construct($ITEM_CLASS$ ...$items) { $this->items = $items; } public function push($ITEM_CLASS$ $item): self { $copy = clone $this; $copy->items[] = $item; return $copy; } public function pop(): self { $copy = clone $this; \array_pop($copy->items); return $copy; } public function first(): ?$ITEM_CLASS$ { return $this->items[0] ?? null; } public function last(): ?$ITEM_CLASS$ { if(count($this->items) === 0) { return null; } return $this->items[count($this->items) - 1]; } public function contains($ITEM_CLASS$ $item): bool { foreach($this->items as $existingItem) { if($existingItem->equals($item)) { return true; } } return false; } /** * @return $ITEM_CLASS$[] */ public function items(): array { return $this->items; } public function toArray(): array { return \array_map(function ($ITEM_CLASS$ $item) { return $item->to$TYPE_METHOD$(); }, $this->items); } public function equals($other): bool { if(!$other instanceof self) { return false; } return $this->toArray() === $other->toArray(); } public function __toString(): string { return \json_encode($this->toArray()); }" description="Collection value object" toReformat="true" toShortenFQNames="true">
0 commit comments