1<?php 2/** 3 * This file is part of the FreeDSx LDAP package. 4 * 5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11namespace FreeDSx\Ldap\Entry; 12 13/** 14 * Represents a set of change objects. 15 * 16 * @author Chad Sikorra <Chad.Sikorra@gmail.com> 17 */ 18class Changes implements \Countable, \IteratorAggregate 19{ 20 /** 21 * @var Change[] 22 */ 23 protected $changes = []; 24 25 /** 26 * @param Change ...$changes 27 */ 28 public function __construct(Change ...$changes) 29 { 30 $this->changes = $changes; 31 } 32 33 /** 34 * Add a change to the list. 35 * 36 * @param Change ...$changes 37 * @return $this 38 */ 39 public function add(Change ...$changes) 40 { 41 foreach ($changes as $change) { 42 $this->changes[] = $change; 43 } 44 45 return $this; 46 } 47 48 /** 49 * Check if the change is in the change list. 50 * 51 * @param Change $change 52 * @return bool 53 */ 54 public function has(Change $change) 55 { 56 return \array_search($change, $this->changes, true) !== false; 57 } 58 59 /** 60 * Remove a change from the list. 61 * 62 * @param Change ...$changes 63 * @return $this 64 */ 65 public function remove(Change ...$changes) 66 { 67 foreach ($changes as $change) { 68 if (($index = \array_search($change, $this->changes, true)) !== false) { 69 unset($this->changes[$index]); 70 } 71 } 72 73 return $this; 74 } 75 76 /** 77 * Removes all changes from the list. 78 * 79 * @return $this 80 */ 81 public function reset() 82 { 83 $this->changes = []; 84 85 return $this; 86 } 87 88 /** 89 * Set the change list to just these changes. 90 * 91 * @param Change ...$changes 92 * @return $this 93 */ 94 public function set(Change ...$changes) 95 { 96 $this->changes = $changes; 97 98 return $this; 99 } 100 101 /** 102 * @return Change[] 103 */ 104 public function toArray(): array 105 { 106 return $this->changes; 107 } 108 109 /** 110 * @return int 111 */ 112 public function count() 113 { 114 return \count($this->changes); 115 } 116 117 /** 118 * @return \ArrayIterator|\Traversable 119 */ 120 public function getIterator() 121 { 122 return new \ArrayIterator($this->changes); 123 } 124} 125