aboutsummaryrefslogtreecommitdiff
path: root/app/Models/FilterAction.php
blob: e9445608690ea1ff1ded70636bc42422d8979464 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

class FreshRSS_FilterAction {

	/** @var FreshRSS_BooleanSearch */
	private $booleanSearch = null;
	/** @var array<string>|null */
	private $actions = null;

	/** @param array<string> $actions */
	private function __construct(FreshRSS_BooleanSearch $booleanSearch, array $actions) {
		$this->booleanSearch = $booleanSearch;
		$this->_actions($actions);
	}

	public function booleanSearch(): FreshRSS_BooleanSearch {
		return $this->booleanSearch;
	}

	/** @return array<string> */
	public function actions(): array {
		return $this->actions ?? [];
	}

	/** @param array<string> $actions */
	public function _actions(?array $actions): void {
		if (is_array($actions)) {
			$this->actions = array_unique($actions);
		} else {
			$this->actions = null;
		}
	}

	/** @return array{'search'?:string,'actions'?:array<string>} */
	public function toJSON(): array {
		if (is_array($this->actions) && $this->booleanSearch != null) {
			return [
				'search' => $this->booleanSearch->getRawInput(),
				'actions' => $this->actions,
			];
		}
		return [];
	}

	/** @param array|mixed|null $json */
	public static function fromJSON($json): ?FreshRSS_FilterAction {
		if (!empty($json['search']) && !empty($json['actions']) && is_array($json['actions'])) {
			return new FreshRSS_FilterAction(new FreshRSS_BooleanSearch($json['search']), $json['actions']);
		}
		return null;
	}
}