<?php namespace Toolset\ChangeSet; /** * Class ChangeSet * * Represents a set of iteratable, countable Changes * * @package ChangeSet */ class ChangeSet implements \Iterator, \ArrayAccess, \Countable { /** * @var Change[] */ protected $changes = array(); /** * @var int Iterator position key */ private $position = 0; /** * Add a Change to the ChangeSet * * @param Change $change * * @throws ChangesetValidationException When a Change response with false to an isValid() call * * @return false When a change with the same id already exists */ public function addChange(Change $change) { if (!$change->isValid()) { throw new ChangeSetValidationException( sprintf("Attempted to add an invalid Change to a ChangeSet. Change: %s", $change) ); } foreach ($this->changes as $changeWeAlreadyHave) { if ($changeWeAlreadyHave->getId() === $change->getId()) { return false; } } $this->changes[] = $change; } /** * Remove a Change from the ChangeSet * * @param Change $change */ public function removeChange(Change $change) { array_walk($this->changes, function($changeToCheck, $key) use ($change) { /** @var Change $changeToCheck */ if ($changeToCheck->getId() === $change->getId()) { unset($this->changes[$key]); } }); } /** * Get changes * * @return Change[] */ public function getChanges() { return $this->changes; } /** * {@inheritdoc} * * @return Change */ public function current() { return $this->changes[$this->position]; } /** * {@inheritdoc} */ public function next() { ++$this->position; } /** * {@inheritdoc} */ public function key() { return $this->position; } /** * {@inheritdoc} */ public function valid() { return isset($this->changes[$this->position]); } /** * {@inheritdoc} */ public function rewind() { $this->position = 0; } /** * {@inheritdoc} */ public function count() { return count($this->changes); } /** * {@inheritdoc} */ public function offsetGet($offset) { return isset($this->changes[$offset]) ? $this->changes[$offset] : null; } /** * {@inheritdoc} */ public function offsetSet($offset, $value) { if (is_null($offset)) { $this->changes[] = $value; } else { $this->changes[$offset] = $value; } } /** * {@inheritdoc} */ public function offsetExists($offset) { return isset($this->changes[$offset]); } /** * {@inheritdoc} */ public function offsetUnset($offset) { unset($this->changes[$offset]); } }
You have javascript disabled. You will not be able to edit any code.