Integer::invertedPow()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\System;
13
14
/**
15
 * Integer Class.
16
 *
17
 * @author Karel Osorio Ramírez <[email protected]>
18
 */
19
class Integer extends Number
20
{
21
    /**
22
     * @param int $value
23
     *
24
     * @return \Cubiche\Domain\System\Integer
25
     */
26
    public static function fromNative($value)
27
    {
28
        return new static($value);
29
    }
30
31
    /**
32
     * @param int $value
33
     *
34
     * @throws \InvalidArgumentException
35
     */
36
    protected function __construct($value)
37
    {
38
        $filteredValue = \filter_var($value, FILTER_VALIDATE_INT);
39
        if ($filteredValue === false) {
40
            throw new \InvalidArgumentException(sprintf(
41
                'Argument "%s" is invalid. Allowed types for argument are "int".',
42
                $value
43
            ));
44
        }
45
46
        $this->value = $value;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function toInteger(RoundingMode $roundingMode = null)
53
    {
54
        return new self($this->value);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function toReal()
61
    {
62
        return Real::fromNative($this->value);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function toDecimal()
69
    {
70
        return Decimal::fromNative($this->value);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function isInfinite()
77
    {
78
        return false;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function isPositive()
85
    {
86
        return $this->value > 0;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function isNegative()
93
    {
94
        return $this->value < 0;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function isZero()
101
    {
102
        return $this->value === 0;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    protected function invertedAdd(Number $x)
109
    {
110
        return $x->addInteger($this);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function addInteger(Integer $x)
117
    {
118
        return new static($this->toNative() + $x->toNative());
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function addReal(Real $x)
125
    {
126
        return $this->toReal()->addReal($x);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function addDecimal(Decimal $x, $scale = null)
133
    {
134
        return $this->toDecimal()->addDecimal($x, $scale);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    protected function invertedSub(Number $x)
141
    {
142
        return $x->subInteger($this);
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function subInteger(Integer $x)
149
    {
150
        return new static($this->toNative() - $x->toNative());
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function subReal(Real $x)
157
    {
158
        return $this->toReal()->subReal($x);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function subDecimal(Decimal $x, $scale = null)
165
    {
166
        return $this->toDecimal()->subDecimal($x, $scale);
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    protected function invertedMult(Number $x)
173
    {
174
        return $x->multInteger($this);
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function multInteger(Integer $x)
181
    {
182
        return new static($this->toNative() * $x->toNative());
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function multReal(Real $x)
189
    {
190
        return $this->toReal()->multReal($x);
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function multDecimal(Decimal $x, $scale = null)
197
    {
198
        return $this->toDecimal()->multDecimal($x, $scale);
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    protected function invertedDiv(Number $x)
205
    {
206
        return $x->divInteger($this);
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 View Code Duplication
    public function divInteger(Integer $x)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214
        $value = $this->divSpecialCases($x);
215
        if ($value !== null) {
216
            return $value;
217
        }
218
219
        return Real::fromNative($this->toNative() / $x->toNative());
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function divReal(Real $x)
226
    {
227
        return $this->toReal()->divReal($x);
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    public function divDecimal(Decimal $x, $scale = null)
234
    {
235
        return $this->toDecimal()->divDecimal($x, $scale);
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241
    protected function invertedPow(Number $x)
242
    {
243
        return $x->powInteger($this);
244
    }
245
246
    /**
247
     * @param int $x
248
     *
249
     * @return Number
250
     */
251
    public function powInteger(Integer $x)
252
    {
253
        return new static(\pow($this->toNative(), $x->toNative()));
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function powReal(Real $x)
260
    {
261
        return $this->toReal()->powReal($x);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function powDecimal(Decimal $x, $scale = null)
268
    {
269
        return $this->toDecimal()->powDecimal($x, $scale);
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function sqrt($scale = null)
276
    {
277
        return $this->toReal()->sqrt($scale);
278
    }
279
280
    /**
281
     * @return \Cubiche\Domain\System\Integer
282
     */
283
    public function inc()
284
    {
285
        return new static($this->toNative() + 1);
286
    }
287
288
    /**
289
     * @return \Cubiche\Domain\System\Integer
290
     */
291
    public function dec()
292
    {
293
        return new static($this->toNative() - 1);
294
    }
295
296
    /**
297
     * @param int $x
298
     *
299
     * @return \Cubiche\Domain\System\Integer
300
     */
301
    public function mod(Integer $x)
302
    {
303
        return new static($this->toNative() % $x->toNative());
304
    }
305
306
    /**
307
     * @return bool
308
     */
309
    public function isEven()
310
    {
311
        return $this->mod(self::fromNative(2))->isZero();
312
    }
313
314
    /**
315
     * @return bool
316
     */
317
    public function isOdd()
318
    {
319
        return !$this->isEven();
320
    }
321
}
322