Skip to content
This repository

Make Partial proxy to view render function #2957

Closed
wants to merge 3 commits into from

2 participants

Michaël Gallego Matthew Weier O'Phinney
Michaël Gallego

No description provided.

library/Zend/View/Helper/Partial.php
((39 lines not shown))
56   - $view->partialCounter = $this->partialCounter;
57   - }
58   -
59   - if (!empty($model)) {
60   - if (is_array($model)) {
61   - $view->vars()->assign($model);
62   - } elseif (is_object($model)) {
63   - if (null !== ($objectKey = $this->getObjectKey())) {
64   - $view->vars()->offsetSet($objectKey, $model);
65   - } elseif (method_exists($model, 'toArray')) {
66   - $view->vars()->assign($model->toArray());
67   - } else {
68   - $view->vars()->assign(get_object_vars($model));
69   - }
70   - }
71   - }
1
Matthew Weier O'Phinney Owner

We should keep the logic in this conditional; otherwise we break all backwards compatibility for the helper. Do the following:

  • If the model is a scalar, reset it to an empty array
  • If the model is a non-ModelInterface, non-View\Variables object, do the logic in the "elseif (is_object($model))" conditional, and capture the results to a variable

Then pass the model as an argument to render() (which you already do).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Matthew Weier O'Phinney weierophinney commented on the diff
library/Zend/View/Helper/Partial.php
((40 lines not shown))
57   - }
58   -
59   - if (!empty($model)) {
60   - if (is_array($model)) {
61   - $view->vars()->assign($model);
62   - } elseif (is_object($model)) {
63   - if (null !== ($objectKey = $this->getObjectKey())) {
64   - $view->vars()->offsetSet($objectKey, $model);
65   - } elseif (method_exists($model, 'toArray')) {
66   - $view->vars()->assign($model->toArray());
67   - } else {
68   - $view->vars()->assign(get_object_vars($model));
69   - }
  39 + if (is_scalar($values)) {
  40 + $values = array();
  41 + } elseif (is_object($values)) {
2
Matthew Weier O'Phinney Owner

Make this:

} elseif ($values instanceof \Zend\View\Model\ModelInterface) {
    $values = $values->getVariables();
} elseif (is_object($values)) {
   ... 

(importing ModelInterface, of course). The reason being that we can't count on a View Model implementation implementing the various bits that the next conditional looks at, but we can count on getVariables().

Matthew Weier O'Phinney Owner

Actually, we should probably check if $name is a View Model -- that could simplify things, too. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Matthew Weier O'Phinney weierophinney commented on the diff
library/Zend/View/Helper/Partial.php
((32 lines not shown))
49 34 {
50 35 if (0 == func_num_args()) {
51 36 return $this;
52 37 }
53 38
54   - $view = $this->cloneView();
55   - if (isset($this->partialCounter)) {
56   - $view->partialCounter = $this->partialCounter;
57   - }
58   -
59   - if (!empty($model)) {
60   - if (is_array($model)) {
61   - $view->vars()->assign($model);
62   - } elseif (is_object($model)) {
63   - if (null !== ($objectKey = $this->getObjectKey())) {
64   - $view->vars()->offsetSet($objectKey, $model);
65   - } elseif (method_exists($model, 'toArray')) {
1
Matthew Weier O'Phinney Owner

We need to keep the object key functionality as well; removing it is a BC break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#2957] Cleanup
- To keep backwards compatibility:
  - re-instated "object key" functionality
- For forwards compatibility:
  - allow specifying a view model as the sole argument
  - pull variables from the view model when passed as the second argument
6940e30
Matthew Weier O'Phinney

Re-introduced the "object key", and also ensured that a ViewModel instance can be used as the sole or second argument to partial(). Merged.

Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#2957] Cleanup
- To keep backwards compatibility:
  - re-instated "object key" functionality
- For forwards compatibility:
  - allow specifying a view model as the sole argument
  - pull variables from the view model when passed as the second argument
cd98d47
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/2957'
Close #2957
081446e
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/2957' into develop
Forward port #2957
d63be4a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
90 library/Zend/View/Helper/Partial.php
@@ -10,6 +10,7 @@
10 10
11 11 namespace Zend\View\Helper;
12 12
  13 +use Traversable;
13 14 use Zend\View\Exception;
14 15
15 16 /**
@@ -21,97 +22,30 @@
21 22 class Partial extends AbstractHelper
22 23 {
23 24 /**
24   - * Variable to which object will be assigned
25   - * @var string
26   - */
27   - protected $objectKey;
28   -
29   - /**
30 25 * Renders a template fragment within a variable scope distinct from the
31   - * calling View object.
32   - *
33   - * If no arguments are passed, returns the helper instance.
34   - *
35   - * If the $model is an array, it is passed to the view object's assign()
36   - * method.
37   - *
38   - * If the $model is an object, it first checks to see if the object
39   - * implements a 'toArray' method; if so, it passes the result of that
40   - * method to to the view object's assign() method. Otherwise, the result of
41   - * get_object_vars() is passed.
  26 + * calling View object. It proxies to view's render function
42 27 *
43 28 * @param string $name Name of view script
44   - * @param array $model Variables to populate in the view
  29 + * @param array $values Variables to populate in the view
45 30 * @return string|Partial
46 31 * @throws Exception\RuntimeException
47 32 */
48   - public function __invoke($name = null, $model = null)
  33 + public function __invoke($name = null, $values = null)
49 34 {
50 35 if (0 == func_num_args()) {
51 36 return $this;
52 37 }
53 38
54   - $view = $this->cloneView();
55   - if (isset($this->partialCounter)) {
56   - $view->partialCounter = $this->partialCounter;
57   - }
58   -
59   - if (!empty($model)) {
60   - if (is_array($model)) {
61   - $view->vars()->assign($model);
62   - } elseif (is_object($model)) {
63   - if (null !== ($objectKey = $this->getObjectKey())) {
64   - $view->vars()->offsetSet($objectKey, $model);
65   - } elseif (method_exists($model, 'toArray')) {
66   - $view->vars()->assign($model->toArray());
67   - } else {
68   - $view->vars()->assign(get_object_vars($model));
69   - }
  39 + if (is_scalar($values)) {
  40 + $values = array();
  41 + } elseif (is_object($values)) {
  42 + if (method_exists($values, 'toArray')) {
  43 + $values = $values->toArray();
  44 + } else {
  45 + $values = get_object_vars($values);
70 46 }
71 47 }
72 48
73   - return $view->render($name);
74   - }
75   -
76   - /**
77   - * Clone the current View
78   - *
79   - * @return \Zend\View\Renderer\RendererInterface
80   - */
81   - public function cloneView()
82   - {
83   - $view = clone $this->view;
84   - $view->setVars(array());
85   - return $view;
86   - }
87   -
88   - /**
89   - * Set object key
90   - *
91   - * @param string $key
92   - * @return Partial
93   - */
94   - public function setObjectKey($key)
95   - {
96   - if (null === $key) {
97   - $this->objectKey = null;
98   - } else {
99   - $this->objectKey = (string) $key;
100   - }
101   -
102   - return $this;
103   - }
104   -
105   - /**
106   - * Retrieve object key
107   - *
108   - * The objectKey is the variable to which an object in the iterator will be
109   - * assigned.
110   - *
111   - * @return null|string
112   - */
113   - public function getObjectKey()
114   - {
115   - return $this->objectKey;
  49 + return $this->getView()->render($name, $values);
116 50 }
117 51 }
43 library/Zend/View/Helper/PartialLoop.php
@@ -10,7 +10,9 @@
10 10
11 11 namespace Zend\View\Helper;
12 12
  13 +use Iterator;
13 14 use Traversable;
  15 +use Zend\Stdlib\ArrayUtils;
14 16 use Zend\View\Exception;
15 17
16 18 /**
@@ -22,9 +24,9 @@
22 24 */
23 25 class PartialLoop extends Partial
24 26 {
25   -
26 27 /**
27 28 * Marker to where the pointer is at in the loop
  29 + *
28 30 * @var integer
29 31 */
30 32 protected $partialCounter = 0;
@@ -36,40 +38,53 @@ class PartialLoop extends Partial
36 38 * If no arguments are provided, returns object instance.
37 39 *
38 40 * @param string $name Name of view script
39   - * @param array $model Variables to populate in the view
  41 + * @param array $values Variables to populate in the view
40 42 * @return string
41 43 * @throws Exception\InvalidArgumentException
42 44 */
43   - public function __invoke($name = null, $model = null)
  45 + public function __invoke($name = null, $values = null)
44 46 {
45 47 if (0 == func_num_args()) {
46 48 return $this;
47 49 }
48 50
49   - if (!is_array($model)
50   - && (!$model instanceof Traversable)
51   - && (is_object($model) && !method_exists($model, 'toArray'))
  51 + if (!is_array($values)
  52 + && (!$values instanceof Traversable)
  53 + && (is_object($values) && !method_exists($values, 'toArray'))
52 54 ) {
53 55 throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data');
54 56 }
55 57
56   - if (is_object($model)
57   - && (!$model instanceof Traversable)
58   - && method_exists($model, 'toArray')
  58 + if (is_object($values)
  59 + && (!$values instanceof Traversable)
  60 + && method_exists($values, 'toArray')
59 61 ) {
60   - $model = $model->toArray();
  62 + $values = $values->toArray();
  63 + }
  64 +
  65 + if ($values instanceof Iterator) {
  66 + $values = ArrayUtils::iteratorToArray($values);
61 67 }
62 68
63   - $content = '';
64 69 // reset the counter if it's called again
65 70 $this->partialCounter = 0;
66   - foreach ($model as $item) {
67   - // increment the counter variable
68   - $this->partialCounter++;
  71 + $content = '';
69 72
  73 + foreach ($values as $item) {
  74 + $this->partialCounter++;
70 75 $content .= parent::__invoke($name, $item);
71 76 }
72 77
73 78 return $content;
74 79 }
  80 +
  81 + /**
  82 + * Get the partial counter
  83 + *
  84 + * @return int
  85 + */
  86 + public function getPartialCounter()
  87 + {
  88 + return $this->partialCounter;
  89 + }
75 90 }
28 tests/ZendTest/View/Helper/PartialLoopTest.php
@@ -221,7 +221,6 @@ public function testShouldNotCastToArrayIfObjectIsTraversable()
221 221 $view = new View();
222 222 $view->resolver()->addPath($this->basePath . '/application/views/scripts');
223 223 $this->helper->setView($view);
224   - $this->helper->setObjectKey('obj');
225 224
226 225 $result = $this->helper->__invoke('partialLoopObject.phtml', $o);
227 226 foreach ($data as $item) {
@@ -245,7 +244,7 @@ public function testEmptyArrayPassedToPartialLoopShouldNotThrowException()
245 244 /**
246 245 * @group ZF-2737
247 246 */
248   - public function testPartialLoopIncramentsPartialCounter()
  247 + public function testPartialLoopIncrementsPartialCounter()
249 248 {
250 249 $data = array(
251 250 array('message' => 'foo'),
@@ -258,15 +257,8 @@ public function testPartialLoopIncramentsPartialCounter()
258 257 $view->resolver()->addPath($this->basePath . '/application/views/scripts');
259 258 $this->helper->setView($view);
260 259
261   - $result = $this->helper->__invoke('partialLoopCouter.phtml', $data);
262   - foreach ($data as $key => $item) {
263   - $string = sprintf(
264   - 'This is an iteration: %s, pointer at %d',
265   - $item['message'],
266   - $key + 1
267   - );
268   - $this->assertContains($string, $result, $result);
269   - }
  260 + $this->helper->__invoke('partialLoopCouter.phtml', $data);
  261 + $this->assertEquals(4, $this->helper->getPartialCounter());
270 262 }
271 263
272 264 /**
@@ -285,17 +277,11 @@ public function testPartialLoopPartialCounterResets()
285 277 $view->resolver()->addPath($this->basePath . '/application/views/scripts');
286 278 $this->helper->setView($view);
287 279
288   - $result = $this->helper->__invoke('partialLoopCouter.phtml', $data);
289   - foreach ($data as $key=>$item) {
290   - $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1);
291   - $this->assertContains($string, $result);
292   - }
  280 + $this->helper->__invoke('partialLoopCouter.phtml', $data);
  281 + $this->assertEquals(4, $this->helper->getPartialCounter());
293 282
294   - $result = $this->helper->__invoke('partialLoopCouter.phtml', $data);
295   - foreach ($data as $key=>$item) {
296   - $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1);
297   - $this->assertContains($string, $result);
298   - }
  283 + $this->helper->__invoke('partialLoopCouter.phtml', $data);
  284 + $this->assertEquals(4, $this->helper->getPartialCounter());
299 285 }
300 286 }
301 287
73 tests/ZendTest/View/Helper/PartialTest.php
@@ -94,34 +94,6 @@ public function testSetViewSetsViewProperty()
94 94 $this->assertSame($view, $this->helper->getView());
95 95 }
96 96
97   - /**
98   - * @return void
99   - */
100   - public function testCloneViewReturnsDifferentViewInstance()
101   - {
102   - $view = new View();
103   - $this->helper->setView($view);
104   - $clone = $this->helper->cloneView();
105   - $this->assertNotSame($view, $clone);
106   - $this->assertTrue($clone instanceof View);
107   - }
108   -
109   - /**
110   - * @return void
111   - */
112   - public function testCloneViewClearsViewVariables()
113   - {
114   - $view = new View();
115   - $view->foo = 'bar';
116   - $this->helper->setView($view);
117   -
118   - $clone = $this->helper->cloneView();
119   - $clonedVars = $clone->vars();
120   -
121   - $this->assertEquals(0, count($clonedVars));
122   - $this->assertNull($clone->vars()->foo);
123   - }
124   -
125 97 public function testObjectModelWithPublicPropertiesSetsViewVariables()
126 98 {
127 99 $model = new \stdClass();
@@ -154,56 +126,11 @@ public function testObjectModelWithToArraySetsViewVariables()
154 126 }
155 127 }
156 128
157   - public function testObjectModelSetInObjectKeyWhenKeyPresent()
158   - {
159   - $this->helper->setObjectKey('foo');
160   - $model = new \stdClass();
161   - $model->footest = 'bar';
162   - $model->bartest = 'baz';
163   -
164   - $view = new View;
165   - $view->resolver()->addPath($this->basePath . '/application/views/scripts');
166   - $this->helper->setView($view);
167   - $return = $this->helper->__invoke('partialObj.phtml', $model);
168   -
169   - $this->assertNotContains('No object model passed', $return);
170   -
171   - foreach (get_object_vars($model) as $key => $value) {
172   - $string = sprintf('%s: %s', $key, $value);
173   - $this->assertContains($string, $return, "Checking for '$return' containing '$string'");
174   - }
175   - }
176   -
177 129 public function testPassingNoArgsReturnsHelperInstance()
178 130 {
179 131 $test = $this->helper->__invoke();
180 132 $this->assertSame($this->helper, $test);
181 133 }
182   -
183   - public function testObjectKeyIsNullByDefault()
184   - {
185   - $this->assertNull($this->helper->getObjectKey());
186   - }
187   -
188   - public function testCanSetObjectKey()
189   - {
190   - $this->testObjectKeyIsNullByDefault();
191   - $this->helper->setObjectKey('foo');
192   - $this->assertEquals('foo', $this->helper->getObjectKey());
193   - }
194   -
195   - public function testCanSetObjectKeyToNullValue()
196   - {
197   - $this->testCanSetObjectKey();
198   - $this->helper->setObjectKey(null);
199   - $this->assertNull($this->helper->getObjectKey());
200   - }
201   -
202   - public function testSetObjectKeyImplementsFluentInterface()
203   - {
204   - $test = $this->helper->setObjectKey('foo');
205   - $this->assertSame($this->helper, $test);
206   - }
207 134 }
208 135
209 136 class Aggregate

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.