koel/app/Values/SmartPlaylistRuleGroup.php

43 lines
994 B
PHP
Raw Normal View History

<?php
namespace App\Values;
use Illuminate\Contracts\Support\Arrayable;
2022-07-29 06:47:10 +00:00
use Illuminate\Support\Arr;
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)
{
}
public static function tryCreate(array $jsonArray): ?self
{
try {
return self::create($jsonArray);
2022-07-27 08:49:33 +00:00
} catch (Throwable) {
return null;
}
}
public static function create(array $jsonArray): self
{
2022-07-27 08:49:33 +00:00
$rules = collect(array_map(static function (array $rawRuleConfig) {
return SmartPlaylistRule::create($rawRuleConfig);
}, $jsonArray['rules']));
2022-07-29 06:47:10 +00:00
return new self(Arr::get($jsonArray, 'id'), $rules);
}
/** @return array<mixed> */
public function toArray(): array
{
return [
'id' => $this->id,
'rules' => $this->rules->toArray(),
];
}
}