Passed
Push — master ( 817cab...57b58d )
by Paul
09:30 queued 02:21
created

ReviewHtml::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

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