Completed
Branchmaster (48c558)
created

Template::process()   C

↳ Parent: Template

Complexity

Conditions 7
Paths 6

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 49
Code Lines 21

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 6
nop 1
1
<?php
2
namespace Redaxscript\Parser\Tag;
3
4
/**
5
 * children class to parse content for template tags
6
 *
7
 * @since 3.0.0
8
 *
9
 * @package Redaxscript
10
 * @category Parser
11
 * @author Henry Ruhs
12
 */
13
14
class Template extends TagAbstract
15
{
16
	/**
17
	 * options of the template tag
18
	 *
19
	 * @var array
20
	 */
21
22
	protected $_optionArray =
23
	[
24
		'search' =>
25
		[
26
			'<template>',
27
			'</template>'
28
		],
29
		'namespace' => 'Redaxscript\Template\Tag',
30
		'delimiter' => '@@@'
31
	];
32
33
	/**
34
	 * process the class
35
	 *
36
	 * @since 3.0.0
37
	 *
38
	 * @param string $content content to be parsed
39
	 *
40
	 * @return string
41
	 */
42
43
	public function process($content = null)
44
	{
45
		$output = str_replace($this->_optionArray['search'], $this->_optionArray['delimiter'], $content);
46
		$partArray = array_filter(explode($this->_optionArray['delimiter'], $output));
47
		$templateClass = $this->_optionArray['namespace'];
48
49
		/* parse as needed */
50
51
		foreach ($partArray as $key => $value)
52
		{
53
			if ($key % 2)
54
			{
55
				$partArray[$key] = null;
56
				$json = json_decode($value, true);
57
58
				/* call with parameter */
59
60
				if (is_array($json))
61
				{
62
					foreach ($json as $method => $parameterArray)
63
					{
64
						/* method exists */
65
66
						if (method_exists($templateClass, $method))
67
						{
68
							$partArray[$key] = call_user_func_array(
69
							[
70
								$templateClass,
71
								$method
72
							], $parameterArray);
73
						}
74
					}
75
				}
76
77
				/* else simple call */
78
79
				else if (method_exists($templateClass, $value))
80
				{
81
					$partArray[$key] = call_user_func(
82
					[
83
						$templateClass,
84
						$value
85
					]);
86
				}
87
			}
88
		}
89
		$output = implode($partArray);
90
		return $output;
91
	}
92
}