| 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 |
|