5
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in
17
* the documentation and/or other materials provided with the
20
* * Neither the name of Sebastian Bergmann nor the names of his
21
* contributors may be used to endorse or promote products derived
22
* from this software without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
* POSSIBILITY OF SUCH DAMAGE.
39
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42
* @link http://www.phpunit.de/
43
* @since File available since Release 2.3.0
46
require_once 'IMoney.php';
47
require_once 'Money.php';
54
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
55
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
56
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
57
* @version Release: 3.4.14
58
* @link http://www.phpunit.de/
59
* @since Class available since Release 2.3.0
61
class MoneyBag implements IMoney
63
protected $fMonies = array();
65
public static function create(IMoney $m1, IMoney $m2)
67
$result = new MoneyBag;
68
$m1->appendTo($result);
69
$m2->appendTo($result);
71
return $result->simplify();
74
public function add(IMoney $m)
76
return $m->addMoneyBag($this);
79
public function addMoney(Money $m)
81
return MoneyBag::create($m, $this);
84
public function addMoneyBag(MoneyBag $s)
86
return MoneyBag::create($s, $this);
89
public function appendBag(MoneyBag $aBag)
91
foreach ($aBag->monies() as $aMoney) {
92
$this->appendMoney($aMoney);
96
public function monies()
98
return $this->fMonies;
101
public function appendMoney(Money $aMoney)
103
if ($aMoney->isZero()) {
107
$old = $this->findMoney($aMoney->currency());
110
$this->fMonies[] = $aMoney;
114
$keys = array_keys($this->fMonies);
117
for ($i = 0; $i < $max; $i++) {
118
if ($this->fMonies[$keys[$i]] === $old) {
119
unset($this->fMonies[$keys[$i]]);
124
$sum = $old->add($aMoney);
126
if ($sum->isZero()) {
130
$this->fMonies[] = $sum;
133
public function equals($anObject)
135
if ($this->isZero() &&
136
$anObject instanceof IMoney) {
137
return $anObject->isZero();
140
if ($anObject instanceof MoneyBag) {
141
if (count($anObject->monies()) != count($this->fMonies)) {
145
foreach ($this->fMonies as $m) {
146
if (!$anObject->contains($m)) {
157
protected function findMoney($currency)
159
foreach ($this->fMonies as $m) {
160
if ($m->currency() == $currency) {
168
protected function contains(Money $m)
170
$found = $this->findMoney($m->currency());
172
if ($found == NULL) {
176
return $found->amount() == $m->amount();
179
public function hashCode()
183
foreach ($this->fMonies as $m) {
184
$hash ^= $m->hashCode();
190
public function isZero()
192
return count($this->fMonies) == 0;
195
public function multiply($factor)
197
$result = new MoneyBag;
200
foreach ($this->fMonies as $m) {
201
$result->appendMoney($m->multiply($factor));
208
public function negate()
210
$result = new MoneyBag;
212
foreach ($this->fMonies as $m) {
213
$result->appendMoney($m->negate());
219
protected function simplify()
221
if (count($this->fMonies) == 1) {
222
return array_pop($this->fMonies);
228
public function subtract(IMoney $m)
230
return $this->add($m->negate());
233
public function toString()
237
foreach ($this->fMonies as $m) {
238
$buffer .= $m->toString();
241
return $buffer . '}';
244
public function appendTo(MoneyBag $m)
246
$m->appendBag($this);
b'\\ No newline at end of file'