Requirements: Do not implement ArrayIterator but RecursiveIterator

refs #8508
This commit is contained in:
Johannes Meyer 2015-02-26 10:52:39 +01:00
parent 551207b5b8
commit d0a8dd8973
2 changed files with 75 additions and 17 deletions

View File

@ -1,13 +1,15 @@
<?php
use \RecursiveIteratorIterator;
use Icinga\Web\Wizard;
$requirements = $form->getRequirements();
$iterator = new RecursiveIteratorIterator($requirements);
?>
<table class="requirements">
<tbody>
<?php foreach ($requirements as $requirement): ?>
<?php foreach ($iterator as $requirement): ?>
<tr>
<td><h2><?= $requirement->getTitle(); ?></h2></td>
<td style="width: 50%">

View File

@ -3,14 +3,13 @@
namespace Icinga\Module\Setup;
use ArrayIterator;
use LogicException;
use IteratorAggregate;
use RecursiveIterator;
/**
* Container to store and handle requirements
*/
class Requirements implements IteratorAggregate
class Requirements implements RecursiveIterator
{
/**
* Mode AND (all requirements must met)
@ -94,7 +93,7 @@ class Requirements implements IteratorAggregate
public function add(Requirement $requirement)
{
$merged = false;
foreach ($this as $knownRequirement) {
foreach ($this->requirements as $knownRequirement) {
if ($knownRequirement instanceof Requirement && $requirement->equals($knownRequirement)) {
if ($this->getMode() === static::MODE_AND && !$requirement->isOptional()) {
$knownRequirement->setOptional(false);
@ -142,16 +141,6 @@ class Requirements implements IteratorAggregate
return $this->containsMandatoryRequirements || $this->getMode() === static::MODE_OR;
}
/**
* Return an iterator of all registered requirements
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->getAll());
}
/**
* Register the given requirements
*
@ -162,7 +151,7 @@ class Requirements implements IteratorAggregate
public function merge(Requirements $requirements)
{
if ($this->getMode() === static::MODE_OR && $requirements->getMode() === static::MODE_OR) {
foreach ($requirements as $requirement) {
foreach ($requirements->getAll() as $requirement) {
if ($requirement instanceof static) {
$this->merge($requirement);
} else {
@ -188,7 +177,7 @@ class Requirements implements IteratorAggregate
public function fulfilled()
{
$state = false;
foreach ($this as $requirement) {
foreach ($this->requirements as $requirement) {
if ($requirement instanceof static) {
if ($requirement->fulfilled()) {
if ($this->getMode() === static::MODE_OR) {
@ -218,4 +207,71 @@ class Requirements implements IteratorAggregate
return $state;
}
/**
* Return whether the current element represents a nested set of requirements
*
* @return bool
*/
public function hasChildren()
{
$current = $this->current();
return $current instanceof static;
}
/**
* Return a iterator for the current nested set of requirements
*
* @return RecursiveIterator
*/
public function getChildren()
{
return $this->current();
}
/**
* Rewind the iterator to its first element
*/
public function rewind()
{
reset($this->requirements);
}
/**
* Return whether the current iterator position is valid
*
* @return bool
*/
public function valid()
{
return $this->key() !== null;
}
/**
* Return the current element in the iteration
*
* @return Requirement|Requirements
*/
public function current()
{
return current($this->requirements);
}
/**
* Return the position of the current element in the iteration
*
* @return int
*/
public function key()
{
return key($this->requirements);
}
/**
* Advance the iterator to the next element
*/
public function next()
{
next($this->requirements);
}
}