2021-10-08 16:23:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Values;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
final class SmartPlaylistRuleGroup implements Arrayable
|
|
|
|
{
|
|
|
|
public ?int $id;
|
|
|
|
|
|
|
|
/** @var Collection|array<SmartPlaylistRule> */
|
|
|
|
public Collection $rules;
|
|
|
|
|
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);
|
2021-10-08 16:23:45 +00:00
|
|
|
} catch (Throwable $exception) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:05:51 +00:00
|
|
|
public static function create(array $jsonArray): self
|
|
|
|
{
|
|
|
|
$group = new self();
|
|
|
|
$group->id = $jsonArray['id'] ?? null;
|
|
|
|
|
|
|
|
$group->rules = collect(array_map(static function (array $rawRuleConfig) {
|
|
|
|
return SmartPlaylistRule::create($rawRuleConfig);
|
|
|
|
}, $jsonArray['rules']));
|
|
|
|
|
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
/** @return array<mixed> */
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'rules' => $this->rules->toArray(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|