48 lines
1.1 KiB
PHP
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();
|
|
}
|
|
}
|