Passed
Push — master ( ef917e...ce4978 )
by Paul
05:49
created

ReviewHtml::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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