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 / DisplayCondition / Mapper.php

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

124 lines 2.9 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\DisplayCondition;
4
5 use Averta\Core\Utility\Arr;
6 use Averta\WordPress\Utility\JSON;
7 use Depicter\Exception\EntityException;
8
9 /**
10 * Hydrates displayConditions of a document in corresponding classes to process conditions
11 */
12 class Mapper {
13
14 /**
15 * @var mixed
16 */
17 private $displayConditionsSlim;
18
19 /**
20 * @var DisplayConditionGroup[]
21 */
22 private $displayConditionGroups = [];
23
24
25 /**
26 * Constructor
27 */
28 public function __construct( $displayConditionsSlim ){
29 $this->displayConditionsSlim = $displayConditionsSlim;
30 $this->displayConditionGroups = $this->hydrate( $this->displayConditionsSlim );
31 }
32
33 /**
34 * Hydrates DisplayConditions data into appropriate classes
35 */
36 private function hydrate( $displayConditionsSlim ){
37 // convert displayConditions to object
38 $displayConditionsSlim = is_array( $displayConditionsSlim ) ? JSON::decode( JSON::encode( $this->displayConditionsSlim ), false ) : $displayConditionsSlim;
39
40 $groups = [];
41
42 try{
43 foreach( $displayConditionsSlim as $displayConditionGroup ){
44 $mapper = new \JsonMapper();
45 $mapper->classMap['\Depicter\Rules\Condition\Base'] = function ( $class, $conditionParams ){
46 // Skip if slug of condition cannot be found
47 if( empty( $conditionParams->slug ) || !strpos( $conditionParams->slug , '_') ){
48 return $class;
49 }
50
51 // find exclusive class of each Condition by its corresponding slug
52 [ $groupID, $className ] = explode('_', $conditionParams->slug );
53 if ( strpos( $className, '|' ) ) {
54 $className = explode( '|', $className )[0];
55 }
56
57 return "\\Depicter\\Rules\\Condition\\{$groupID}\\{$className}";
58 };
59
60 $groups[] = $mapper->map( $displayConditionGroup, new DisplayConditionGroup() );
61 }
62 } catch( EntityException|\JsonMapper_Exception $e ){}
63
64 return $groups;
65 }
66
67 /**
68 * Get all DisplayConditionGroups and corresponding conditions in
69 *
70 * @return DisplayConditionGroup[]
71 */
72 public function groups(){
73 return $this->displayConditionGroups;
74 }
75
76 public function all(){
77 $conditions = [];
78
79 if( ! empty( $this->displayConditionGroups ) ){
80 foreach( $this->displayConditionGroups as $displayConditionGroup ){
81 $conditions = Arr::merge( $displayConditionGroup->conditions(), $conditions );
82 }
83 }
84
85 return $conditions;
86 }
87
88 /**
89 * Checks if all conditions in groups are passed or not
90 *
91 * @param $value
92 *
93 * @return bool
94 */
95 public function areMet( $value = null ){
96
97 $currentOperator = 'and';
98 $overAllCheck = true;
99
100 if( ! empty( $this->displayConditionGroups ) ){
101
102 foreach( $this->displayConditionGroups as $displayConditionGroup ){
103 $currentGroupResult = $displayConditionGroup->check( $value );
104 if( $currentOperator === 'and' ){
105 if( ! $currentGroupResult ){
106 return false;
107 }
108 $overAllCheck = true;
109 } else {
110 if( $currentGroupResult ){
111 return true;
112 }
113 }
114
115 $currentOperator = $displayConditionGroup->operator;
116 }
117
118 }
119
120 return $overAllCheck;
121 }
122
123 }
124