~ubuntu-branches/ubuntu/natty/phpunit/natty

« back to all changes in this revision

Viewing changes to PHPUnit-3.4.14/PHPUnit/Samples/Money/MoneyBag.php

  • Committer: Package Import Robot
  • Author(s): Ivan Borzenkov
  • Date: 2010-12-11 18:19:39 UTC
  • mfrom: (0.11.1) (1.5.3) (12.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20101211181939-8650nbu08hf2z9v1
Tags: 3.5.5-2
fix doc-base-file-references-missing-file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * PHPUnit
4
 
 *
5
 
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6
 
 * All rights reserved.
7
 
 *
8
 
 * Redistribution and use in source and binary forms, with or without
9
 
 * modification, are permitted provided that the following conditions
10
 
 * are met:
11
 
 *
12
 
 *   * Redistributions of source code must retain the above copyright
13
 
 *     notice, this list of conditions and the following disclaimer.
14
 
 *
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
18
 
 *     distribution.
19
 
 *
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.
23
 
 *
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.
36
 
 *
37
 
 * @category   Testing
38
 
 * @package    PHPUnit
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
44
 
 */
45
 
 
46
 
require_once 'IMoney.php';
47
 
require_once 'Money.php';
48
 
 
49
 
/**
50
 
 * A MoneyBag.
51
 
 *
52
 
 * @category   Testing
53
 
 * @package    PHPUnit
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
60
 
 */
61
 
class MoneyBag implements IMoney
62
 
{
63
 
    protected $fMonies = array();
64
 
 
65
 
    public static function create(IMoney $m1, IMoney $m2)
66
 
    {
67
 
        $result = new MoneyBag;
68
 
        $m1->appendTo($result);
69
 
        $m2->appendTo($result);
70
 
 
71
 
        return $result->simplify();
72
 
    }
73
 
 
74
 
    public function add(IMoney $m)
75
 
    {
76
 
        return $m->addMoneyBag($this);
77
 
    }
78
 
 
79
 
    public function addMoney(Money $m)
80
 
    {
81
 
        return MoneyBag::create($m, $this);
82
 
    }
83
 
 
84
 
    public function addMoneyBag(MoneyBag $s)
85
 
    {
86
 
        return MoneyBag::create($s, $this);
87
 
    }
88
 
 
89
 
    public function appendBag(MoneyBag $aBag)
90
 
    {
91
 
        foreach ($aBag->monies() as $aMoney) {
92
 
            $this->appendMoney($aMoney);
93
 
        }
94
 
    }
95
 
 
96
 
    public function monies()
97
 
    {
98
 
        return $this->fMonies;
99
 
    }
100
 
 
101
 
    public function appendMoney(Money $aMoney)
102
 
    {
103
 
        if ($aMoney->isZero()) {
104
 
            return;
105
 
        }
106
 
 
107
 
        $old = $this->findMoney($aMoney->currency());
108
 
 
109
 
        if ($old == NULL) {
110
 
            $this->fMonies[] = $aMoney;
111
 
            return;
112
 
        }
113
 
 
114
 
        $keys = array_keys($this->fMonies);
115
 
        $max  = count($keys);
116
 
 
117
 
        for ($i = 0; $i < $max; $i++) {
118
 
            if ($this->fMonies[$keys[$i]] === $old) {
119
 
                unset($this->fMonies[$keys[$i]]);
120
 
                break;
121
 
            }
122
 
        }
123
 
 
124
 
        $sum = $old->add($aMoney);
125
 
 
126
 
        if ($sum->isZero()) {
127
 
            return;
128
 
        }
129
 
 
130
 
        $this->fMonies[] = $sum;
131
 
    }
132
 
 
133
 
    public function equals($anObject)
134
 
    {
135
 
        if ($this->isZero() &&
136
 
            $anObject instanceof IMoney) {
137
 
            return $anObject->isZero();
138
 
        }
139
 
 
140
 
        if ($anObject instanceof MoneyBag) {
141
 
            if (count($anObject->monies()) != count($this->fMonies)) {
142
 
                return FALSE;
143
 
            }
144
 
 
145
 
            foreach ($this->fMonies as $m) {
146
 
                if (!$anObject->contains($m)) {
147
 
                    return FALSE;
148
 
                }
149
 
            }
150
 
 
151
 
            return TRUE;
152
 
        }
153
 
 
154
 
        return FALSE;
155
 
    }
156
 
 
157
 
    protected function findMoney($currency)
158
 
    {
159
 
        foreach ($this->fMonies as $m) {
160
 
            if ($m->currency() == $currency) {
161
 
                return $m;
162
 
            }
163
 
        }
164
 
 
165
 
        return NULL;
166
 
    }
167
 
 
168
 
    protected function contains(Money $m)
169
 
    {
170
 
        $found = $this->findMoney($m->currency());
171
 
 
172
 
        if ($found == NULL) {
173
 
            return FALSE;
174
 
        }
175
 
 
176
 
        return $found->amount() == $m->amount();
177
 
    }
178
 
 
179
 
    public function hashCode()
180
 
    {
181
 
        $hash = 0;
182
 
 
183
 
        foreach ($this->fMonies as $m) {
184
 
            $hash ^= $m->hashCode();
185
 
        }
186
 
 
187
 
        return $hash;
188
 
    }
189
 
 
190
 
    public function isZero()
191
 
    {
192
 
        return count($this->fMonies) == 0;
193
 
    }
194
 
 
195
 
    public function multiply($factor)
196
 
    {
197
 
        $result = new MoneyBag;
198
 
 
199
 
        if ($factor != 0) {
200
 
            foreach ($this->fMonies as $m) {
201
 
                $result->appendMoney($m->multiply($factor));
202
 
            }
203
 
        }
204
 
 
205
 
        return $result;
206
 
    }
207
 
 
208
 
    public function negate()
209
 
    {
210
 
        $result = new MoneyBag;
211
 
 
212
 
        foreach ($this->fMonies as $m) {
213
 
                $result->appendMoney($m->negate());
214
 
        }
215
 
 
216
 
        return $result;
217
 
    }
218
 
 
219
 
    protected function simplify()
220
 
    {
221
 
        if (count($this->fMonies) == 1) {
222
 
            return array_pop($this->fMonies);
223
 
        }
224
 
 
225
 
        return $this;
226
 
    }
227
 
 
228
 
    public function subtract(IMoney $m)
229
 
    {
230
 
        return $this->add($m->negate());
231
 
    }
232
 
 
233
 
    public function toString()
234
 
    {
235
 
        $buffer = '{';
236
 
 
237
 
        foreach ($this->fMonies as $m) {
238
 
            $buffer .= $m->toString();
239
 
        }
240
 
 
241
 
        return $buffer . '}';
242
 
    }
243
 
 
244
 
    public function appendTo(MoneyBag $m)
245
 
    {
246
 
        $m->appendBag($this);
247
 
    }
248
 
}
249
 
?>
 
 
b'\\ No newline at end of file'