1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Defaults\SiteReviewsDefaults; |
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
9
|
|
|
use GeminiLabs\SiteReviews\Review; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @property string $avatar |
13
|
|
|
* @property string $content |
14
|
|
|
* @property string $date |
15
|
|
|
* @property string $author |
16
|
|
|
* @property int $rating |
17
|
|
|
* @property string $response |
18
|
|
|
* @property string $title |
19
|
|
|
* etc. |
20
|
|
|
*/ |
21
|
|
|
class ReviewHtml extends \ArrayObject |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public $args; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $context; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Review |
35
|
|
|
*/ |
36
|
|
|
public $review; |
37
|
|
|
|
38
|
6 |
|
public function __construct(Review $review, array $args = []) |
39
|
|
|
{ |
40
|
6 |
|
$this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
41
|
6 |
|
$this->context = $this->buildContext($review); |
42
|
6 |
|
$this->review = $review; |
43
|
6 |
|
parent::__construct($this->context, \ArrayObject::STD_PROP_LIST|\ArrayObject::ARRAY_AS_PROPS); |
44
|
6 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string|void |
48
|
|
|
*/ |
49
|
6 |
|
public function __toString() |
50
|
|
|
{ |
51
|
6 |
|
if (empty($this->context)) { |
52
|
|
|
return ''; |
53
|
|
|
} |
54
|
6 |
|
return glsr(Template::class)->build('templates/review', [ |
55
|
6 |
|
'args' => $this->args, |
56
|
6 |
|
'context' => $this->context, |
57
|
6 |
|
'review' => $this->review, |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
6 |
|
public function buildContext(Review $review) |
65
|
|
|
{ |
66
|
6 |
|
glsr()->action('review/build/before', $review, $this); |
67
|
6 |
|
$templateTags = []; |
68
|
6 |
|
$assignedTag = array_filter([ |
69
|
6 |
|
'assigned_posts' => $review->assigned_posts, |
70
|
6 |
|
'assigned_terms' => $review->assigned_terms, |
71
|
6 |
|
'assigned_users' => $review->assigned_users, |
72
|
|
|
]); |
73
|
6 |
|
$templateTags['assigned'] = json_encode($assignedTag); |
74
|
6 |
|
foreach ($review as $key => $value) { |
75
|
6 |
|
$tag = $this->normalizeTemplateTag($key); |
76
|
6 |
|
$templateTags[$tag] = $this->buildTemplateTag($review, $tag, $value); |
77
|
|
|
} |
78
|
6 |
|
return glsr()->filterArray('review/build/after', $templateTags, $review, $this); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $tag |
83
|
|
|
* @param string|array $value |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
6 |
|
public function buildTemplateTag(Review $review, $tag, $value) |
87
|
|
|
{ |
88
|
6 |
|
$args = $this->args; |
89
|
6 |
|
$tagSlug = implode('-', ['review', $tag, 'tag']); |
90
|
6 |
|
$className = Helper::buildClassName($tagSlug, 'Modules\Html\Tags'); |
91
|
6 |
|
$className = glsr()->filterString('review/tag/'.$tag, $className); |
92
|
6 |
|
$field = class_exists($className) |
93
|
6 |
|
? glsr($className, compact('tag', 'args'))->handleFor('review', $value, $review) |
94
|
6 |
|
: Cast::toString($value, false); |
95
|
6 |
|
return glsr()->filterString('review/build/tag/'.$tag, $field, $value, $review, $this); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param mixed $key |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function offsetGet($key) |
103
|
|
|
{ |
104
|
|
|
if (array_key_exists($key, $this->context)) { |
105
|
|
|
return $this->context[$key]; |
106
|
|
|
} |
107
|
|
|
$key = Helper::ifTrue('values' === $key, 'context', $key); // @deprecated in v5.0 |
108
|
|
|
return Helper::ifTrue(property_exists($this, $key), $this->$key); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $tag |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
6 |
|
protected function normalizeTemplateTag($tag) |
116
|
|
|
{ |
117
|
|
|
$mappedTags = [ |
118
|
6 |
|
'ID' => 'review_id', |
119
|
|
|
]; |
120
|
6 |
|
return Arr::get($mappedTags, $tag, $tag); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|