Passed
Push — master ( 507915...bc62fe )
by Gabriel
03:45
created

AssetCollection::setAssetType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\Assets\Assets;
4
5
use Nip\Collections\Typed\ClassCollection;
6
7
/**
8
 * Class AssetCollection
9
 * @package ByTIC\Assets\Assets
10
 */
11
class AssetCollection extends ClassCollection
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $validClass = Asset::class;
17
18
    protected $assetType = '';
19
20
    /**
21
     * @inheritDoc
22
     */
23 1
    public function offsetSet($offset, $value)
24
    {
25 1
        $this->ensureItem($value);
26 1
        parent::offsetSet($offset, $value);
27 1
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 1
    public function unshift($value, $key = null)
33
    {
34 1
        $this->ensureItem($value);
35 1
        parent::unshift($value, $key);
36 1
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 1
    public function add($source, $name = null)
42
    {
43 1
        parent::add($this->newItem($source), $name);
44 1
    }
45
46
    /**
47
     * @param $content
48
     */
49
    public function addRaw($content)
50
    {
51
        $this->getInternal()->appendContent($content);
52
    }
53
54
    /**
55
     * @return string
56
     */
57 3
    public function getAssetType(): string
58
    {
59 3
        return $this->assetType;
60
    }
61
62
    /**
63
     * @param string $type
64
     */
65 2
    public function setAssetType(string $type): void
66
    {
67 2
        $this->assetType = $type;
68 2
    }
69
    /**
70
     * @return string
71
     */
72
    public function __toString()
73
    {
74
        return $this->render();
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function render()
81
    {
82
        $output = '';
83
        $items = $this->all();
84
        unset($items['_internal']);
85
        foreach ($items as $item) {
86
            $output .= $item->output();
87
        }
88
        return $output;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function renderRaw()
95
    {
96
        return $this->getInternal()->output();
97
    }
98
99
    /**
100
     * @return Asset
101
     */
102
    protected function getInternal()
103
    {
104
        if (!$this->has('_internal')) {
105
            $asset = $this->newItem('');
106
            $this->set('_internal', $asset);
107
            return $asset;
108
        }
109
        return $this->get('_internal');
110
    }
111
112
    /**
113
     * @param $value
114
     */
115 2
    protected function ensureItem(&$value)
116
    {
117 2
        if ($value instanceof Asset) {
118 1
            return;
119
        }
120 1
        $value = $this->newItem($value);
121 1
    }
122
123
    /**
124
     * @param $value
125
     * @return Asset
126
     */
127 2
    protected function newItem($value)
128
    {
129 2
        return new Asset($value, $this->getAssetType());
130
    }
131
}
132