FilterAction.php 1.3 KB

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