Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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))
- $view->partialCounter = $this->partialCounter;
- }
-
- if (!empty($model)) {
- if (is_array($model)) {
- $view->vars()->assign($model);
- } elseif (is_object($model)) {
- if (null !== ($objectKey = $this->getObjectKey())) {
- $view->vars()->offsetSet($objectKey, $model);
- } elseif (method_exists($model, 'toArray')) {
- $view->vars()->assign($model->toArray());
- } else {
- $view->vars()->assign(get_object_vars($model));
- }
- }
- }
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))
- }
-
- if (!empty($model)) {
- if (is_array($model)) {
- $view->vars()->assign($model);
- } elseif (is_object($model)) {
- if (null !== ($objectKey = $this->getObjectKey())) {
- $view->vars()->offsetSet($objectKey, $model);
- } elseif (method_exists($model, 'toArray')) {
- $view->vars()->assign($model->toArray());
- } else {
- $view->vars()->assign(get_object_vars($model));
- }
+ if (is_scalar($values)) {
+ $values = array();
+ } elseif (is_object($values)) {
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))
{
if (0 == func_num_args()) {
return $this;
}
- $view = $this->cloneView();
- if (isset($this->partialCounter)) {
- $view->partialCounter = $this->partialCounter;
- }
-
- if (!empty($model)) {
- if (is_array($model)) {
- $view->vars()->assign($model);
- } elseif (is_object($model)) {
- if (null !== ($objectKey = $this->getObjectKey())) {
- $view->vars()->offsetSet($objectKey, $model);
- } elseif (method_exists($model, 'toArray')) {
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
View
@@ -10,6 +10,7 @@
namespace Zend\View\Helper;
+use Traversable;
use Zend\View\Exception;
/**
@@ -21,97 +22,30 @@
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.
- *
- * If no arguments are passed, returns the helper instance.
- *
- * If the $model is an array, it is passed to the view object's assign()
- * method.
- *
- * If the $model is an object, it first checks to see if the object
- * implements a 'toArray' method; if so, it passes the result of that
- * method to to the view object's assign() method. Otherwise, the result of
- * get_object_vars() is passed.
+ * calling View object. It proxies to view's render function
*
* @param string $name Name of view script
- * @param array $model Variables to populate in the view
+ * @param array $values Variables to populate in the view
* @return string|Partial
* @throws Exception\RuntimeException
*/
- public function __invoke($name = null, $model = null)
+ public function __invoke($name = null, $values = null)
{
if (0 == func_num_args()) {
return $this;
}
- $view = $this->cloneView();
- if (isset($this->partialCounter)) {
- $view->partialCounter = $this->partialCounter;
- }
-
- if (!empty($model)) {
- if (is_array($model)) {
- $view->vars()->assign($model);
- } elseif (is_object($model)) {
- if (null !== ($objectKey = $this->getObjectKey())) {
- $view->vars()->offsetSet($objectKey, $model);
- } elseif (method_exists($model, 'toArray')) {
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
- $view->vars()->assign($model->toArray());
- } else {
- $view->vars()->assign(get_object_vars($model));
- }
+ if (is_scalar($values)) {
+ $values = array();
+ } elseif (is_object($values)) {
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
+ if (method_exists($values, 'toArray')) {
+ $values = $values->toArray();
+ } else {
+ $values = get_object_vars($values);
}
}
- return $view->render($name);
- }
-
- /**
- * Clone the current View
- *
- * @return \Zend\View\Renderer\RendererInterface
- */
- public function cloneView()
- {
- $view = clone $this->view;
- $view->setVars(array());
- return $view;
- }
-
- /**
- * Set object key
- *
- * @param string $key
- * @return Partial
- */
- public function setObjectKey($key)
- {
- if (null === $key) {
- $this->objectKey = null;
- } else {
- $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;
+ return $this->getView()->render($name, $values);
}
}
43 library/Zend/View/Helper/PartialLoop.php
View
@@ -10,7 +10,9 @@
namespace Zend\View\Helper;
+use Iterator;
use Traversable;
+use Zend\Stdlib\ArrayUtils;
use Zend\View\Exception;
/**
@@ -22,9 +24,9 @@
*/
class PartialLoop extends Partial
{
-
/**
* Marker to where the pointer is at in the loop
+ *
* @var integer
*/
protected $partialCounter = 0;
@@ -36,40 +38,53 @@ class PartialLoop extends Partial
* If no arguments are provided, returns object instance.
*
* @param string $name Name of view script
- * @param array $model Variables to populate in the view
+ * @param array $values Variables to populate in the view
* @return string
* @throws Exception\InvalidArgumentException
*/
- public function __invoke($name = null, $model = null)
+ public function __invoke($name = null, $values = null)
{
if (0 == func_num_args()) {
return $this;
}
- if (!is_array($model)
- && (!$model instanceof Traversable)
- && (is_object($model) && !method_exists($model, 'toArray'))
+ if (!is_array($values)
+ && (!$values instanceof Traversable)
+ && (is_object($values) && !method_exists($values, 'toArray'))
) {
throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data');
}
- if (is_object($model)
- && (!$model instanceof Traversable)
- && method_exists($model, 'toArray')
+ if (is_object($values)
+ && (!$values instanceof Traversable)
+ && method_exists($values, 'toArray')
) {
- $model = $model->toArray();
+ $values = $values->toArray();
+ }
+
+ if ($values instanceof Iterator) {
+ $values = ArrayUtils::iteratorToArray($values);
}
- $content = '';
// reset the counter if it's called again
$this->partialCounter = 0;
- foreach ($model as $item) {
- // increment the counter variable
- $this->partialCounter++;
+ $content = '';
+ foreach ($values as $item) {
+ $this->partialCounter++;
$content .= parent::__invoke($name, $item);
}
return $content;
}
+
+ /**
+ * Get the partial counter
+ *
+ * @return int
+ */
+ public function getPartialCounter()
+ {
+ return $this->partialCounter;
+ }
}
28 tests/ZendTest/View/Helper/PartialLoopTest.php
View
@@ -221,7 +221,6 @@ public function testShouldNotCastToArrayIfObjectIsTraversable()
$view = new View();
$view->resolver()->addPath($this->basePath . '/application/views/scripts');
$this->helper->setView($view);
- $this->helper->setObjectKey('obj');
$result = $this->helper->__invoke('partialLoopObject.phtml', $o);
foreach ($data as $item) {
@@ -245,7 +244,7 @@ public function testEmptyArrayPassedToPartialLoopShouldNotThrowException()
/**
* @group ZF-2737
*/
- public function testPartialLoopIncramentsPartialCounter()
+ public function testPartialLoopIncrementsPartialCounter()
{
$data = array(
array('message' => 'foo'),
@@ -258,15 +257,8 @@ public function testPartialLoopIncramentsPartialCounter()
$view->resolver()->addPath($this->basePath . '/application/views/scripts');
$this->helper->setView($view);
- $result = $this->helper->__invoke('partialLoopCouter.phtml', $data);
- foreach ($data as $key => $item) {
- $string = sprintf(
- 'This is an iteration: %s, pointer at %d',
- $item['message'],
- $key + 1
- );
- $this->assertContains($string, $result, $result);
- }
+ $this->helper->__invoke('partialLoopCouter.phtml', $data);
+ $this->assertEquals(4, $this->helper->getPartialCounter());
}
/**
@@ -285,17 +277,11 @@ public function testPartialLoopPartialCounterResets()
$view->resolver()->addPath($this->basePath . '/application/views/scripts');
$this->helper->setView($view);
- $result = $this->helper->__invoke('partialLoopCouter.phtml', $data);
- foreach ($data as $key=>$item) {
- $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1);
- $this->assertContains($string, $result);
- }
+ $this->helper->__invoke('partialLoopCouter.phtml', $data);
+ $this->assertEquals(4, $this->helper->getPartialCounter());
- $result = $this->helper->__invoke('partialLoopCouter.phtml', $data);
- foreach ($data as $key=>$item) {
- $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1);
- $this->assertContains($string, $result);
- }
+ $this->helper->__invoke('partialLoopCouter.phtml', $data);
+ $this->assertEquals(4, $this->helper->getPartialCounter());
}
}
73 tests/ZendTest/View/Helper/PartialTest.php
View
@@ -94,34 +94,6 @@ public function testSetViewSetsViewProperty()
$this->assertSame($view, $this->helper->getView());
}
- /**
- * @return void
- */
- public function testCloneViewReturnsDifferentViewInstance()
- {
- $view = new View();
- $this->helper->setView($view);
- $clone = $this->helper->cloneView();
- $this->assertNotSame($view, $clone);
- $this->assertTrue($clone instanceof View);
- }
-
- /**
- * @return void
- */
- public function testCloneViewClearsViewVariables()
- {
- $view = new View();
- $view->foo = 'bar';
- $this->helper->setView($view);
-
- $clone = $this->helper->cloneView();
- $clonedVars = $clone->vars();
-
- $this->assertEquals(0, count($clonedVars));
- $this->assertNull($clone->vars()->foo);
- }
-
public function testObjectModelWithPublicPropertiesSetsViewVariables()
{
$model = new \stdClass();
@@ -154,56 +126,11 @@ public function testObjectModelWithToArraySetsViewVariables()
}
}
- public function testObjectModelSetInObjectKeyWhenKeyPresent()
- {
- $this->helper->setObjectKey('foo');
- $model = new \stdClass();
- $model->footest = 'bar';
- $model->bartest = 'baz';
-
- $view = new View;
- $view->resolver()->addPath($this->basePath . '/application/views/scripts');
- $this->helper->setView($view);
- $return = $this->helper->__invoke('partialObj.phtml', $model);
-
- $this->assertNotContains('No object model passed', $return);
-
- foreach (get_object_vars($model) as $key => $value) {
- $string = sprintf('%s: %s', $key, $value);
- $this->assertContains($string, $return, "Checking for '$return' containing '$string'");
- }
- }
-
public function testPassingNoArgsReturnsHelperInstance()
{
$test = $this->helper->__invoke();
$this->assertSame($this->helper, $test);
}
-
- public function testObjectKeyIsNullByDefault()
- {
- $this->assertNull($this->helper->getObjectKey());
- }
-
- public function testCanSetObjectKey()
- {
- $this->testObjectKeyIsNullByDefault();
- $this->helper->setObjectKey('foo');
- $this->assertEquals('foo', $this->helper->getObjectKey());
- }
-
- public function testCanSetObjectKeyToNullValue()
- {
- $this->testCanSetObjectKey();
- $this->helper->setObjectKey(null);
- $this->assertNull($this->helper->getObjectKey());
- }
-
- public function testSetObjectKeyImplementsFluentInterface()
- {
- $test = $this->helper->setObjectKey('foo');
- $this->assertSame($this->helper, $test);
- }
}
class Aggregate
Something went wrong with that request. Please try again.