Passed
Push — master ( 090ea5...57cca1 )
by Richard
03:41 queued 12s
created

RichText::init()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 21
rs 9.2222
1
<?php
2
3
4
namespace Riclep\Storyblok\Fields;
5
6
7
use Riclep\Storyblok\Field;
8
use Riclep\Storyblok\Traits\HasChildClasses;
9
use Storyblok\RichtextRender\Resolver;
10
11
class RichText extends Field
12
{
13
	use HasChildClasses;
14
15
	public function init() {
16
		$richtextResolver = new Resolver();
17
18
		$content = [];
19
20
		// Loop through all the nodes looking for a ‘blok’ nodes and convery them to
21
		// the correct Block Class. All other nodes are converted to HTML
22
		foreach ($this->content['content'] as $node) {
23
			if ($node['type'] === 'blok' && isset($node['attrs']['body']) && is_array($node['attrs']['body'])) {
24
				foreach ($node['attrs']['body'] as $blockContent) {
25
					$class = $this->getChildClassName('Block', $blockContent['component']);
26
					$block = new $class($blockContent, $this->block());
27
28
					$content[] = $block;
29
				}
30
			} else {
31
				$content[] = $richtextResolver->render(["content" => [$node]]);
32
			}
33
		}
34
35
		$this->content = collect($content);
36
	}
37
38
	/**
39
	 * Converts the data to HTML when printed. If there is an inline Component
40
	 * it will use it’s render method.
41
	 *
42
	 * @return string
43
	 */
44
	public function __toString()
45
	{
46
		$html = "";
47
48
		foreach ($this->content as $content) {
49
			if (is_string($content)) {
50
				$html .= $content;
51
			} else {
52
				$html .= $content->render();
53
			}
54
		}
55
56
		return $html;
57
	}
58
}