FilterAction.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_FilterAction {
  4. /** @var list<string>|null */
  5. private ?array $actions = null;
  6. /** @param array<string> $actions */
  7. private function __construct(private readonly FreshRSS_BooleanSearch $booleanSearch, array $actions) {
  8. $this->_actions($actions);
  9. }
  10. public function booleanSearch(): FreshRSS_BooleanSearch {
  11. return $this->booleanSearch;
  12. }
  13. /** @return list<string> */
  14. public function actions(): array {
  15. return $this->actions ?? [];
  16. }
  17. /** @param array<string> $actions */
  18. public function _actions(?array $actions): void {
  19. if (is_array($actions)) {
  20. $this->actions = array_values(array_unique($actions));
  21. } else {
  22. $this->actions = null;
  23. }
  24. }
  25. /** @return array{search?:string,actions?:array<string>} */
  26. public function toJSON(): array {
  27. if (is_array($this->actions) && $this->booleanSearch != null) {
  28. return [
  29. 'search' => $this->booleanSearch->__toString(),
  30. 'actions' => $this->actions,
  31. ];
  32. }
  33. return [];
  34. }
  35. /** @param array|mixed|null $json */
  36. public static function fromJSON($json): ?FreshRSS_FilterAction {
  37. if (is_array($json) && !empty($json['search']) && is_string($json['search']) &&
  38. !empty($json['actions']) && is_array($json['actions']) && is_array_values_string($json['actions'])) {
  39. return new FreshRSS_FilterAction(new FreshRSS_BooleanSearch($json['search']), $json['actions']);
  40. }
  41. return null;
  42. }
  43. }