Test Failed
Push — develop ( 7aea1a...9fd874 )
by Paul
15:27
created

ReviewHtml::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0116
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
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsShortcode;
11
12
/**
13
 * @property string $avatar
14
 * @property string $content
15
 * @property string $date
16
 * @property string $author
17
 * @property int    $rating
18
 * @property string $response
19
 * @property string $title
20
 */
21
class ReviewHtml extends \ArrayObject
22
{
23
    public array $args;
24
    public array $context;
25
    public Review $review;
26
27
    protected array $attributes = [];
28
29 8
    public function __construct(Review $review, array $args = [])
30
    {
31 8
        $this->args = glsr(SiteReviewsDefaults::class)->unguardedMerge($args);
32 8
        $this->context = $this->buildContext($review);
33 8
        $this->review = $review;
34 8
        parent::__construct($this->context, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
35
    }
36
37 8
    public function __toString(): string
38
    {
39 8
        if (empty($this->context)) {
40
            return '';
41
        }
42 8
        return glsr(Template::class)->build('templates/review', [
43 8
            'args' => $this->args,
44 8
            'context' => $this->context,
45 8
            'review' => $this->review,
46 8
        ]);
47
    }
48
49
    public function attributes(): array
50
    {
51
        if (empty($this->attributes)) {
52
            $this->attributes = glsr(SiteReviewsShortcode::class)->attributes($this->args);
53
        }
54
        return $this->attributes;
55
    }
56
57 8
    public function buildContext(Review $review): array
58
    {
59 8
        $context = $this->buildTemplateTags($review);
60 8
        return glsr()->filterArray('review/build/context', $context, $review, $this);
61
    }
62
63
    /**
64
     * @param string|array $value
65
     */
66 8
    public function buildTemplateTag(Review $review, string $tag, $value): string
67
    {
68 8
        $args = $this->args;
69 8
        $className = Helper::buildClassName(['review', $tag, 'tag'], 'Modules\Html\Tags');
70 8
        $className = glsr()->filterString("review/tag/{$tag}", $className, $this);
71 8
        $field = class_exists($className)
72 8
            ? glsr($className, compact('tag', 'args'))->handleFor('review', $value, $review)
73 8
            : Cast::toString($value, false);
74 8
        return glsr()->filterString("review/build/tag/{$tag}", $field, $value, $review, $this);
75
    }
76
77 8
    public function buildTemplateTags(Review $review): array
78
    {
79 8
        glsr()->action('review/build/before', $review, $this);
80 8
        $templateTags = [];
81 8
        $assignedTag = array_filter([
82 8
            'assigned_posts' => $review->assigned_posts,
83 8
            'assigned_terms' => $review->assigned_terms,
84 8
            'assigned_users' => $review->assigned_users,
85 8
        ]);
86 8
        $templateTags['assigned'] = wp_json_encode($assignedTag);
87 8
        $values = $review->toArray();
88 8
        foreach ($values as $key => $value) {
89 8
            $tag = $this->normalizeTemplateTag($key);
90 8
            $templateTags[$tag] = $this->buildTemplateTag($review, $tag, $value);
91
        }
92 8
        return glsr()->filterArray('review/build/after', $templateTags, $review, $this);
93
    }
94
95
    /**
96
     * @param string $key
97
     *
98
     * @return mixed
99
     */
100
    #[\ReturnTypeWillChange]
101
    public function offsetGet($key)
102
    {
103
        if ('attributes' === $key) {
104
            return glsr(Attributes::class)->div($this->attributes())->toString();
105
        }
106
        if (array_key_exists($key, $this->context)) {
107
            return $this->context[$key];
108
        }
109
        return property_exists($this, $key)
110
            ? $this->$key
111
            : glsr()->filterString("review/html/{$key}", null, $this);
112
    }
113
114 8
    protected function normalizeTemplateTag(string $tag): string
115
    {
116 8
        $mappedTags = [
117 8
            'ID' => 'review_id',
118 8
            'is_verified' => 'verified',
119 8
        ];
120 8
        return Arr::get($mappedTags, $tag, $tag);
121
    }
122
}
123