This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Partial.php
95 lines (84 loc) · 2.46 KB
/
Partial.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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\View\Helper;
use Zend\View\Exception;
use Zend\View\Model\ModelInterface;
/**
* Helper for rendering a template fragment in its own variable scope.
*/
class Partial extends AbstractHelper
{
/**
* Variable to which object will be assigned
*
* @var string
*/
protected $objectKey;
/**
* Renders a template fragment within a variable scope distinct from the
* calling View object. It proxies to view's render function
*
* @param string|ModelInterface $name Name of view script, or a view model
* @param array|object $values Variables to populate in the view
* @throws Exception\RuntimeException
* @return string|Partial
*/
public function __invoke($name = null, $values = null)
{
if (0 == func_num_args()) {
return $this;
}
// If we were passed only a view model, just render it.
if ($name instanceof ModelInterface) {
return $this->getView()->render($name);
}
if (is_scalar($values)) {
$values = [];
} elseif ($values instanceof ModelInterface) {
$values = $values->getVariables();
} elseif (is_object($values)) {
if (null !== ($objectKey = $this->getObjectKey())) {
$values = [$objectKey => $values];
} elseif (method_exists($values, 'toArray')) {
$values = $values->toArray();
} else {
$values = get_object_vars($values);
}
}
return $this->getView()->render($name, $values);
}
/**
* Set object key
*
* @param string|null $key
*
* @return self
*/
public function setObjectKey($key)
{
if (null === $key) {
$this->objectKey = null;
return $this;
}
$this->objectKey = (string) $key;
return $this;
}
/**
* Retrieve object key
*
* The objectKey is the variable to which an object in the iterator will be
* assigned.
*
* @return null|string
*/
public function getObjectKey()
{
return $this->objectKey;
}
}