-
Notifications
You must be signed in to change notification settings - Fork 5
/
SelectizeRenderer.php
126 lines (105 loc) · 3.75 KB
/
SelectizeRenderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Charcoal\Admin\Service;
use Exception;
// From PSR-3
use Psr\Log\LoggerAwareTrait;
// From 'charcoal-cms'
use Charcoal\Cms\TemplateableTrait;
// From 'charcoal-factory'
use Charcoal\Factory\FactoryInterface;
// From 'charcoal-core'
use Charcoal\Model\ModelInterface;
// From 'charcoal-translator'
use Charcoal\Translator\TranslatorAwareTrait;
// From 'charcoal-view'
use Charcoal\View\ViewInterface;
/**
* Renders a template given the object meta or template controller.
*
* Selectize renderer service
*/
class SelectizeRenderer
{
use TranslatorAwareTrait;
use TemplateableTrait;
use LoggerAwareTrait;
/**
* Template Factory
*
* @var FactoryInterface
*/
private $templateFactory;
/**
* @var ViewInterface
*/
private $view;
/**
* PHP 5 allows developers to declare constructor methods for classes.
* Classes which have a constructor method call this method on each newly-created object,
* so it is suitable for any initialization that the object may need before it is used.
*
* Note: Parent constructors are not called implicitly if the child class defines a constructor.
* In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
*
* @param array $data Dependencies.
* @throws Exception If missing dependencies.
* @return self
* @link http://php.net/manual/en/language.oop5.decon.php
*/
public function __construct(array $data)
{
if (!isset($data['logger'])) {
throw new Exception(
'You must set the logger in the Exporter Constructor.'
);
}
$this->logger = $data['logger'];
$this->templateFactory = $data['template_factory'];
$this->view = $data['view'];
$this->setTranslator($data['translator']);
return $this;
}
/**
* @param string $templateIdent The templateIdent as string.
* @param ModelInterface $object The ObjectType for context.
* @param string|null $controllerIdent The ControllerIdent string to override Object context.
* @throws \InvalidArgumentException If the callable id not callable.
* @return string
*/
public function renderTemplate($templateIdent, ModelInterface $object, $controllerIdent = null)
{
$template = null;
if ($controllerIdent && is_string($controllerIdent)) {
$controllerIdent = explode('::', $controllerIdent);
$controllerCallable = isset($controllerIdent[1]) ? $controllerIdent[1] : null;
$controllerIdent = $controllerIdent[0];
$template = $this->templateFactory->create($controllerIdent);
if ($controllerCallable) {
$method = [ $template, $controllerCallable ];
if (!is_callable($method)) {
throw new \InvalidArgumentException(sprintf(
'%s::%s supplied in %s::%s is not a callable method.',
$controllerIdent,
$controllerCallable,
__CLASS__,
__FUNCTION__
));
}
call_user_func($method, $object);
} elseif (is_callable($template)) {
$template($object);
} else {
throw new \InvalidArgumentException(sprintf(
'%s supplied in %s::%s is not callable.',
get_class($template),
__CLASS__,
__FUNCTION__
));
}
}
if (!$template) {
$template = $object;
}
return $this->view->render($templateIdent, $template);
}
}