Passed
Push — master ( 37d5c2...da7383 )
by Richard
03:04 queued 12s
created

AutoParagraphs::autoParagraphs()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 5
rs 9.6111
1
<?php
2
3
4
namespace Riclep\Storyblok\Traits;
5
6
trait AutoParagraphs
7
{
8
	protected $autoParagraphs = [];
9
10
	/**
11
	 * Loops over every field in $autoParagraphs and creates a duplicate
12
	 * field suffixed with _html containing html paragraphs
13
	 */
14
	private function initAutoParagraphs() {
15
		if (!empty($this->autoParagraphs)) {
16
			foreach ($this->autoParagraphs as $field) {
17
				if ($this->content->has($field)) {
18
					$this->content[$field . '_html'] = $this->autoParagraph($this->content[$field]);
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
				}
20
			}
21
		}
22
	}
23
24
	/**
25
	 * Performs the actual transformation
26
	 *
27
	 * @param $text
28
	 * @return string
29
	 */
30
	private function autoParagraph($text) {
31
		$paragraphs = explode("\n", $text);
32
		return '<p>' . implode('</p><p>', array_filter($paragraphs)) . '</p>';
33
	}
34
}