Files
kupshop/bundles/KupShop/PreordersBundle/Util/UserPreorderCustomData.php
2025-08-02 16:30:27 +02:00

48 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\PreordersBundle\Util;
use Doctrine\Common\Collections\ArrayCollection;
use KupShop\PreordersBundle\Entity\UserPreorder;
/**
* @extends ArrayCollection<string, mixed>
*/
class UserPreorderCustomData extends ArrayCollection implements \JsonSerializable, PersistentCollection
{
public function __construct(
private readonly UserPreorder $userPreorder,
) {
parent::__construct($this->fetchCustomData());
}
public function save(): ?\Throwable
{
try {
UserPreordersDataUtil::upsert($this->userPreorder, customData: $this->toArray());
} catch (\Throwable $e) {
return $e;
}
return null;
}
private function fetchCustomData(): array
{
try {
$data = UserPreordersDataUtil::get($this->userPreorder);
} catch (\Throwable) {
return [];
}
return $data['custom_data'] ?? [];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}