PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Rules / DisplayRules.php

DisplayRules.php in Depicter — Popup & Slider Builder trunk, at app/src/Rules/DisplayRules.php

183 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Depicter\Rules;
4
5 use Averta\Core\Utility\Data;
6 use Averta\Core\Utility\JSON;
7 use Depicter\Rules\DisplayCondition\Mapper;
8
9 /**
10 * Class to parsing DisplayRules of a document
11 *
12 * @property array|null $displayConditions Get displayConditions of current document in array
13 * @property array|null $displayConditionsSlim Get displayConditions of current document without redundant group and condition IDs in array
14 */
15 class DisplayRules {
16
17 /**
18 * Document ID
19 *
20 * @var int
21 */
22 protected $documentID;
23
24 /**
25 * Stores DisplayRules
26 *
27 * @var array
28 */
29 protected $rules = [];
30
31 /**
32 * Stores DisplayConditions
33 *
34 * @var array
35 */
36 protected $displayConditionsInArray = [];
37
38
39 /**
40 * Stores DisplayConditions
41 *
42 * @var Mapper
43 */
44 protected $displayConditionsMapper;
45
46
47
48 public function __construct( $documentID ){
49 $this->documentID = $documentID;
50 $this->rules['raw'] = \Depicter::metaRepository()->get( $documentID, 'rules', '');
51 }
52
53 /**
54 * Retrieves RAW DisplayRules
55 *
56 * @return mixed
57 */
58 public function raw(){
59 return $this->rules['raw'];
60 }
61
62 /**
63 * Get displayRules in array format
64 *
65 * @return array
66 */
67 public function toArray(){
68 if( !empty( $this->rules['array'] ) ){
69 return $this->rules['array'];
70 }
71
72 if ( JSON::isJson( $this->raw() ) ) {
73 $this->rules['array'] = JSON::decode( $this->raw(), true );
74 }
75
76 return $this->rules['array'] ?? [];
77 }
78
79 /**
80 * Get displayRules in object format
81 *
82 * @return object
83 */
84 public function get(){
85 if( !empty( $this->rules['object'] ) ){
86 return $this->rules['object'];
87 }
88
89 if ( JSON::isJson( $this->raw() ) ) {
90 $this->rules['object'] = JSON::decode( $this->raw(), false );
91 }
92
93 return $this->rules['object'] ?? new \stdClass();
94 }
95
96 /**
97 * Stores displayRules for the document
98 *
99 * @param $content
100 *
101 * @return false|mixed
102 */
103 public function set( $content ){
104 $result = \Depicter::metaRepository()->update( $this->documentID, 'rules', $content );
105
106 $this->rules = [];
107 $this->rules['raw'] = \Depicter::metaRepository()->get( $this->documentID, 'rules', '');
108
109 return $result;
110 }
111
112 /**
113 * Get value of a defined property
114 *
115 * @param $name
116 *
117 * @return array|null
118 */
119 public function __get( $name ){
120
121 // Get all displayConditions of current document in array
122 if( $name === 'displayConditions' ){
123 if( !empty( $this->displayConditionsInArray ) ){
124 return $this->displayConditionsInArray;
125 }
126 $displayConditions = $this->get()->displayConditions ?? [];
127 $this->displayConditionsInArray = $displayConditions ? Data::cast( $displayConditions, 'array' ) : $displayConditions;
128
129 return $this->displayConditionsInArray;
130
131 // Get all displayConditions of current document without redundant group and condition IDs
132 } elseif( $name === 'displayConditionsSlim' ){
133 // remove all redundant displayGroup IDs
134 $displayConditions = array_values( $this->displayConditions );
135 // remove all redundant condition IDs
136 return array_map( function( $displayConditionGroup ){
137 if( is_array( $displayConditionGroup['conditions'] ) ){
138 $displayConditionGroup['conditions'] = array_values( $displayConditionGroup['conditions'] );
139 }
140 return $displayConditionGroup;
141 }, $displayConditions );
142 }
143
144 return null;
145 }
146
147 /**
148 * Get displayConditionsMapper instance containing all parsed data of displayConditions
149 *
150 * @return Mapper
151 */
152 public function displayConditions(){
153 if( empty( $this->displayConditionsMapper ) ){
154 $this->displayConditionsMapper = new Mapper( $this->displayConditionsSlim );
155 }
156 return $this->displayConditionsMapper;
157 }
158
159 /**
160 * Whether the document can be visible or not
161 *
162 * @param array $args params
163 *
164 * @return bool
165 */
166 public function isVisible( $args = [] ){
167 $rules = $this->get();
168
169 if ( !empty( $rules->visibilitySchedule ) && !empty( $rules->visibilitySchedule->enable ) ) {
170 $visibilityTime = $rules->visibilitySchedule;
171 if ( !empty( $visibilityTime->start ) && ! \Depicter::schedule()->isDatePassed( $visibilityTime->start ) ) {
172 return false;
173 }
174
175 if ( !empty( $visibilityTime->end ) && \Depicter::schedule()->isDatePassed( $visibilityTime->end ) ) {
176 return false;
177 }
178 }
179
180 return true;
181 }
182 }
183