2021-10-08 16:23:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Values;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
2022-07-29 06:47:10 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2021-10-08 16:23:45 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
final class SmartPlaylistRuleGroup implements Arrayable
|
|
|
|
{
|
2022-07-27 08:49:33 +00:00
|
|
|
private function __construct(public ?int $id, public Collection $rules)
|
|
|
|
{
|
|
|
|
}
|
2021-10-08 16:23:45 +00:00
|
|
|
|
2021-10-10 18:05:51 +00:00
|
|
|
public static function tryCreate(array $jsonArray): ?self
|
2021-10-08 16:23:45 +00:00
|
|
|
{
|
|
|
|
try {
|
2021-10-10 18:05:51 +00:00
|
|
|
return self::create($jsonArray);
|
2022-07-27 08:49:33 +00:00
|
|
|
} catch (Throwable) {
|
2021-10-08 16:23:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:05:51 +00:00
|
|
|
public static function create(array $jsonArray): self
|
|
|
|
{
|
2022-07-27 08:49:33 +00:00
|
|
|
$rules = collect(array_map(static function (array $rawRuleConfig) {
|
2021-10-10 18:05:51 +00:00
|
|
|
return SmartPlaylistRule::create($rawRuleConfig);
|
|
|
|
}, $jsonArray['rules']));
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
return new self(Arr::get($jsonArray, 'id'), $rules);
|
2021-10-10 18:05:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
/** @return array<mixed> */
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'rules' => $this->rules->toArray(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|