Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
We need to create a list of budgets and then show information about them.
This way, by grouping via array, we can place other types of information that will not be part of the budget, thus creating gaps in our code.
<?php
class Budget
{
public float $value;
public int $items;
}
<?php
$budget1 = new Budget();
$budget1->items = 7;
$budget1->value = 1500.75;
$budget2 = new Budget();
$budget2->items = 3;
$budget2->value = 150;
$budget3 = new Budget();
$budget3->items = 5;
$budget3->value = 1350;
$budgetList = [
$budget1,
$budget2,
$budget3
];
foreach ($budgetList as $budget) {
echo "Value: ". $budget->value . PHP_EOL;
echo "Items: ". $budget->items . PHP_EOL;
echo PHP_EOL;
}
Now, using the Itarator pattern, we create an object that is a list of budgets, which only accepts budgets, and then we make it traversable by the PHP Iterator.
<?php
class BudgetList implements \IteratorAggregate
{
public array $budget = [];
public function addBudget(Budget $budget): void
{
$this->budget[] = $budget;
}
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->budget);
}
}
<?php
$budget1 = new Budget();
$budget1->items = 7;
$budget1->value = 1500.75;
$budget2 = new Budget();
$budget2->items = 3;
$budget2->value = 150;
$budget3 = new Budget();
$budget3->items = 5;
$budget3->value = 1350;
$budgetList = new BudgetList();
$budgetList->addBudget($budget1);
$budgetList->addBudget($budget2);
$budgetList->addBudget($budget3);
foreach ($budgetList as $budget) {
echo "Value: ". $budget->value . PHP_EOL;
echo "Items: ". $budget->items . PHP_EOL;
echo PHP_EOL;
}
composer install
php wrong/test.php
php right/test.php