PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.19.6
GiveWP – Donation Plugin and Fundraising Platform v2.19.6
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Framework / FieldsAPI / Concerns / HasVisibilityConditions.php
HasVisibilityConditions.php
106 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\FieldsAPI\Concerns;
4
5 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
6 use Give\Framework\FieldsAPI\Conditions\BasicCondition;
7
8 /**
9 * @since 2.13.0
10 */
11 trait HasVisibilityConditions
12 {
13
14 /**
15 * The node is visible by default. These are the conditions for it to remain visible.
16 *
17 * @since 2.13.0
18 *
19 * @var BasicCondition[]
20 */
21 protected $visibilityConditions = [];
22
23 /**
24 * Get the visibility conditions.
25 *
26 * @since 2.13.0
27 *
28 * @return BasicCondition[]
29 */
30 public function getVisibilityConditions()
31 {
32 return $this->visibilityConditions;
33 }
34
35 /**
36 * @since 2.16.0
37 *
38 * @return boolean
39 */
40 public function hasVisibilityConditions()
41 {
42 return ! empty($this->visibilityConditions);
43 }
44
45 /**
46 * Set a condition for showing the node.
47 *
48 * @since 2.13.0
49 *
50 * @param string $field
51 * @param string $operator
52 * @param mixed $value
53 * @param string $boolean
54 *
55 * @return $this
56 */
57 public function showIf($field, $operator, $value, $boolean = 'and')
58 {
59 $this->visibilityConditions[] = new BasicCondition($field, $operator, $value, $boolean);
60
61 return $this;
62 }
63
64 /**
65 * Set multiple conditions for showing the node.
66 *
67 * @since 2.13.0
68 *
69 * @param BasicCondition|array ...$conditions
70 *
71 * @return $this
72 */
73 public function showWhen(...$conditions)
74 {
75 foreach ($conditions as $condition) {
76 $this->visibilityConditions[] = $this->normalizeCondition($condition);
77 }
78
79 return $this;
80 }
81
82 /**
83 * Normalize the condition if in array format.
84 *
85 * @since 2.13.0
86 *
87 * @param BasicCondition|array $condition
88 *
89 * @return BasicCondition
90 *
91 * @throws InvalidArgumentException
92 */
93 protected function normalizeCondition($condition)
94 {
95 if ($condition instanceof BasicCondition) {
96 return $condition;
97 }
98
99 if (is_array($condition)) {
100 return new BasicCondition(...$condition);
101 }
102
103 throw new InvalidArgumentException('Parameter $condition must be a BasicCondition or an array.');
104 }
105 }
106