Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit fa581f1

Browse files
gsteelweierophinney
authored andcommitted
Add methods and tests that providing the ability to clear/delete placeholder containers
1 parent 81db40e commit fa581f1

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

doc/book/helpers/placeholder.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ $this->placeholder('foo')
6363
<?= $this->placeholder('foo') ?>
6464
```
6565

66-
The above results in an unodered list with pretty indentation.
66+
The above results in an unordered list with pretty indentation.
6767

6868
Because the `Placeholder` container objects extend `ArrayObject`, you can also
6969
assign content to a specific key in the container easily, instead of simply
@@ -131,6 +131,21 @@ foreach ($this->data as $datum): ?>
131131
<?= $this->placeholder('foo')->data ?>
132132
```
133133

134+
## Clearing Content
135+
136+
In certain situations it is desirable to remove or clear containers and aggregated content. The placeholder view helper
137+
provides two methods to either delete a specific container or clear all containers at once:
138+
139+
```php
140+
<?php
141+
// Delete a single Container
142+
$placeholderHelper = $this->plugin('placeholder');
143+
$placeholderHelper->deleteContainer('myNamedContainer');
144+
// Clear all containers at once
145+
$placeholderHelper->clearContainers();
146+
?>
147+
```
148+
134149
## Concrete Implementations
135150

136151
zend-view ships with a number of "concrete" placeholder implementations. These

src/Helper/Placeholder.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Placeholder extends AbstractHelper
2323
/**
2424
* Placeholder items
2525
*
26-
* @var array
26+
* @var Container\AbstractContainer[]
2727
*/
2828
protected $items = [];
2929

@@ -97,4 +97,28 @@ public function containerExists($key)
9797
$return = array_key_exists($key, $this->items);
9898
return $return;
9999
}
100+
101+
/**
102+
* Delete a specific container by name
103+
*
104+
* @param string $key
105+
* @return self
106+
*/
107+
public function deleteContainer($key)
108+
{
109+
$key = (string) $key;
110+
unset($this->items[$key]);
111+
return $this;
112+
}
113+
114+
/**
115+
* Remove all containers
116+
*
117+
* @return self
118+
*/
119+
public function clearContainers()
120+
{
121+
$this->items = [];
122+
return $this;
123+
}
100124
}

test/Helper/PlaceholderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,29 @@ public function testPlaceholderRetrievesSameContainerOnSubsequentCalls()
7777
$container2 = $this->placeholder->__invoke('foo');
7878
$this->assertSame($container1, $container2);
7979
}
80+
81+
public function testContainersCanBeDeleted()
82+
{
83+
$container = $this->placeholder->__invoke('foo');
84+
$container->set('Value');
85+
$this->assertTrue($this->placeholder->containerExists('foo'));
86+
$this->assertSame('Value', (string) $this->placeholder->__invoke('foo'));
87+
$this->placeholder->deleteContainer('foo');
88+
$this->assertFalse($this->placeholder->containerExists('foo'));
89+
$this->assertSame('', (string) $this->placeholder->__invoke('foo'));
90+
}
91+
92+
public function testClearContainersRemovesAllContainers()
93+
{
94+
$this->placeholder->__invoke('foo');
95+
$this->placeholder->__invoke('bar');
96+
97+
$this->assertTrue($this->placeholder->containerExists('foo'));
98+
$this->assertTrue($this->placeholder->containerExists('bar'));
99+
100+
$this->placeholder->clearContainers();
101+
102+
$this->assertFalse($this->placeholder->containerExists('foo'));
103+
$this->assertFalse($this->placeholder->containerExists('bar'));
104+
}
80105
}

0 commit comments

Comments
 (0)