FilterAction.php 1.4 KB

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