@@ -29,6 +29,10 @@ |
||
29 | 29 | { |
30 | 30 | private $decSepRetriever; |
31 | 31 | |
32 | + /** |
|
33 | + * @param string $schemaName |
|
34 | + * @param string $name |
|
35 | + */ |
|
32 | 36 | public function __construct($schemaName, $name, IConnection $connection) |
33 | 37 | { |
34 | 38 | parent::__construct($schemaName, $name, $connection); |
@@ -45,8 +45,7 @@ discard block |
||
45 | 45 | $decSep = $this->decSepRetriever->getValue(); |
46 | 46 | try { |
47 | 47 | return Money::fromString($str, $decSep); |
48 | - } |
|
49 | - catch (\InvalidArgumentException $e) { |
|
48 | + } catch (\InvalidArgumentException $e) { |
|
50 | 49 | $this->throwInvalidValue($str, $e); |
51 | 50 | } |
52 | 51 | } |
@@ -59,17 +58,13 @@ discard block |
||
59 | 58 | |
60 | 59 | if ($val instanceof Money) { |
61 | 60 | $str = $val->getAmount()->toString(); |
62 | - } |
|
63 | - elseif ($val instanceof Decimal) { |
|
61 | + } elseif ($val instanceof Decimal) { |
|
64 | 62 | $str = $val->toString(); |
65 | - } |
|
66 | - elseif (is_int($val) || is_float($val) || filter_var($val, FILTER_VALIDATE_FLOAT)) { |
|
63 | + } elseif (is_int($val) || is_float($val) || filter_var($val, FILTER_VALIDATE_FLOAT)) { |
|
67 | 64 | $str = (string)$val; |
68 | - } |
|
69 | - elseif (is_string($val)) { |
|
65 | + } elseif (is_string($val)) { |
|
70 | 66 | $str = Money::fromString($val, '.')->getAmount()->toString(); |
71 | - } |
|
72 | - else { |
|
67 | + } else { |
|
73 | 68 | $this->throwInvalidValue($val); |
74 | 69 | } |
75 | 70 |
@@ -1,10 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Ivory\Type\Std; |
3 | 3 | |
4 | -use Ivory\Value\Line; |
|
5 | -use Ivory\Value\LineSegment; |
|
6 | 4 | use Ivory\Value\Path; |
7 | -use Ivory\Value\Polygon; |
|
8 | 5 | |
9 | 6 | /** |
10 | 7 | * Path on a plane. |
@@ -41,7 +41,7 @@ |
||
41 | 41 | $fragments = explode(',', $pointListStr); |
42 | 42 | try { |
43 | 43 | for ($i = 0; $i < count($fragments); $i += 2) { |
44 | - $points[] = $this->pointType->parseValue($fragments[$i] . ',' . $fragments[$i+1]); |
|
44 | + $points[] = $this->pointType->parseValue($fragments[$i] . ',' . $fragments[$i + 1]); |
|
45 | 45 | } |
46 | 46 | return Path::fromPoints($points, ($isOpen ? Path::OPEN : Path::CLOSED)); |
47 | 47 | } |
@@ -44,12 +44,10 @@ discard block |
||
44 | 44 | $points[] = $this->pointType->parseValue($fragments[$i] . ',' . $fragments[$i+1]); |
45 | 45 | } |
46 | 46 | return Path::fromPoints($points, ($isOpen ? Path::OPEN : Path::CLOSED)); |
47 | - } |
|
48 | - catch (\InvalidArgumentException $e) { |
|
47 | + } catch (\InvalidArgumentException $e) { |
|
49 | 48 | $this->throwInvalidValue($str, $e); |
50 | 49 | } |
51 | - } |
|
52 | - else { |
|
50 | + } else { |
|
53 | 51 | $this->throwInvalidValue($str); |
54 | 52 | } |
55 | 53 | } |
@@ -63,8 +61,7 @@ discard block |
||
63 | 61 | if (!$val instanceof Path) { |
64 | 62 | try { |
65 | 63 | $val = Path::fromPoints($val); |
66 | - } |
|
67 | - catch (\InvalidArgumentException $e) { |
|
64 | + } catch (\InvalidArgumentException $e) { |
|
68 | 65 | $this->throwInvalidValue($val, $e); |
69 | 66 | } |
70 | 67 | } |
@@ -1,8 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Ivory\Type\Std; |
3 | 3 | |
4 | -use Ivory\Value\Line; |
|
5 | -use Ivory\Value\LineSegment; |
|
6 | 4 | use Ivory\Value\Polygon; |
7 | 5 | |
8 | 6 | /** |
@@ -39,7 +39,7 @@ |
||
39 | 39 | $fragments = explode(',', $pointListStr); |
40 | 40 | try { |
41 | 41 | for ($i = 0; $i < count($fragments); $i += 2) { |
42 | - $points[] = $this->pointType->parseValue($fragments[$i] . ',' . $fragments[$i+1]); |
|
42 | + $points[] = $this->pointType->parseValue($fragments[$i] . ',' . $fragments[$i + 1]); |
|
43 | 43 | } |
44 | 44 | return Polygon::fromPoints($points); |
45 | 45 | } |
@@ -42,12 +42,10 @@ discard block |
||
42 | 42 | $points[] = $this->pointType->parseValue($fragments[$i] . ',' . $fragments[$i+1]); |
43 | 43 | } |
44 | 44 | return Polygon::fromPoints($points); |
45 | - } |
|
46 | - catch (\InvalidArgumentException $e) { |
|
45 | + } catch (\InvalidArgumentException $e) { |
|
47 | 46 | $this->throwInvalidValue($str, $e); |
48 | 47 | } |
49 | - } |
|
50 | - else { |
|
48 | + } else { |
|
51 | 49 | $this->throwInvalidValue($str); |
52 | 50 | } |
53 | 51 | } |
@@ -61,8 +59,7 @@ discard block |
||
61 | 59 | if (!$val instanceof Polygon) { |
62 | 60 | try { |
63 | 61 | $val = Polygon::fromPoints($val); |
64 | - } |
|
65 | - catch (\InvalidArgumentException $e) { |
|
62 | + } catch (\InvalidArgumentException $e) { |
|
66 | 63 | $this->throwInvalidValue($val, $e); |
67 | 64 | } |
68 | 65 | } |
@@ -45,6 +45,10 @@ |
||
45 | 45 | |
46 | 46 | private $dateStyleRetriever; |
47 | 47 | |
48 | + /** |
|
49 | + * @param string $schemaName |
|
50 | + * @param string $name |
|
51 | + */ |
|
48 | 52 | public function __construct($schemaName, $name, IConnection $connection) |
49 | 53 | { |
50 | 54 | parent::__construct($schemaName, $name, $connection); |
@@ -58,11 +58,9 @@ discard block |
||
58 | 58 | { |
59 | 59 | if ($str === null) { |
60 | 60 | return null; |
61 | - } |
|
62 | - elseif ($str == 'infinity') { |
|
61 | + } elseif ($str == 'infinity') { |
|
63 | 62 | return Timestamp::infinity(); |
64 | - } |
|
65 | - elseif ($str == '-infinity') { |
|
63 | + } elseif ($str == '-infinity') { |
|
66 | 64 | return Timestamp::minusInfinity(); |
67 | 65 | } |
68 | 66 | |
@@ -86,8 +84,7 @@ discard block |
||
86 | 84 | preg_match('~^(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+(?:\.\d+)?).*?(BC)?$~', $str, $matches); |
87 | 85 | if ($dateStyle->getOrder() == DateStyle::ORDER_DMY) { |
88 | 86 | list(, $d, $m, $y, $h, $i, $s) = $matches; |
89 | - } |
|
90 | - else { |
|
87 | + } else { |
|
91 | 88 | list(, $m, $d, $y, $h, $i, $s) = $matches; |
92 | 89 | } |
93 | 90 | break; |
@@ -95,8 +92,7 @@ discard block |
||
95 | 92 | preg_match('~^\w{3} (\d+|\w{3}) (\d+|\w{3}) (\d+):(\d+):(\d+(?:\.\d+)?) (\d+).*?(BC)?$~', $str, $matches); |
96 | 93 | if ($dateStyle->getOrder() == DateStyle::ORDER_DMY) { |
97 | 94 | list(, $d, $ms, $h, $i, $s, $y) = $matches; |
98 | - } |
|
99 | - else { |
|
95 | + } else { |
|
100 | 96 | list(, $ms, $d, $h, $i, $s, $y) = $matches; |
101 | 97 | } |
102 | 98 | static $monthNames = [ |
@@ -131,14 +127,11 @@ discard block |
||
131 | 127 | ($val->format('u') ? $val->format('.u') : ''), |
132 | 128 | ($val->getYear() < 0 ? ' BC' : '') |
133 | 129 | ); |
134 | - } |
|
135 | - elseif ($val === Timestamp::infinity()) { |
|
130 | + } elseif ($val === Timestamp::infinity()) { |
|
136 | 131 | return "'infinity'"; |
137 | - } |
|
138 | - elseif ($val === Timestamp::minusInfinity()) { |
|
132 | + } elseif ($val === Timestamp::minusInfinity()) { |
|
139 | 133 | return "'-infinity'"; |
140 | - } |
|
141 | - else { |
|
134 | + } else { |
|
142 | 135 | throw new \LogicException('A non-finite timestamp not recognized'); |
143 | 136 | } |
144 | 137 | } |
@@ -28,6 +28,10 @@ discard block |
||
28 | 28 | private $dateStyleRetriever; |
29 | 29 | private $localMeanTimeZoneRetriever; |
30 | 30 | |
31 | + /** |
|
32 | + * @param string $schemaName |
|
33 | + * @param string $name |
|
34 | + */ |
|
31 | 35 | public function __construct($schemaName, $name, IConnection $connection) |
32 | 36 | { |
33 | 37 | parent::__construct($schemaName, $name, $connection); |
@@ -38,6 +42,10 @@ discard block |
||
38 | 42 | $this->localMeanTimeZoneRetriever = new ConnConfigValueRetriever( |
39 | 43 | $connection->getConfig(), |
40 | 44 | ConfigParam::TIME_ZONE, |
45 | + |
|
46 | + /** |
|
47 | + * @param string $timeZone |
|
48 | + */ |
|
41 | 49 | function ($timeZone) use ($connection) { |
42 | 50 | try { |
43 | 51 | $tz = new \DateTimeZone($timeZone); |
@@ -38,13 +38,13 @@ discard block |
||
38 | 38 | $this->localMeanTimeZoneRetriever = new ConnConfigValueRetriever( |
39 | 39 | $connection->getConfig(), |
40 | 40 | ConfigParam::TIME_ZONE, |
41 | - function ($timeZone) use ($connection) { |
|
41 | + function($timeZone) use ($connection) { |
|
42 | 42 | try { |
43 | 43 | $tz = new \DateTimeZone($timeZone); |
44 | 44 | $longitude = $tz->getLocation()['longitude']; |
45 | 45 | $offset = $longitude * 24 / 360; |
46 | 46 | $abs = abs($offset); |
47 | - $tzSpec = sprintf('%s%d:%02d', ($offset >= 0 ? '+' : '-'), (int)$abs, (int)(($abs - (int)$abs)*60)); |
|
47 | + $tzSpec = sprintf('%s%d:%02d', ($offset >= 0 ? '+' : '-'), (int)$abs, (int)(($abs - (int)$abs) * 60)); |
|
48 | 48 | // unfortunately, \DateTimeZone cannot be created with offsets precise to seconds |
49 | 49 | return new \DateTimeZone($tzSpec); |
50 | 50 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | case DateStyle::FORMAT_POSTGRES: |
100 | 100 | preg_match('~^\w{3} (\d+|\w{3}) (\d+|\w{3}) (\d+):(\d+):(\d+(?:\.\d+)?) (\d+) ([^ ]+)( BC)?$~', $str, $matches); |
101 | 101 | if ($dateStyle->getOrder() == DateStyle::ORDER_DMY) { |
102 | - list(, $d, $ms, $h, $i, $s, $y ,$z) = $matches; |
|
102 | + list(, $d, $ms, $h, $i, $s, $y, $z) = $matches; |
|
103 | 103 | } |
104 | 104 | else { |
105 | 105 | list(, $ms, $d, $h, $i, $s, $y, $z) = $matches; |
@@ -47,8 +47,7 @@ discard block |
||
47 | 47 | $tzSpec = sprintf('%s%d:%02d', ($offset >= 0 ? '+' : '-'), (int)$abs, (int)(($abs - (int)$abs)*60)); |
48 | 48 | // unfortunately, \DateTimeZone cannot be created with offsets precise to seconds |
49 | 49 | return new \DateTimeZone($tzSpec); |
50 | - } |
|
51 | - catch (\Exception $e) { |
|
50 | + } catch (\Exception $e) { |
|
52 | 51 | $msg = "Time zone '$timeZone', as configured for the PostgreSQL connection {$connection->getName()}, " |
53 | 52 | . "is unknown to PHP. Falling back to UTC (only relevant for timestamptz values representing " |
54 | 53 | . "very old date/times)."; |
@@ -63,11 +62,9 @@ discard block |
||
63 | 62 | { |
64 | 63 | if ($str === null) { |
65 | 64 | return null; |
66 | - } |
|
67 | - elseif ($str == 'infinity') { |
|
65 | + } elseif ($str == 'infinity') { |
|
68 | 66 | return TimestampTz::infinity(); |
69 | - } |
|
70 | - elseif ($str == '-infinity') { |
|
67 | + } elseif ($str == '-infinity') { |
|
71 | 68 | return TimestampTz::minusInfinity(); |
72 | 69 | } |
73 | 70 | |
@@ -91,8 +88,7 @@ discard block |
||
91 | 88 | preg_match('~^(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+(?:\.\d+)?) ([^ ]+)( BC)?$~', $str, $matches); |
92 | 89 | if ($dateStyle->getOrder() == DateStyle::ORDER_DMY) { |
93 | 90 | list(, $d, $m, $y, $h, $i, $s, $z) = $matches; |
94 | - } |
|
95 | - else { |
|
91 | + } else { |
|
96 | 92 | list(, $m, $d, $y, $h, $i, $s, $z) = $matches; |
97 | 93 | } |
98 | 94 | break; |
@@ -100,8 +96,7 @@ discard block |
||
100 | 96 | preg_match('~^\w{3} (\d+|\w{3}) (\d+|\w{3}) (\d+):(\d+):(\d+(?:\.\d+)?) (\d+) ([^ ]+)( BC)?$~', $str, $matches); |
101 | 97 | if ($dateStyle->getOrder() == DateStyle::ORDER_DMY) { |
102 | 98 | list(, $d, $ms, $h, $i, $s, $y ,$z) = $matches; |
103 | - } |
|
104 | - else { |
|
99 | + } else { |
|
105 | 100 | list(, $ms, $d, $h, $i, $s, $y, $z) = $matches; |
106 | 101 | } |
107 | 102 | static $monthNames = [ |
@@ -140,14 +135,11 @@ discard block |
||
140 | 135 | $val->getOffsetISOString(), |
141 | 136 | ($val->getYear() < 0 ? ' BC' : '') |
142 | 137 | ); |
143 | - } |
|
144 | - elseif ($val === TimestampTz::infinity()) { |
|
138 | + } elseif ($val === TimestampTz::infinity()) { |
|
145 | 139 | return "'infinity'"; |
146 | - } |
|
147 | - elseif ($val === TimestampTz::minusInfinity()) { |
|
140 | + } elseif ($val === TimestampTz::minusInfinity()) { |
|
148 | 141 | return "'-infinity'"; |
149 | - } |
|
150 | - else { |
|
142 | + } else { |
|
151 | 143 | throw new \LogicException('A non-finite timezone-aware timestamp not recognized'); |
152 | 144 | } |
153 | 145 | } |
@@ -10,9 +10,9 @@ |
||
10 | 10 | * plain array of matches, but an extended array: each item is a pair of the matching portion of the needle and the |
11 | 11 | * byte offset to the subject, like {@link preg_match_all()} does with the `PREG_OFFSET_CAPTURE` flag. |
12 | 12 | * |
13 | - * @param string|string[] $pattern |
|
13 | + * @param string $pattern |
|
14 | 14 | * @param callable $callback |
15 | - * @param string|string[] $subject |
|
15 | + * @param string $subject |
|
16 | 16 | * @param int $limit |
17 | 17 | * @param int|null $count |
18 | 18 | * @return string|string[] depending on whether <tt>$subject</tt> is <tt>string</tt> or <tt>array</tt>, |
@@ -5,59 +5,59 @@ |
||
5 | 5 | |
6 | 6 | class StringUtils |
7 | 7 | { |
8 | - /** |
|
9 | - * Like {@link preg_replace_callback()}, it performs a string search and replace. The callback does not receive |
|
10 | - * plain array of matches, but an extended array: each item is a pair of the matching portion of the needle and the |
|
11 | - * byte offset to the subject, like {@link preg_match_all()} does with the `PREG_OFFSET_CAPTURE` flag. |
|
12 | - * |
|
13 | - * @param string|string[] $pattern |
|
14 | - * @param callable $callback |
|
15 | - * @param string|string[] $subject |
|
16 | - * @param int $limit |
|
17 | - * @param int|null $count |
|
18 | - * @return string|string[] depending on whether <tt>$subject</tt> is <tt>string</tt> or <tt>array</tt>, |
|
19 | - * a <tt>string</tt> or <tt>array</tt> is returned |
|
20 | - */ |
|
21 | - public static function pregReplaceCallbackWithOffset( |
|
22 | - $pattern, callable $callback, $subject, int $limit = -1, int &$count = null) |
|
23 | - { |
|
24 | - if (is_array($subject)) { |
|
25 | - $result = []; |
|
26 | - foreach ($subject as $item) { |
|
27 | - $result[] = self::pregReplaceCallbackWithOffsetImpl($pattern, $callback, $item, $limit, $count); |
|
28 | - } |
|
29 | - return $result; |
|
30 | - } |
|
31 | - else { |
|
32 | - return self::pregReplaceCallbackWithOffsetImpl($pattern, $callback, $subject, $limit, $count); |
|
33 | - } |
|
34 | - } |
|
8 | + /** |
|
9 | + * Like {@link preg_replace_callback()}, it performs a string search and replace. The callback does not receive |
|
10 | + * plain array of matches, but an extended array: each item is a pair of the matching portion of the needle and the |
|
11 | + * byte offset to the subject, like {@link preg_match_all()} does with the `PREG_OFFSET_CAPTURE` flag. |
|
12 | + * |
|
13 | + * @param string|string[] $pattern |
|
14 | + * @param callable $callback |
|
15 | + * @param string|string[] $subject |
|
16 | + * @param int $limit |
|
17 | + * @param int|null $count |
|
18 | + * @return string|string[] depending on whether <tt>$subject</tt> is <tt>string</tt> or <tt>array</tt>, |
|
19 | + * a <tt>string</tt> or <tt>array</tt> is returned |
|
20 | + */ |
|
21 | + public static function pregReplaceCallbackWithOffset( |
|
22 | + $pattern, callable $callback, $subject, int $limit = -1, int &$count = null) |
|
23 | + { |
|
24 | + if (is_array($subject)) { |
|
25 | + $result = []; |
|
26 | + foreach ($subject as $item) { |
|
27 | + $result[] = self::pregReplaceCallbackWithOffsetImpl($pattern, $callback, $item, $limit, $count); |
|
28 | + } |
|
29 | + return $result; |
|
30 | + } |
|
31 | + else { |
|
32 | + return self::pregReplaceCallbackWithOffsetImpl($pattern, $callback, $subject, $limit, $count); |
|
33 | + } |
|
34 | + } |
|
35 | 35 | |
36 | - private static function pregReplaceCallbackWithOffsetImpl( |
|
37 | - $pattern, callable $callback, string $subject, int $limit = -1, int &$count = null) : string |
|
38 | - { |
|
39 | - if (is_array($pattern)) { |
|
40 | - throw new NotImplementedException(); // TODO |
|
41 | - } |
|
36 | + private static function pregReplaceCallbackWithOffsetImpl( |
|
37 | + $pattern, callable $callback, string $subject, int $limit = -1, int &$count = null) : string |
|
38 | + { |
|
39 | + if (is_array($pattern)) { |
|
40 | + throw new NotImplementedException(); // TODO |
|
41 | + } |
|
42 | 42 | |
43 | - $count = 0; |
|
43 | + $count = 0; |
|
44 | 44 | |
45 | - $curOffset = 0; |
|
46 | - $result = ''; |
|
47 | - preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); |
|
48 | - foreach ($matches as $i => $match) { |
|
49 | - if ($limit >= 0 && $i >= $limit) { |
|
50 | - break; |
|
51 | - } |
|
45 | + $curOffset = 0; |
|
46 | + $result = ''; |
|
47 | + preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); |
|
48 | + foreach ($matches as $i => $match) { |
|
49 | + if ($limit >= 0 && $i >= $limit) { |
|
50 | + break; |
|
51 | + } |
|
52 | 52 | |
53 | - $result .= substr($subject, $curOffset, $match[0][1] - $curOffset) . $callback($match); |
|
54 | - $curOffset = $match[0][1] + strlen($match[0][0]); |
|
55 | - $count++; |
|
56 | - } |
|
57 | - $result .= substr($subject, $curOffset); |
|
53 | + $result .= substr($subject, $curOffset, $match[0][1] - $curOffset) . $callback($match); |
|
54 | + $curOffset = $match[0][1] + strlen($match[0][0]); |
|
55 | + $count++; |
|
56 | + } |
|
57 | + $result .= substr($subject, $curOffset); |
|
58 | 58 | |
59 | - return $result; |
|
60 | - } |
|
59 | + return $result; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | /** |
63 | 63 | * @param int $num |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | * a <tt>string</tt> or <tt>array</tt> is returned |
20 | 20 | */ |
21 | 21 | public static function pregReplaceCallbackWithOffset( |
22 | - $pattern, callable $callback, $subject, int $limit = -1, int &$count = null) |
|
22 | + $pattern, callable $callback, $subject, int $limit = -1, int & $count = null) |
|
23 | 23 | { |
24 | 24 | if (is_array($subject)) { |
25 | 25 | $result = []; |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | } |
35 | 35 | |
36 | 36 | private static function pregReplaceCallbackWithOffsetImpl( |
37 | - $pattern, callable $callback, string $subject, int $limit = -1, int &$count = null) : string |
|
37 | + $pattern, callable $callback, string $subject, int $limit = -1, int & $count = null) : string |
|
38 | 38 | { |
39 | 39 | if (is_array($pattern)) { |
40 | 40 | throw new NotImplementedException(); // TODO |
@@ -27,8 +27,7 @@ |
||
27 | 27 | $result[] = self::pregReplaceCallbackWithOffsetImpl($pattern, $callback, $item, $limit, $count); |
28 | 28 | } |
29 | 29 | return $result; |
30 | - } |
|
31 | - else { |
|
30 | + } else { |
|
32 | 31 | return self::pregReplaceCallbackWithOffsetImpl($pattern, $callback, $subject, $limit, $count); |
33 | 32 | } |
34 | 33 | } |
@@ -84,7 +84,7 @@ |
||
84 | 84 | |
85 | 85 | /** |
86 | 86 | * @param mixed $other |
87 | - * @return bool whether this and the other composite value are of the identical type and contain the same data |
|
87 | + * @return null|boolean whether this and the other composite value are of the identical type and contain the same data |
|
88 | 88 | */ |
89 | 89 | public function equals($other) |
90 | 90 | { |
@@ -28,178 +28,178 @@ |
||
28 | 28 | */ |
29 | 29 | class Composite implements IComparable, \ArrayAccess, \IteratorAggregate |
30 | 30 | { |
31 | - /** @var CompositeType type of the composite */ |
|
32 | - private $type; |
|
33 | - /** @var array list of attribute values */ |
|
34 | - private $values; |
|
35 | - |
|
36 | - /** |
|
37 | - * Creates a new composite value out of a list of attributes values. |
|
38 | - * |
|
39 | - * @param CompositeType $type the type of the value |
|
40 | - * @param array $values list of values of corresponding attributes |
|
41 | - * @return Composite |
|
42 | - */ |
|
43 | - public static function fromList(CompositeType $type, $values) |
|
44 | - { |
|
45 | - return new Composite($type, $values); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Creates a new composite value out of a map of attribute names to the corresponding values. |
|
50 | - * |
|
51 | - * Attributes not mentioned in the given map are set to `null`. |
|
52 | - * |
|
53 | - * @param NamedCompositeType $type the type of the value; must be a named composite type |
|
54 | - * @param array $map map: attribute name => value; unspecified attributes get a <tt>null</tt> value |
|
55 | - * @return Composite |
|
56 | - */ |
|
57 | - public static function fromMap(NamedCompositeType $type, $map) |
|
58 | - { |
|
59 | - $values = array_fill(0, count($type->getAttributes()), null); |
|
60 | - foreach ($map as $k => $v) { |
|
61 | - $pos = $type->getAttPos($k); |
|
62 | - if ($pos !== null) { |
|
63 | - $values[$pos] = $v; |
|
64 | - } |
|
65 | - else { |
|
66 | - $typeName = "{$type->getSchemaName()}.{$type->getName()}"; |
|
67 | - $msg = "Error creating a composite value of type $typeName: key '$k' is undefined"; |
|
68 | - throw new \InvalidArgumentException($msg); |
|
69 | - } |
|
70 | - } |
|
71 | - return new Composite($type, $values); |
|
72 | - } |
|
73 | - |
|
74 | - private function __construct(CompositeType $type, $values) |
|
75 | - { |
|
76 | - $this->type = $type; |
|
77 | - $this->values = $values; |
|
78 | - } |
|
79 | - |
|
80 | - final public function getType() |
|
81 | - { |
|
82 | - return $this->type; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param mixed $other |
|
87 | - * @return bool whether this and the other composite value are of the identical type and contain the same data |
|
88 | - */ |
|
89 | - public function equals($other) |
|
90 | - { |
|
91 | - if ($other === null) { |
|
92 | - return null; |
|
93 | - } |
|
94 | - if (get_class($this) != get_class($other)) { |
|
95 | - return false; |
|
96 | - } |
|
97 | - if ($this->type !== $other->type) { |
|
98 | - return false; |
|
99 | - } |
|
100 | - |
|
101 | - return ValueUtils::equals($this->values, $other->values); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @return array list of the elementary values |
|
106 | - */ |
|
107 | - public function toList() |
|
108 | - { |
|
109 | - return $this->values; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return array ordered map: attribute name => attribute value |
|
114 | - * @throws UnsupportedException if called on a value of an ad hoc composite type, i.e., such that defines no |
|
115 | - * attributes |
|
116 | - */ |
|
117 | - public function toMap() |
|
118 | - { |
|
119 | - $attNames = array_keys($this->type->getAttributes()); |
|
120 | - if (!$attNames) { |
|
121 | - $msg = 'Ad hoc composite type value cannot be converted to map - no attributes are defined'; |
|
122 | - throw new UnsupportedException($msg); |
|
123 | - } |
|
124 | - $result = []; |
|
125 | - foreach ($this->values as $i => $v) { |
|
126 | - if (isset($attNames[$i])) { |
|
127 | - $k = $attNames[$i]; |
|
128 | - $result[$k] = $v; |
|
129 | - } |
|
130 | - } |
|
131 | - return $result; |
|
132 | - } |
|
133 | - |
|
134 | - //region dynamic properties |
|
135 | - |
|
136 | - /** |
|
137 | - * @param string $name attribute name |
|
138 | - * @return mixed value of the given attribute, or <tt>null</tt> if no such attribute is defined on this value |
|
139 | - */ |
|
140 | - public function __get($name) |
|
141 | - { |
|
142 | - $pos = $this->type->getAttPos($name); |
|
143 | - if ($pos !== null) { |
|
144 | - return $this->values[$pos]; |
|
145 | - } |
|
146 | - else { |
|
147 | - return null; |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @param string $name attribute name |
|
153 | - * @return bool whether the attribute is defined on this value |
|
154 | - */ |
|
155 | - public function __isset($name) |
|
156 | - { |
|
157 | - return ($this->type->getAttPos($name) !== null); |
|
158 | - } |
|
159 | - |
|
160 | - //endregion |
|
161 | - |
|
162 | - //region \IArrayAccess |
|
163 | - |
|
164 | - public function offsetExists($offset) |
|
165 | - { |
|
166 | - if (filter_var($offset, FILTER_VALIDATE_INT) !== false) { |
|
167 | - return ($offset < count($this->values)); |
|
168 | - } |
|
169 | - else { |
|
170 | - return ($this->type->getAttPos($offset) !== null); |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - public function offsetGet($offset) |
|
175 | - { |
|
176 | - if (filter_var($offset, FILTER_VALIDATE_INT) !== false) { |
|
177 | - return $this->values[$offset]; |
|
178 | - } |
|
179 | - else { |
|
180 | - return $this->values[$this->type->getAttPos($offset)]; |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - public function offsetSet($offset, $value) |
|
185 | - { |
|
186 | - throw new ImmutableException(); |
|
187 | - } |
|
188 | - |
|
189 | - public function offsetUnset($offset) |
|
190 | - { |
|
191 | - throw new ImmutableException(); |
|
192 | - } |
|
193 | - |
|
194 | - //endregion |
|
195 | - |
|
196 | - //region \IteratorAggregate |
|
197 | - |
|
198 | - public function getIterator() |
|
199 | - { |
|
200 | - $arr = ($this->type->getAttributes() ? $this->toMap() : $this->values); |
|
201 | - return new \ArrayIterator($arr); |
|
202 | - } |
|
203 | - |
|
204 | - //endregion |
|
31 | + /** @var CompositeType type of the composite */ |
|
32 | + private $type; |
|
33 | + /** @var array list of attribute values */ |
|
34 | + private $values; |
|
35 | + |
|
36 | + /** |
|
37 | + * Creates a new composite value out of a list of attributes values. |
|
38 | + * |
|
39 | + * @param CompositeType $type the type of the value |
|
40 | + * @param array $values list of values of corresponding attributes |
|
41 | + * @return Composite |
|
42 | + */ |
|
43 | + public static function fromList(CompositeType $type, $values) |
|
44 | + { |
|
45 | + return new Composite($type, $values); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Creates a new composite value out of a map of attribute names to the corresponding values. |
|
50 | + * |
|
51 | + * Attributes not mentioned in the given map are set to `null`. |
|
52 | + * |
|
53 | + * @param NamedCompositeType $type the type of the value; must be a named composite type |
|
54 | + * @param array $map map: attribute name => value; unspecified attributes get a <tt>null</tt> value |
|
55 | + * @return Composite |
|
56 | + */ |
|
57 | + public static function fromMap(NamedCompositeType $type, $map) |
|
58 | + { |
|
59 | + $values = array_fill(0, count($type->getAttributes()), null); |
|
60 | + foreach ($map as $k => $v) { |
|
61 | + $pos = $type->getAttPos($k); |
|
62 | + if ($pos !== null) { |
|
63 | + $values[$pos] = $v; |
|
64 | + } |
|
65 | + else { |
|
66 | + $typeName = "{$type->getSchemaName()}.{$type->getName()}"; |
|
67 | + $msg = "Error creating a composite value of type $typeName: key '$k' is undefined"; |
|
68 | + throw new \InvalidArgumentException($msg); |
|
69 | + } |
|
70 | + } |
|
71 | + return new Composite($type, $values); |
|
72 | + } |
|
73 | + |
|
74 | + private function __construct(CompositeType $type, $values) |
|
75 | + { |
|
76 | + $this->type = $type; |
|
77 | + $this->values = $values; |
|
78 | + } |
|
79 | + |
|
80 | + final public function getType() |
|
81 | + { |
|
82 | + return $this->type; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param mixed $other |
|
87 | + * @return bool whether this and the other composite value are of the identical type and contain the same data |
|
88 | + */ |
|
89 | + public function equals($other) |
|
90 | + { |
|
91 | + if ($other === null) { |
|
92 | + return null; |
|
93 | + } |
|
94 | + if (get_class($this) != get_class($other)) { |
|
95 | + return false; |
|
96 | + } |
|
97 | + if ($this->type !== $other->type) { |
|
98 | + return false; |
|
99 | + } |
|
100 | + |
|
101 | + return ValueUtils::equals($this->values, $other->values); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @return array list of the elementary values |
|
106 | + */ |
|
107 | + public function toList() |
|
108 | + { |
|
109 | + return $this->values; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return array ordered map: attribute name => attribute value |
|
114 | + * @throws UnsupportedException if called on a value of an ad hoc composite type, i.e., such that defines no |
|
115 | + * attributes |
|
116 | + */ |
|
117 | + public function toMap() |
|
118 | + { |
|
119 | + $attNames = array_keys($this->type->getAttributes()); |
|
120 | + if (!$attNames) { |
|
121 | + $msg = 'Ad hoc composite type value cannot be converted to map - no attributes are defined'; |
|
122 | + throw new UnsupportedException($msg); |
|
123 | + } |
|
124 | + $result = []; |
|
125 | + foreach ($this->values as $i => $v) { |
|
126 | + if (isset($attNames[$i])) { |
|
127 | + $k = $attNames[$i]; |
|
128 | + $result[$k] = $v; |
|
129 | + } |
|
130 | + } |
|
131 | + return $result; |
|
132 | + } |
|
133 | + |
|
134 | + //region dynamic properties |
|
135 | + |
|
136 | + /** |
|
137 | + * @param string $name attribute name |
|
138 | + * @return mixed value of the given attribute, or <tt>null</tt> if no such attribute is defined on this value |
|
139 | + */ |
|
140 | + public function __get($name) |
|
141 | + { |
|
142 | + $pos = $this->type->getAttPos($name); |
|
143 | + if ($pos !== null) { |
|
144 | + return $this->values[$pos]; |
|
145 | + } |
|
146 | + else { |
|
147 | + return null; |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @param string $name attribute name |
|
153 | + * @return bool whether the attribute is defined on this value |
|
154 | + */ |
|
155 | + public function __isset($name) |
|
156 | + { |
|
157 | + return ($this->type->getAttPos($name) !== null); |
|
158 | + } |
|
159 | + |
|
160 | + //endregion |
|
161 | + |
|
162 | + //region \IArrayAccess |
|
163 | + |
|
164 | + public function offsetExists($offset) |
|
165 | + { |
|
166 | + if (filter_var($offset, FILTER_VALIDATE_INT) !== false) { |
|
167 | + return ($offset < count($this->values)); |
|
168 | + } |
|
169 | + else { |
|
170 | + return ($this->type->getAttPos($offset) !== null); |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + public function offsetGet($offset) |
|
175 | + { |
|
176 | + if (filter_var($offset, FILTER_VALIDATE_INT) !== false) { |
|
177 | + return $this->values[$offset]; |
|
178 | + } |
|
179 | + else { |
|
180 | + return $this->values[$this->type->getAttPos($offset)]; |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + public function offsetSet($offset, $value) |
|
185 | + { |
|
186 | + throw new ImmutableException(); |
|
187 | + } |
|
188 | + |
|
189 | + public function offsetUnset($offset) |
|
190 | + { |
|
191 | + throw new ImmutableException(); |
|
192 | + } |
|
193 | + |
|
194 | + //endregion |
|
195 | + |
|
196 | + //region \IteratorAggregate |
|
197 | + |
|
198 | + public function getIterator() |
|
199 | + { |
|
200 | + $arr = ($this->type->getAttributes() ? $this->toMap() : $this->values); |
|
201 | + return new \ArrayIterator($arr); |
|
202 | + } |
|
203 | + |
|
204 | + //endregion |
|
205 | 205 | } |
@@ -61,8 +61,7 @@ discard block |
||
61 | 61 | $pos = $type->getAttPos($k); |
62 | 62 | if ($pos !== null) { |
63 | 63 | $values[$pos] = $v; |
64 | - } |
|
65 | - else { |
|
64 | + } else { |
|
66 | 65 | $typeName = "{$type->getSchemaName()}.{$type->getName()}"; |
67 | 66 | $msg = "Error creating a composite value of type $typeName: key '$k' is undefined"; |
68 | 67 | throw new \InvalidArgumentException($msg); |
@@ -142,8 +141,7 @@ discard block |
||
142 | 141 | $pos = $this->type->getAttPos($name); |
143 | 142 | if ($pos !== null) { |
144 | 143 | return $this->values[$pos]; |
145 | - } |
|
146 | - else { |
|
144 | + } else { |
|
147 | 145 | return null; |
148 | 146 | } |
149 | 147 | } |
@@ -165,8 +163,7 @@ discard block |
||
165 | 163 | { |
166 | 164 | if (filter_var($offset, FILTER_VALIDATE_INT) !== false) { |
167 | 165 | return ($offset < count($this->values)); |
168 | - } |
|
169 | - else { |
|
166 | + } else { |
|
170 | 167 | return ($this->type->getAttPos($offset) !== null); |
171 | 168 | } |
172 | 169 | } |
@@ -175,8 +172,7 @@ discard block |
||
175 | 172 | { |
176 | 173 | if (filter_var($offset, FILTER_VALIDATE_INT) !== false) { |
177 | 174 | return $this->values[$offset]; |
178 | - } |
|
179 | - else { |
|
175 | + } else { |
|
180 | 176 | return $this->values[$this->type->getAttPos($offset)]; |
181 | 177 | } |
182 | 178 | } |
@@ -56,6 +56,7 @@ discard block |
||
56 | 56 | |
57 | 57 | /** |
58 | 58 | * @internal Only for the purpose of Ivory itself. |
59 | + * @param integer $inf |
|
59 | 60 | */ |
60 | 61 | final protected function __construct($inf, \DateTimeImmutable $dt = null) |
61 | 62 | { |
@@ -178,7 +179,7 @@ discard block |
||
178 | 179 | |
179 | 180 | /** |
180 | 181 | * @param \DateTimeZone $timezone timezone to create the {@link \DateTime} object with |
181 | - * @return \DateTime|null the date/time represented as a {@link \DateTimeImmutable} object; |
|
182 | + * @return null|\DateTimeImmutable the date/time represented as a {@link \DateTimeImmutable} object; |
|
182 | 183 | * <tt>null</tt> iff the date/time is not finite |
183 | 184 | */ |
184 | 185 | public function toDateTimeImmutable(\DateTimeZone $timezone = null) |
@@ -233,6 +234,13 @@ discard block |
||
233 | 234 | } |
234 | 235 | |
235 | 236 | |
237 | + /** |
|
238 | + * @param integer $years |
|
239 | + * @param integer $months |
|
240 | + * @param integer $days |
|
241 | + * @param integer $hours |
|
242 | + * @param integer $minutes |
|
243 | + */ |
|
236 | 244 | final protected function addPartsImpl($years, $months, $days, $hours, $minutes, $seconds) |
237 | 245 | { |
238 | 246 | if ($this->inf) { |
@@ -87,8 +87,7 @@ discard block |
||
87 | 87 | $z = $this->getZeroBasedYear(); |
88 | 88 | if ($z > 0 || $z === null) { |
89 | 89 | return $z; |
90 | - } |
|
91 | - else { |
|
90 | + } else { |
|
92 | 91 | return $z - 1; |
93 | 92 | } |
94 | 93 | } |
@@ -132,8 +131,7 @@ discard block |
||
132 | 131 | { |
133 | 132 | if ($this->inf) { |
134 | 133 | return null; |
135 | - } |
|
136 | - else { |
|
134 | + } else { |
|
137 | 135 | return $this->dt->format($dateFmt); |
138 | 136 | } |
139 | 137 | } |
@@ -185,12 +183,10 @@ discard block |
||
185 | 183 | { |
186 | 184 | if ($this->inf) { |
187 | 185 | return null; |
188 | - } |
|
189 | - else { |
|
186 | + } else { |
|
190 | 187 | if ($timezone === $this->dt->getTimezone()) { |
191 | 188 | return $this->dt; |
192 | - } |
|
193 | - else { |
|
189 | + } else { |
|
194 | 190 | return new \DateTimeImmutable($this->toISOString(), $timezone); |
195 | 191 | } |
196 | 192 | } |
@@ -253,8 +249,7 @@ discard block |
||
253 | 249 | if ($resFracSec < 0) { |
254 | 250 | $resFracSec++; |
255 | 251 | $wholeSec--; |
256 | - } |
|
257 | - elseif ($resFracSec >= 1) { |
|
252 | + } elseif ($resFracSec >= 1) { |
|
258 | 253 | $resFracSec--; |
259 | 254 | $wholeSec++; |
260 | 255 | } |
@@ -266,8 +266,8 @@ discard block |
||
266 | 266 | * which might or might not be desired. Such a difference only arises in the trailing fractional zero digits, |
267 | 267 | * though. |
268 | 268 | * |
269 | - * @param string|int|float|Decimal|object $number number to compare this number with |
|
270 | - * @return bool whether this number numerically equals to <tt>$number</tt> |
|
269 | + * @param Decimal|null $number number to compare this number with |
|
270 | + * @return null|boolean whether this number numerically equals to <tt>$number</tt> |
|
271 | 271 | */ |
272 | 272 | public function equals($number) |
273 | 273 | { |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | } |
288 | 288 | |
289 | 289 | /** |
290 | - * @param string|int|float|Decimal|object $number |
|
290 | + * @param Decimal $number |
|
291 | 291 | * @return bool <tt>true</tt> iff this number is numerically greater than <tt>$number</tt> |
292 | 292 | */ |
293 | 293 | public function greaterThan($number) |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | } |
328 | 328 | |
329 | 329 | /** |
330 | - * @return bool whether this is zero |
|
330 | + * @return null|boolean whether this is zero |
|
331 | 331 | */ |
332 | 332 | public function isZero() |
333 | 333 | { |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | } |
376 | 376 | |
377 | 377 | /** |
378 | - * @param string|int|float|Decimal|object $augend |
|
378 | + * @param integer $augend |
|
379 | 379 | * @return Decimal a new decimal number, representing the result of sum of this and the given number |
380 | 380 | */ |
381 | 381 | public function add($augend) |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | } |
393 | 393 | |
394 | 394 | /** |
395 | - * @param string|int|float|Decimal|object $subtrahend |
|
395 | + * @param Decimal $subtrahend |
|
396 | 396 | * @return Decimal a new decimal number, representing the result of subtraction of this and the given number |
397 | 397 | */ |
398 | 398 | public function subtract($subtrahend) |
@@ -409,7 +409,7 @@ discard block |
||
409 | 409 | } |
410 | 410 | |
411 | 411 | /** |
412 | - * @param string|int|float|Decimal|object $multiplicand |
|
412 | + * @param Decimal $multiplicand |
|
413 | 413 | * @return Decimal a new decimal number, representing the result of multiplication of this and the given number |
414 | 414 | */ |
415 | 415 | public function multiply($multiplicand) |
@@ -426,7 +426,7 @@ discard block |
||
426 | 426 | } |
427 | 427 | |
428 | 428 | /** |
429 | - * @param string|int|float|Decimal|object $divisor |
|
429 | + * @param Decimal $divisor |
|
430 | 430 | * @return Decimal a new decimal number, representing the result of division of this number with the given number |
431 | 431 | * @throws \RuntimeException if <tt>$divisor</tt> is zero |
432 | 432 | */ |
@@ -32,686 +32,686 @@ |
||
32 | 32 | */ |
33 | 33 | class Decimal implements IComparable |
34 | 34 | { |
35 | - /** Maximal number of decimal digits considered by PostgreSQL. */ |
|
36 | - const SCALE_LIMIT = 16383; |
|
37 | - /** |
|
38 | - * Minimal number of significant digits for inexact calculations, like division or square root. It reflects the |
|
39 | - * behaviour of PostgreSQL. |
|
40 | - */ |
|
41 | - const MIN_SIG_DIGITS = 16; |
|
42 | - |
|
43 | - /** @var string|null string of decimal digits, including the sign and decimal point; null for the not-a-number */ |
|
44 | - private $val; |
|
45 | - /** @var int number of decimal digits in the fractional part, to the right of the decimal point (if any) */ |
|
46 | - private $scale; |
|
47 | - |
|
48 | - /** |
|
49 | - * @param string|int|float|Decimal|object $decimalNumber the value of the decimal number; |
|
50 | - * leading zeros are ignored (even after the minus sign), as |
|
51 | - * well as leading and trailing whitespace; |
|
52 | - * if an object is passed, it gets cast to string (which works |
|
53 | - * for {@link \GMP} objects, besides others) |
|
54 | - * @param int|null $scale the requested scale (i.e., the number of decimal digits in the fractional part) of the |
|
55 | - * number; |
|
56 | - * must be non-negative, or null; |
|
57 | - * if smaller than the scale of the given number, the number gets mathematically rounded to |
|
58 | - * the requested scale; |
|
59 | - * if greater than the scale of the given number, the number gets padded to have such many |
|
60 | - * decimal places; |
|
61 | - * if not given, it is computed from the given number automatically |
|
62 | - * @return Decimal |
|
63 | - */ |
|
64 | - public static function fromNumber($decimalNumber, $scale = null) |
|
65 | - { |
|
66 | - if ($decimalNumber === null) { |
|
67 | - throw new \InvalidArgumentException('decimalNumber'); |
|
68 | - } |
|
69 | - if ($scale < 0) { |
|
70 | - throw new \InvalidArgumentException('scale'); |
|
71 | - } |
|
72 | - if ($decimalNumber instanceof Decimal) { |
|
73 | - if ($scale === null || $scale == $decimalNumber->scale) { |
|
74 | - return $decimalNumber; |
|
75 | - } |
|
76 | - else { |
|
77 | - return $decimalNumber->round($scale); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - if (is_int($decimalNumber)) { |
|
82 | - $val = (string)$decimalNumber; |
|
83 | - } |
|
84 | - elseif (is_float($decimalNumber)) { |
|
85 | - $val = (string)$decimalNumber; |
|
86 | - $ePos = stripos($val, 'e'); |
|
87 | - if ($ePos > 0) { |
|
88 | - $exp = (int)substr($val, $ePos + 1); |
|
89 | - $decs = substr($val, 0, $ePos); |
|
90 | - list($wp, $dp) = explode('.', $decs, 2); |
|
91 | - |
|
92 | - if ($exp >= 0) { |
|
93 | - $dpLen = strlen($dp); |
|
94 | - if ($exp >= $dpLen) { |
|
95 | - $val = $wp . $dp . str_repeat('0', $exp - $dpLen); |
|
96 | - } |
|
97 | - else { |
|
98 | - $val = $wp . substr($dp, 0, $exp) . '.' . substr($dp, $exp); |
|
99 | - } |
|
100 | - } |
|
101 | - else { |
|
102 | - $mn = ($wp[0] == '-' ? 1 : 0); |
|
103 | - $ord = strlen($wp) - $mn; |
|
104 | - if (-$exp >= $ord) { |
|
105 | - $prefix = ($mn ? '-' : '') . '0.' . str_repeat('0', -$exp - $ord); |
|
106 | - if (($mn ? ($wp != '-0') : ($wp != '0'))) { |
|
107 | - $prefix .= substr($wp, $mn); |
|
108 | - } |
|
109 | - $val = $prefix . $dp; |
|
110 | - } |
|
111 | - else { |
|
112 | - $val = substr($wp, 0, $mn + $ord + $exp) . '.' . substr($wp, $mn + $ord + $exp) . $dp; |
|
113 | - } |
|
114 | - } |
|
115 | - } |
|
116 | - } |
|
117 | - else { |
|
118 | - if (!preg_match('~^\s*(-?)(?|(\.[0-9]+)|0*([0-9]+(?:\.[0-9]*)?))\s*$~', (string)$decimalNumber, $m)) { |
|
119 | - throw new \InvalidArgumentException('decimalNumber'); |
|
120 | - } |
|
121 | - $val = $m[1] . $m[2]; // cuts off unnecessary leading zeros |
|
122 | - } |
|
123 | - |
|
124 | - return new Decimal($val, $scale); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Gets the not-a-number special value. Returns the same object every time. |
|
129 | - * |
|
130 | - * It only equals to the not-a-number, and is greater than any number value. The result of any operation is, again, |
|
131 | - * the not-a-number. |
|
132 | - * |
|
133 | - * @return Decimal the not-a-number value |
|
134 | - */ |
|
135 | - public static function NaN() |
|
136 | - { |
|
137 | - static $dec = null; |
|
138 | - if ($dec === null) { |
|
139 | - $dec = new Decimal(null); |
|
140 | - } |
|
141 | - return $dec; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Gets the decimal object representing the number zero. Returns the same object every time. |
|
146 | - * |
|
147 | - * @return Decimal |
|
148 | - */ |
|
149 | - public static function zero() |
|
150 | - { |
|
151 | - static $dec = null; |
|
152 | - if ($dec === null) { |
|
153 | - $dec = new Decimal(0); |
|
154 | - } |
|
155 | - return $dec; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * NOTE: The constructor is kept private so that extra sanity checks are performed on user input, while trusting the |
|
160 | - * results of the operations without these extra checks. |
|
161 | - * |
|
162 | - * @param string|null $val |
|
163 | - * @param int|null $scale |
|
164 | - */ |
|
165 | - private function __construct($val, $scale = null) |
|
166 | - { |
|
167 | - if ($val === null) { |
|
168 | - $this->val = null; |
|
169 | - $this->scale = null; |
|
170 | - return; |
|
171 | - } |
|
172 | - |
|
173 | - $this->val = (string)$val; |
|
174 | - |
|
175 | - $decPoint = strpos($this->val, '.'); |
|
176 | - if ($scale < 0) { |
|
177 | - $this->scale = 0; |
|
178 | - $minus = ($this->val[0] == '-' ? 1 : 0); |
|
179 | - if ($decPoint !== false) { |
|
180 | - $digits = substr($this->val, $minus, $decPoint - $minus); |
|
181 | - } |
|
182 | - else { |
|
183 | - $digits = ($minus ? substr($this->val, $minus) : $this->val); |
|
184 | - } |
|
185 | - $len = strlen($digits); |
|
186 | - $sigPos = $len + $scale; |
|
187 | - if ($sigPos < 0) { |
|
188 | - $this->val = '0'; |
|
189 | - return; |
|
190 | - } |
|
191 | - $inc = ($digits[$sigPos] >= 5); |
|
192 | - if (!$inc && $sigPos == 0) { |
|
193 | - $this->val = '0'; |
|
194 | - return; |
|
195 | - } |
|
196 | - for ($i = $sigPos; $i < $len; $i++) { |
|
197 | - $digits[$i] = '0'; |
|
198 | - } |
|
199 | - if ($inc) { |
|
200 | - $augend = '1' . str_repeat('0', -$scale); |
|
201 | - $digits = bcadd($digits, $augend, 0); |
|
202 | - } |
|
203 | - $this->val = ($minus ? '-' : '') . $digits; |
|
204 | - } |
|
205 | - elseif ($decPoint === false) { |
|
206 | - if ($scale > 0) { |
|
207 | - $this->val .= '.' . str_repeat('0', $scale); |
|
208 | - $this->scale = $scale; |
|
209 | - } |
|
210 | - else { |
|
211 | - $this->scale = 0; |
|
212 | - } |
|
213 | - } |
|
214 | - else { |
|
215 | - $len = strlen($this->val); |
|
216 | - $this->scale = $len - $decPoint - 1; |
|
217 | - $scale = min(self::SCALE_LIMIT, ($scale === null ? $this->scale : max(0, $scale))); |
|
218 | - if ($this->scale < $scale) { |
|
219 | - $this->val .= str_repeat('0', $scale - $this->scale); |
|
220 | - $this->scale = $scale; |
|
221 | - } |
|
222 | - elseif ($this->scale > $scale) { |
|
223 | - $newLen = $len - ($this->scale - $scale); |
|
224 | - $inc = ($this->val[$newLen] >= 5); |
|
225 | - if ($scale == 0) { |
|
226 | - $newLen--; |
|
227 | - } |
|
228 | - $this->val = substr($this->val, 0, $newLen); |
|
229 | - if ($inc) { |
|
230 | - $mn = ($this->val[0] == '-' ? '-' : ''); |
|
231 | - $augend = $mn . ($scale == 0 ? '1' : '.' . str_repeat('0', $scale - 1) . '1'); |
|
232 | - $this->val = bcadd($this->val, $augend, $scale); |
|
233 | - } |
|
234 | - $this->scale = $scale; |
|
235 | - } |
|
236 | - |
|
237 | - if ($this->val[0] == '.') { |
|
238 | - $this->val = '0' . $this->val; |
|
239 | - } |
|
240 | - elseif ($this->val[0] == '-' && $this->val[1] == '.') { |
|
241 | - $this->val[0] = '0'; |
|
242 | - $this->val = '-' . $this->val; |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - // eliminate negative zero - PostgreSQL does not differentiate between zeros |
|
247 | - if ($this->val[0] == '-' && strspn($this->val, '-0.') == strlen($this->val)) { |
|
248 | - $this->val = substr($this->val, 1); |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - /** |
|
254 | - * @return int|null number of decimal digits in the fractional part, to the right of the decimal point (if any), or |
|
255 | - * <tt>null</tt> for the not-a-number |
|
256 | - */ |
|
257 | - public function getScale() |
|
258 | - { |
|
259 | - return $this->scale; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Compare this number numerically with another number. |
|
264 | - * |
|
265 | - * Note that using the `==` operator checks that the two {@link Decimal} objects are of the same value and scale, |
|
266 | - * which might or might not be desired. Such a difference only arises in the trailing fractional zero digits, |
|
267 | - * though. |
|
268 | - * |
|
269 | - * @param string|int|float|Decimal|object $number number to compare this number with |
|
270 | - * @return bool whether this number numerically equals to <tt>$number</tt> |
|
271 | - */ |
|
272 | - public function equals($number) |
|
273 | - { |
|
274 | - if ($number === null) { |
|
275 | - return null; |
|
276 | - } |
|
277 | - return ($this->compareTo($number) == 0); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * @param string|int|float|Decimal|object $number |
|
282 | - * @return bool <tt>true</tt> iff this number is numerically less than <tt>$number</tt> |
|
283 | - */ |
|
284 | - public function lessThan($number) |
|
285 | - { |
|
286 | - return ($this->compareTo($number) < 0); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * @param string|int|float|Decimal|object $number |
|
291 | - * @return bool <tt>true</tt> iff this number is numerically greater than <tt>$number</tt> |
|
292 | - */ |
|
293 | - public function greaterThan($number) |
|
294 | - { |
|
295 | - return ($this->compareTo($number) > 0); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * @param string|int|float|Decimal|object $number number to compare this number with |
|
300 | - * @return int -1, 0, or 1 if this number is numerically less than, equal to, or greater than <tt>$number</tt> |
|
301 | - */ |
|
302 | - public function compareTo($number) |
|
303 | - { |
|
304 | - $arg = self::fromNumber($number); |
|
305 | - if ($this->isNaN()) { |
|
306 | - if ($arg->isNaN()) { |
|
307 | - return 0; |
|
308 | - } |
|
309 | - else { |
|
310 | - return 1; |
|
311 | - } |
|
312 | - } |
|
313 | - elseif ($arg->isNaN()) { |
|
314 | - return -1; |
|
315 | - } |
|
316 | - else { |
|
317 | - return bccomp($this->val, $arg->val, max($this->scale, $arg->scale)); |
|
318 | - } |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * @return bool whether this is a not-a-number |
|
323 | - */ |
|
324 | - public function isNaN() |
|
325 | - { |
|
326 | - return ($this->val === null); |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * @return bool whether this is zero |
|
331 | - */ |
|
332 | - public function isZero() |
|
333 | - { |
|
334 | - return ($this->equals(self::zero())); |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @return bool whether this is a positive number |
|
339 | - */ |
|
340 | - public function isPositive() |
|
341 | - { |
|
342 | - return (!$this->isNaN() && $this->compareTo(self::zero()) > 0); |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * @return bool whether this is a negative number |
|
347 | - */ |
|
348 | - public function isNegative() |
|
349 | - { |
|
350 | - return (!$this->isNaN() && $this->val[0] == '-'); |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * @return bool whether this is an integer, i.e., a number without decimal part (or the decimal part equal to zero) |
|
355 | - */ |
|
356 | - public function isInteger() |
|
357 | - { |
|
358 | - if ($this->isNaN()) { |
|
359 | - return false; |
|
360 | - } |
|
361 | - if (strpos($this->val, '.') === false) { |
|
362 | - return true; |
|
363 | - } |
|
364 | - for ($i = strlen($this->val) - 1; ; $i--) { |
|
365 | - switch ($this->val[$i]) { |
|
366 | - case '.': |
|
367 | - return true; |
|
368 | - case '0': |
|
369 | - break; |
|
370 | - default: |
|
371 | - return false; |
|
372 | - } |
|
373 | - } |
|
374 | - return true; |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * @param string|int|float|Decimal|object $augend |
|
379 | - * @return Decimal a new decimal number, representing the result of sum of this and the given number |
|
380 | - */ |
|
381 | - public function add($augend) |
|
382 | - { |
|
383 | - if ($this->isNaN()) { |
|
384 | - return $this; |
|
385 | - } |
|
386 | - $arg = self::fromNumber($augend); |
|
387 | - if ($arg->isNaN()) { |
|
388 | - return $arg; |
|
389 | - } |
|
390 | - $scale = max($this->scale, $arg->scale); |
|
391 | - return new Decimal(bcadd($this->val, $arg->val, $scale + 1), $scale); |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * @param string|int|float|Decimal|object $subtrahend |
|
396 | - * @return Decimal a new decimal number, representing the result of subtraction of this and the given number |
|
397 | - */ |
|
398 | - public function subtract($subtrahend) |
|
399 | - { |
|
400 | - if ($this->isNaN()) { |
|
401 | - return $this; |
|
402 | - } |
|
403 | - $arg = self::fromNumber($subtrahend); |
|
404 | - if ($arg->isNaN()) { |
|
405 | - return $arg; |
|
406 | - } |
|
407 | - $scale = max($this->scale, $arg->scale); |
|
408 | - return new Decimal(bcsub($this->val, $arg->val, $scale + 1), $scale); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * @param string|int|float|Decimal|object $multiplicand |
|
413 | - * @return Decimal a new decimal number, representing the result of multiplication of this and the given number |
|
414 | - */ |
|
415 | - public function multiply($multiplicand) |
|
416 | - { |
|
417 | - if ($this->isNaN()) { |
|
418 | - return $this; |
|
419 | - } |
|
420 | - $arg = self::fromNumber($multiplicand); |
|
421 | - if ($arg->isNaN()) { |
|
422 | - return $arg; |
|
423 | - } |
|
424 | - $scale = min($this->scale + $arg->scale, self::SCALE_LIMIT); |
|
425 | - return new Decimal(bcmul($this->val, $arg->val, $scale + 1), $scale); |
|
426 | - } |
|
427 | - |
|
428 | - /** |
|
429 | - * @param string|int|float|Decimal|object $divisor |
|
430 | - * @return Decimal a new decimal number, representing the result of division of this number with the given number |
|
431 | - * @throws \RuntimeException if <tt>$divisor</tt> is zero |
|
432 | - */ |
|
433 | - public function divide($divisor) |
|
434 | - { |
|
435 | - if ($this->isNaN()) { |
|
436 | - return $this; |
|
437 | - } |
|
438 | - $arg = self::fromNumber($divisor); |
|
439 | - if ($arg->isNaN()) { |
|
440 | - return $arg; |
|
441 | - } |
|
442 | - $scale = max($this->scale, $arg->scale, self::MIN_SIG_DIGITS); |
|
443 | - return new Decimal(bcdiv($this->val, $arg->val, $scale + 1), $scale); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * @param string|int|float|Decimal|object $modulus |
|
448 | - * @return Decimal remainder of this number divided by <tt>$modulus</tt> |
|
449 | - * @throws \RuntimeException if <tt>$modulus</tt> is zero |
|
450 | - */ |
|
451 | - public function mod($modulus) |
|
452 | - { |
|
453 | - if ($this->isNaN()) { |
|
454 | - return $this; |
|
455 | - } |
|
456 | - $arg = self::fromNumber($modulus); |
|
457 | - if ($arg->isNaN()) { |
|
458 | - return $arg; |
|
459 | - } |
|
460 | - if ($arg->isZero()) { |
|
461 | - throw new \RuntimeException('Division by zero'); |
|
462 | - } |
|
463 | - |
|
464 | - // NOTE: bcmod() only calculates integer modulus |
|
465 | - $a = $this->abs(); |
|
466 | - $b = $arg->abs(); |
|
467 | - $m = $a->subtract($b->multiply($a->divide($b)->floor())); |
|
468 | - if ($this->isNegative()) { |
|
469 | - return $m->negate(); |
|
470 | - } |
|
471 | - else { |
|
472 | - return $m; |
|
473 | - } |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * @todo more precise calculation of fractional powers - now only double precision is used |
|
478 | - * @param string|int|float|Decimal|object $power |
|
479 | - * @return Decimal this number powered to <tt>$number</tt> |
|
480 | - * @throws \RuntimeException if this number is negative while <tt>$power</tt> is non-integer (that would lead to a |
|
481 | - * complex result) |
|
482 | - */ |
|
483 | - public function pow($power) |
|
484 | - { |
|
485 | - if ($this->isNaN()) { |
|
486 | - return $this; |
|
487 | - } |
|
488 | - $arg = self::fromNumber($power); |
|
489 | - if ($arg->isNaN()) { |
|
490 | - return $arg; |
|
491 | - } |
|
492 | - |
|
493 | - $scale = max($this->scale, $arg->scale, self::MIN_SIG_DIGITS); |
|
494 | - |
|
495 | - // NOTE: bcpow() only takes integer powers |
|
496 | - if ($arg->isInteger()) { |
|
497 | - return new Decimal(bcpow($this->val, $arg->val, $scale + 1), $scale); |
|
498 | - } |
|
499 | - else { |
|
500 | - if ($this->isNegative()) { |
|
501 | - throw new \RuntimeException('Negative number raised to a non-integer power yields a complex result'); |
|
502 | - } |
|
503 | - return self::fromNumber(pow($this->toFloat(), $arg->toFloat())); |
|
504 | - } |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * @return Decimal the absolute value of this number |
|
509 | - */ |
|
510 | - public function abs() |
|
511 | - { |
|
512 | - if ($this->isNegative()) { |
|
513 | - return $this->negate(); |
|
514 | - } |
|
515 | - else { |
|
516 | - return $this; |
|
517 | - } |
|
518 | - } |
|
519 | - |
|
520 | - /** |
|
521 | - * @return Decimal a new decimal number, representing the negative value of this number |
|
522 | - */ |
|
523 | - public function negate() |
|
524 | - { |
|
525 | - if ($this->isNaN() || $this->isZero()) { |
|
526 | - return $this; |
|
527 | - } |
|
528 | - elseif ($this->val[0] == '-') { |
|
529 | - return new Decimal(substr($this->val, 1)); |
|
530 | - } |
|
531 | - else { |
|
532 | - return new Decimal('-' . $this->val); |
|
533 | - } |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * Computes the exact factorial of this number. |
|
538 | - * Only defined on numbers of scale zero, i.e., those without decimal part, and on the not-a-number. |
|
539 | - * |
|
540 | - * In conformance with PostgreSQL, the factorial of non-positive numbers is defined to be 1. |
|
541 | - * |
|
542 | - * @return Decimal the factorial of this number |
|
543 | - * @throws UndefinedOperationException if this number is neither a zero-scale number nor the not-a-number |
|
544 | - */ |
|
545 | - public function factorial() |
|
546 | - { |
|
547 | - if ($this->isNaN()) { |
|
548 | - return $this; |
|
549 | - } |
|
550 | - if ($this->scale > 0) { |
|
551 | - throw new UndefinedOperationException('Number with a decimal part'); |
|
552 | - } |
|
553 | - if ($this->lessThan(2)) { |
|
554 | - return new Decimal(1); |
|
555 | - } |
|
556 | - |
|
557 | - if (System::hasGMP()) { |
|
558 | - return new Decimal(gmp_strval(gmp_fact($this->toGMP()))); |
|
559 | - } |
|
560 | - |
|
561 | - // OPT: there are more efficient algorithms calculating the factorial; see, e.g., http://www.luschny.de/math/factorial/index.html |
|
562 | - $result = new Decimal(2); |
|
563 | - for ($i = new Decimal(3); $i->compareTo($this) <= 0; $i = $i->add(1)) { |
|
564 | - $result = $result->multiply($i); |
|
565 | - } |
|
566 | - return $result; |
|
567 | - } |
|
568 | - |
|
569 | - /** |
|
570 | - * @param int $scale number of decimal places to round this number to; may be negative to round to higher orders |
|
571 | - * @return Decimal a new decimal number, representing this number rounded to <tt>$scale</tt> decimal places |
|
572 | - */ |
|
573 | - public function round($scale = 0) |
|
574 | - { |
|
575 | - return new Decimal($this->val, $scale); |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * @return Decimal largest integer not greater than this number |
|
580 | - */ |
|
581 | - public function floor() |
|
582 | - { |
|
583 | - $decPoint = strpos($this->val, '.'); |
|
584 | - if ($decPoint === false) { |
|
585 | - return $this; |
|
586 | - } |
|
587 | - if ($this->val[0] != '-') { |
|
588 | - return new Decimal(substr($this->val, 0, $decPoint)); |
|
589 | - } |
|
590 | - elseif ($this->isInteger()) { |
|
591 | - return new Decimal($this->val, 0); |
|
592 | - } |
|
593 | - else { |
|
594 | - // negative number with non-zero fractional part |
|
595 | - return new Decimal(bcsub(substr($this->val, 0, $decPoint), 1, 0)); |
|
596 | - } |
|
597 | - } |
|
598 | - |
|
599 | - /** |
|
600 | - * @return Decimal smallest integer not less than this number |
|
601 | - */ |
|
602 | - public function ceil() |
|
603 | - { |
|
604 | - $decPoint = strpos($this->val, '.'); |
|
605 | - if ($decPoint === false) { |
|
606 | - return $this; |
|
607 | - } |
|
608 | - if ($this->val[0] == '-') { |
|
609 | - return new Decimal(substr($this->val, 0, $decPoint)); |
|
610 | - } |
|
611 | - elseif ($this->isInteger()) { |
|
612 | - return new Decimal($this->val, 0); |
|
613 | - } |
|
614 | - else { |
|
615 | - // non-negative number with non-zero fractional part |
|
616 | - return new Decimal(bcadd(substr($this->val, 0, $decPoint), 1, 0)); |
|
617 | - } |
|
618 | - } |
|
619 | - |
|
620 | - /** |
|
621 | - * @param string|int|float|Decimal|object $number |
|
622 | - * @return Decimal the greater of this number and <tt>$number</tt>, preferably this number if numerically equal |
|
623 | - */ |
|
624 | - public function max($number) |
|
625 | - { |
|
626 | - $arg = self::fromNumber($number); |
|
627 | - if ($this->lessThan($arg)) { |
|
628 | - return $arg; |
|
629 | - } |
|
630 | - else { |
|
631 | - return $this; |
|
632 | - } |
|
633 | - } |
|
634 | - |
|
635 | - /** |
|
636 | - * @param string|int|float|Decimal|object $number |
|
637 | - * @return Decimal the smaller of this number and <tt>$number</tt>, preferably this number if numerically equal |
|
638 | - */ |
|
639 | - public function min($number) |
|
640 | - { |
|
641 | - $arg = self::fromNumber($number); |
|
642 | - if ($this->greaterThan($arg)) { |
|
643 | - return $arg; |
|
644 | - } |
|
645 | - else { |
|
646 | - return $this; |
|
647 | - } |
|
648 | - } |
|
649 | - |
|
650 | - /** |
|
651 | - * @return Decimal a new decimal number, representing the square root of this number |
|
652 | - * @throws UndefinedOperationException if this is a negative number |
|
653 | - */ |
|
654 | - public function sqrt() |
|
655 | - { |
|
656 | - if ($this->isNaN()) { |
|
657 | - return $this; |
|
658 | - } |
|
659 | - if ($this->isNegative()) { |
|
660 | - throw new UndefinedOperationException('square root of negative number'); |
|
661 | - } |
|
662 | - $scale = max($this->scale, self::MIN_SIG_DIGITS); |
|
663 | - return new Decimal(bcsqrt($this->val, $scale + 1)); |
|
664 | - } |
|
665 | - |
|
666 | - |
|
667 | - /** |
|
668 | - * @return int|null the value of this number cast explicitly to <tt>int</tt>; |
|
669 | - * for big numbers, this yields the maximal available integer value (i.e., <tt>PHP_INT_MAX</tt> or |
|
670 | - * <tt>PHP_INT_MIN</tt>); |
|
35 | + /** Maximal number of decimal digits considered by PostgreSQL. */ |
|
36 | + const SCALE_LIMIT = 16383; |
|
37 | + /** |
|
38 | + * Minimal number of significant digits for inexact calculations, like division or square root. It reflects the |
|
39 | + * behaviour of PostgreSQL. |
|
40 | + */ |
|
41 | + const MIN_SIG_DIGITS = 16; |
|
42 | + |
|
43 | + /** @var string|null string of decimal digits, including the sign and decimal point; null for the not-a-number */ |
|
44 | + private $val; |
|
45 | + /** @var int number of decimal digits in the fractional part, to the right of the decimal point (if any) */ |
|
46 | + private $scale; |
|
47 | + |
|
48 | + /** |
|
49 | + * @param string|int|float|Decimal|object $decimalNumber the value of the decimal number; |
|
50 | + * leading zeros are ignored (even after the minus sign), as |
|
51 | + * well as leading and trailing whitespace; |
|
52 | + * if an object is passed, it gets cast to string (which works |
|
53 | + * for {@link \GMP} objects, besides others) |
|
54 | + * @param int|null $scale the requested scale (i.e., the number of decimal digits in the fractional part) of the |
|
55 | + * number; |
|
56 | + * must be non-negative, or null; |
|
57 | + * if smaller than the scale of the given number, the number gets mathematically rounded to |
|
58 | + * the requested scale; |
|
59 | + * if greater than the scale of the given number, the number gets padded to have such many |
|
60 | + * decimal places; |
|
61 | + * if not given, it is computed from the given number automatically |
|
62 | + * @return Decimal |
|
63 | + */ |
|
64 | + public static function fromNumber($decimalNumber, $scale = null) |
|
65 | + { |
|
66 | + if ($decimalNumber === null) { |
|
67 | + throw new \InvalidArgumentException('decimalNumber'); |
|
68 | + } |
|
69 | + if ($scale < 0) { |
|
70 | + throw new \InvalidArgumentException('scale'); |
|
71 | + } |
|
72 | + if ($decimalNumber instanceof Decimal) { |
|
73 | + if ($scale === null || $scale == $decimalNumber->scale) { |
|
74 | + return $decimalNumber; |
|
75 | + } |
|
76 | + else { |
|
77 | + return $decimalNumber->round($scale); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + if (is_int($decimalNumber)) { |
|
82 | + $val = (string)$decimalNumber; |
|
83 | + } |
|
84 | + elseif (is_float($decimalNumber)) { |
|
85 | + $val = (string)$decimalNumber; |
|
86 | + $ePos = stripos($val, 'e'); |
|
87 | + if ($ePos > 0) { |
|
88 | + $exp = (int)substr($val, $ePos + 1); |
|
89 | + $decs = substr($val, 0, $ePos); |
|
90 | + list($wp, $dp) = explode('.', $decs, 2); |
|
91 | + |
|
92 | + if ($exp >= 0) { |
|
93 | + $dpLen = strlen($dp); |
|
94 | + if ($exp >= $dpLen) { |
|
95 | + $val = $wp . $dp . str_repeat('0', $exp - $dpLen); |
|
96 | + } |
|
97 | + else { |
|
98 | + $val = $wp . substr($dp, 0, $exp) . '.' . substr($dp, $exp); |
|
99 | + } |
|
100 | + } |
|
101 | + else { |
|
102 | + $mn = ($wp[0] == '-' ? 1 : 0); |
|
103 | + $ord = strlen($wp) - $mn; |
|
104 | + if (-$exp >= $ord) { |
|
105 | + $prefix = ($mn ? '-' : '') . '0.' . str_repeat('0', -$exp - $ord); |
|
106 | + if (($mn ? ($wp != '-0') : ($wp != '0'))) { |
|
107 | + $prefix .= substr($wp, $mn); |
|
108 | + } |
|
109 | + $val = $prefix . $dp; |
|
110 | + } |
|
111 | + else { |
|
112 | + $val = substr($wp, 0, $mn + $ord + $exp) . '.' . substr($wp, $mn + $ord + $exp) . $dp; |
|
113 | + } |
|
114 | + } |
|
115 | + } |
|
116 | + } |
|
117 | + else { |
|
118 | + if (!preg_match('~^\s*(-?)(?|(\.[0-9]+)|0*([0-9]+(?:\.[0-9]*)?))\s*$~', (string)$decimalNumber, $m)) { |
|
119 | + throw new \InvalidArgumentException('decimalNumber'); |
|
120 | + } |
|
121 | + $val = $m[1] . $m[2]; // cuts off unnecessary leading zeros |
|
122 | + } |
|
123 | + |
|
124 | + return new Decimal($val, $scale); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Gets the not-a-number special value. Returns the same object every time. |
|
129 | + * |
|
130 | + * It only equals to the not-a-number, and is greater than any number value. The result of any operation is, again, |
|
131 | + * the not-a-number. |
|
132 | + * |
|
133 | + * @return Decimal the not-a-number value |
|
134 | + */ |
|
135 | + public static function NaN() |
|
136 | + { |
|
137 | + static $dec = null; |
|
138 | + if ($dec === null) { |
|
139 | + $dec = new Decimal(null); |
|
140 | + } |
|
141 | + return $dec; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Gets the decimal object representing the number zero. Returns the same object every time. |
|
146 | + * |
|
147 | + * @return Decimal |
|
148 | + */ |
|
149 | + public static function zero() |
|
150 | + { |
|
151 | + static $dec = null; |
|
152 | + if ($dec === null) { |
|
153 | + $dec = new Decimal(0); |
|
154 | + } |
|
155 | + return $dec; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * NOTE: The constructor is kept private so that extra sanity checks are performed on user input, while trusting the |
|
160 | + * results of the operations without these extra checks. |
|
161 | + * |
|
162 | + * @param string|null $val |
|
163 | + * @param int|null $scale |
|
164 | + */ |
|
165 | + private function __construct($val, $scale = null) |
|
166 | + { |
|
167 | + if ($val === null) { |
|
168 | + $this->val = null; |
|
169 | + $this->scale = null; |
|
170 | + return; |
|
171 | + } |
|
172 | + |
|
173 | + $this->val = (string)$val; |
|
174 | + |
|
175 | + $decPoint = strpos($this->val, '.'); |
|
176 | + if ($scale < 0) { |
|
177 | + $this->scale = 0; |
|
178 | + $minus = ($this->val[0] == '-' ? 1 : 0); |
|
179 | + if ($decPoint !== false) { |
|
180 | + $digits = substr($this->val, $minus, $decPoint - $minus); |
|
181 | + } |
|
182 | + else { |
|
183 | + $digits = ($minus ? substr($this->val, $minus) : $this->val); |
|
184 | + } |
|
185 | + $len = strlen($digits); |
|
186 | + $sigPos = $len + $scale; |
|
187 | + if ($sigPos < 0) { |
|
188 | + $this->val = '0'; |
|
189 | + return; |
|
190 | + } |
|
191 | + $inc = ($digits[$sigPos] >= 5); |
|
192 | + if (!$inc && $sigPos == 0) { |
|
193 | + $this->val = '0'; |
|
194 | + return; |
|
195 | + } |
|
196 | + for ($i = $sigPos; $i < $len; $i++) { |
|
197 | + $digits[$i] = '0'; |
|
198 | + } |
|
199 | + if ($inc) { |
|
200 | + $augend = '1' . str_repeat('0', -$scale); |
|
201 | + $digits = bcadd($digits, $augend, 0); |
|
202 | + } |
|
203 | + $this->val = ($minus ? '-' : '') . $digits; |
|
204 | + } |
|
205 | + elseif ($decPoint === false) { |
|
206 | + if ($scale > 0) { |
|
207 | + $this->val .= '.' . str_repeat('0', $scale); |
|
208 | + $this->scale = $scale; |
|
209 | + } |
|
210 | + else { |
|
211 | + $this->scale = 0; |
|
212 | + } |
|
213 | + } |
|
214 | + else { |
|
215 | + $len = strlen($this->val); |
|
216 | + $this->scale = $len - $decPoint - 1; |
|
217 | + $scale = min(self::SCALE_LIMIT, ($scale === null ? $this->scale : max(0, $scale))); |
|
218 | + if ($this->scale < $scale) { |
|
219 | + $this->val .= str_repeat('0', $scale - $this->scale); |
|
220 | + $this->scale = $scale; |
|
221 | + } |
|
222 | + elseif ($this->scale > $scale) { |
|
223 | + $newLen = $len - ($this->scale - $scale); |
|
224 | + $inc = ($this->val[$newLen] >= 5); |
|
225 | + if ($scale == 0) { |
|
226 | + $newLen--; |
|
227 | + } |
|
228 | + $this->val = substr($this->val, 0, $newLen); |
|
229 | + if ($inc) { |
|
230 | + $mn = ($this->val[0] == '-' ? '-' : ''); |
|
231 | + $augend = $mn . ($scale == 0 ? '1' : '.' . str_repeat('0', $scale - 1) . '1'); |
|
232 | + $this->val = bcadd($this->val, $augend, $scale); |
|
233 | + } |
|
234 | + $this->scale = $scale; |
|
235 | + } |
|
236 | + |
|
237 | + if ($this->val[0] == '.') { |
|
238 | + $this->val = '0' . $this->val; |
|
239 | + } |
|
240 | + elseif ($this->val[0] == '-' && $this->val[1] == '.') { |
|
241 | + $this->val[0] = '0'; |
|
242 | + $this->val = '-' . $this->val; |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + // eliminate negative zero - PostgreSQL does not differentiate between zeros |
|
247 | + if ($this->val[0] == '-' && strspn($this->val, '-0.') == strlen($this->val)) { |
|
248 | + $this->val = substr($this->val, 1); |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + /** |
|
254 | + * @return int|null number of decimal digits in the fractional part, to the right of the decimal point (if any), or |
|
255 | + * <tt>null</tt> for the not-a-number |
|
256 | + */ |
|
257 | + public function getScale() |
|
258 | + { |
|
259 | + return $this->scale; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Compare this number numerically with another number. |
|
264 | + * |
|
265 | + * Note that using the `==` operator checks that the two {@link Decimal} objects are of the same value and scale, |
|
266 | + * which might or might not be desired. Such a difference only arises in the trailing fractional zero digits, |
|
267 | + * though. |
|
268 | + * |
|
269 | + * @param string|int|float|Decimal|object $number number to compare this number with |
|
270 | + * @return bool whether this number numerically equals to <tt>$number</tt> |
|
271 | + */ |
|
272 | + public function equals($number) |
|
273 | + { |
|
274 | + if ($number === null) { |
|
275 | + return null; |
|
276 | + } |
|
277 | + return ($this->compareTo($number) == 0); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * @param string|int|float|Decimal|object $number |
|
282 | + * @return bool <tt>true</tt> iff this number is numerically less than <tt>$number</tt> |
|
283 | + */ |
|
284 | + public function lessThan($number) |
|
285 | + { |
|
286 | + return ($this->compareTo($number) < 0); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * @param string|int|float|Decimal|object $number |
|
291 | + * @return bool <tt>true</tt> iff this number is numerically greater than <tt>$number</tt> |
|
292 | + */ |
|
293 | + public function greaterThan($number) |
|
294 | + { |
|
295 | + return ($this->compareTo($number) > 0); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * @param string|int|float|Decimal|object $number number to compare this number with |
|
300 | + * @return int -1, 0, or 1 if this number is numerically less than, equal to, or greater than <tt>$number</tt> |
|
301 | + */ |
|
302 | + public function compareTo($number) |
|
303 | + { |
|
304 | + $arg = self::fromNumber($number); |
|
305 | + if ($this->isNaN()) { |
|
306 | + if ($arg->isNaN()) { |
|
307 | + return 0; |
|
308 | + } |
|
309 | + else { |
|
310 | + return 1; |
|
311 | + } |
|
312 | + } |
|
313 | + elseif ($arg->isNaN()) { |
|
314 | + return -1; |
|
315 | + } |
|
316 | + else { |
|
317 | + return bccomp($this->val, $arg->val, max($this->scale, $arg->scale)); |
|
318 | + } |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * @return bool whether this is a not-a-number |
|
323 | + */ |
|
324 | + public function isNaN() |
|
325 | + { |
|
326 | + return ($this->val === null); |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * @return bool whether this is zero |
|
331 | + */ |
|
332 | + public function isZero() |
|
333 | + { |
|
334 | + return ($this->equals(self::zero())); |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @return bool whether this is a positive number |
|
339 | + */ |
|
340 | + public function isPositive() |
|
341 | + { |
|
342 | + return (!$this->isNaN() && $this->compareTo(self::zero()) > 0); |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * @return bool whether this is a negative number |
|
347 | + */ |
|
348 | + public function isNegative() |
|
349 | + { |
|
350 | + return (!$this->isNaN() && $this->val[0] == '-'); |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * @return bool whether this is an integer, i.e., a number without decimal part (or the decimal part equal to zero) |
|
355 | + */ |
|
356 | + public function isInteger() |
|
357 | + { |
|
358 | + if ($this->isNaN()) { |
|
359 | + return false; |
|
360 | + } |
|
361 | + if (strpos($this->val, '.') === false) { |
|
362 | + return true; |
|
363 | + } |
|
364 | + for ($i = strlen($this->val) - 1; ; $i--) { |
|
365 | + switch ($this->val[$i]) { |
|
366 | + case '.': |
|
367 | + return true; |
|
368 | + case '0': |
|
369 | + break; |
|
370 | + default: |
|
371 | + return false; |
|
372 | + } |
|
373 | + } |
|
374 | + return true; |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * @param string|int|float|Decimal|object $augend |
|
379 | + * @return Decimal a new decimal number, representing the result of sum of this and the given number |
|
380 | + */ |
|
381 | + public function add($augend) |
|
382 | + { |
|
383 | + if ($this->isNaN()) { |
|
384 | + return $this; |
|
385 | + } |
|
386 | + $arg = self::fromNumber($augend); |
|
387 | + if ($arg->isNaN()) { |
|
388 | + return $arg; |
|
389 | + } |
|
390 | + $scale = max($this->scale, $arg->scale); |
|
391 | + return new Decimal(bcadd($this->val, $arg->val, $scale + 1), $scale); |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * @param string|int|float|Decimal|object $subtrahend |
|
396 | + * @return Decimal a new decimal number, representing the result of subtraction of this and the given number |
|
397 | + */ |
|
398 | + public function subtract($subtrahend) |
|
399 | + { |
|
400 | + if ($this->isNaN()) { |
|
401 | + return $this; |
|
402 | + } |
|
403 | + $arg = self::fromNumber($subtrahend); |
|
404 | + if ($arg->isNaN()) { |
|
405 | + return $arg; |
|
406 | + } |
|
407 | + $scale = max($this->scale, $arg->scale); |
|
408 | + return new Decimal(bcsub($this->val, $arg->val, $scale + 1), $scale); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * @param string|int|float|Decimal|object $multiplicand |
|
413 | + * @return Decimal a new decimal number, representing the result of multiplication of this and the given number |
|
414 | + */ |
|
415 | + public function multiply($multiplicand) |
|
416 | + { |
|
417 | + if ($this->isNaN()) { |
|
418 | + return $this; |
|
419 | + } |
|
420 | + $arg = self::fromNumber($multiplicand); |
|
421 | + if ($arg->isNaN()) { |
|
422 | + return $arg; |
|
423 | + } |
|
424 | + $scale = min($this->scale + $arg->scale, self::SCALE_LIMIT); |
|
425 | + return new Decimal(bcmul($this->val, $arg->val, $scale + 1), $scale); |
|
426 | + } |
|
427 | + |
|
428 | + /** |
|
429 | + * @param string|int|float|Decimal|object $divisor |
|
430 | + * @return Decimal a new decimal number, representing the result of division of this number with the given number |
|
431 | + * @throws \RuntimeException if <tt>$divisor</tt> is zero |
|
432 | + */ |
|
433 | + public function divide($divisor) |
|
434 | + { |
|
435 | + if ($this->isNaN()) { |
|
436 | + return $this; |
|
437 | + } |
|
438 | + $arg = self::fromNumber($divisor); |
|
439 | + if ($arg->isNaN()) { |
|
440 | + return $arg; |
|
441 | + } |
|
442 | + $scale = max($this->scale, $arg->scale, self::MIN_SIG_DIGITS); |
|
443 | + return new Decimal(bcdiv($this->val, $arg->val, $scale + 1), $scale); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * @param string|int|float|Decimal|object $modulus |
|
448 | + * @return Decimal remainder of this number divided by <tt>$modulus</tt> |
|
449 | + * @throws \RuntimeException if <tt>$modulus</tt> is zero |
|
450 | + */ |
|
451 | + public function mod($modulus) |
|
452 | + { |
|
453 | + if ($this->isNaN()) { |
|
454 | + return $this; |
|
455 | + } |
|
456 | + $arg = self::fromNumber($modulus); |
|
457 | + if ($arg->isNaN()) { |
|
458 | + return $arg; |
|
459 | + } |
|
460 | + if ($arg->isZero()) { |
|
461 | + throw new \RuntimeException('Division by zero'); |
|
462 | + } |
|
463 | + |
|
464 | + // NOTE: bcmod() only calculates integer modulus |
|
465 | + $a = $this->abs(); |
|
466 | + $b = $arg->abs(); |
|
467 | + $m = $a->subtract($b->multiply($a->divide($b)->floor())); |
|
468 | + if ($this->isNegative()) { |
|
469 | + return $m->negate(); |
|
470 | + } |
|
471 | + else { |
|
472 | + return $m; |
|
473 | + } |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * @todo more precise calculation of fractional powers - now only double precision is used |
|
478 | + * @param string|int|float|Decimal|object $power |
|
479 | + * @return Decimal this number powered to <tt>$number</tt> |
|
480 | + * @throws \RuntimeException if this number is negative while <tt>$power</tt> is non-integer (that would lead to a |
|
481 | + * complex result) |
|
482 | + */ |
|
483 | + public function pow($power) |
|
484 | + { |
|
485 | + if ($this->isNaN()) { |
|
486 | + return $this; |
|
487 | + } |
|
488 | + $arg = self::fromNumber($power); |
|
489 | + if ($arg->isNaN()) { |
|
490 | + return $arg; |
|
491 | + } |
|
492 | + |
|
493 | + $scale = max($this->scale, $arg->scale, self::MIN_SIG_DIGITS); |
|
494 | + |
|
495 | + // NOTE: bcpow() only takes integer powers |
|
496 | + if ($arg->isInteger()) { |
|
497 | + return new Decimal(bcpow($this->val, $arg->val, $scale + 1), $scale); |
|
498 | + } |
|
499 | + else { |
|
500 | + if ($this->isNegative()) { |
|
501 | + throw new \RuntimeException('Negative number raised to a non-integer power yields a complex result'); |
|
502 | + } |
|
503 | + return self::fromNumber(pow($this->toFloat(), $arg->toFloat())); |
|
504 | + } |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * @return Decimal the absolute value of this number |
|
509 | + */ |
|
510 | + public function abs() |
|
511 | + { |
|
512 | + if ($this->isNegative()) { |
|
513 | + return $this->negate(); |
|
514 | + } |
|
515 | + else { |
|
516 | + return $this; |
|
517 | + } |
|
518 | + } |
|
519 | + |
|
520 | + /** |
|
521 | + * @return Decimal a new decimal number, representing the negative value of this number |
|
522 | + */ |
|
523 | + public function negate() |
|
524 | + { |
|
525 | + if ($this->isNaN() || $this->isZero()) { |
|
526 | + return $this; |
|
527 | + } |
|
528 | + elseif ($this->val[0] == '-') { |
|
529 | + return new Decimal(substr($this->val, 1)); |
|
530 | + } |
|
531 | + else { |
|
532 | + return new Decimal('-' . $this->val); |
|
533 | + } |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * Computes the exact factorial of this number. |
|
538 | + * Only defined on numbers of scale zero, i.e., those without decimal part, and on the not-a-number. |
|
539 | + * |
|
540 | + * In conformance with PostgreSQL, the factorial of non-positive numbers is defined to be 1. |
|
541 | + * |
|
542 | + * @return Decimal the factorial of this number |
|
543 | + * @throws UndefinedOperationException if this number is neither a zero-scale number nor the not-a-number |
|
544 | + */ |
|
545 | + public function factorial() |
|
546 | + { |
|
547 | + if ($this->isNaN()) { |
|
548 | + return $this; |
|
549 | + } |
|
550 | + if ($this->scale > 0) { |
|
551 | + throw new UndefinedOperationException('Number with a decimal part'); |
|
552 | + } |
|
553 | + if ($this->lessThan(2)) { |
|
554 | + return new Decimal(1); |
|
555 | + } |
|
556 | + |
|
557 | + if (System::hasGMP()) { |
|
558 | + return new Decimal(gmp_strval(gmp_fact($this->toGMP()))); |
|
559 | + } |
|
560 | + |
|
561 | + // OPT: there are more efficient algorithms calculating the factorial; see, e.g., http://www.luschny.de/math/factorial/index.html |
|
562 | + $result = new Decimal(2); |
|
563 | + for ($i = new Decimal(3); $i->compareTo($this) <= 0; $i = $i->add(1)) { |
|
564 | + $result = $result->multiply($i); |
|
565 | + } |
|
566 | + return $result; |
|
567 | + } |
|
568 | + |
|
569 | + /** |
|
570 | + * @param int $scale number of decimal places to round this number to; may be negative to round to higher orders |
|
571 | + * @return Decimal a new decimal number, representing this number rounded to <tt>$scale</tt> decimal places |
|
572 | + */ |
|
573 | + public function round($scale = 0) |
|
574 | + { |
|
575 | + return new Decimal($this->val, $scale); |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * @return Decimal largest integer not greater than this number |
|
580 | + */ |
|
581 | + public function floor() |
|
582 | + { |
|
583 | + $decPoint = strpos($this->val, '.'); |
|
584 | + if ($decPoint === false) { |
|
585 | + return $this; |
|
586 | + } |
|
587 | + if ($this->val[0] != '-') { |
|
588 | + return new Decimal(substr($this->val, 0, $decPoint)); |
|
589 | + } |
|
590 | + elseif ($this->isInteger()) { |
|
591 | + return new Decimal($this->val, 0); |
|
592 | + } |
|
593 | + else { |
|
594 | + // negative number with non-zero fractional part |
|
595 | + return new Decimal(bcsub(substr($this->val, 0, $decPoint), 1, 0)); |
|
596 | + } |
|
597 | + } |
|
598 | + |
|
599 | + /** |
|
600 | + * @return Decimal smallest integer not less than this number |
|
601 | + */ |
|
602 | + public function ceil() |
|
603 | + { |
|
604 | + $decPoint = strpos($this->val, '.'); |
|
605 | + if ($decPoint === false) { |
|
606 | + return $this; |
|
607 | + } |
|
608 | + if ($this->val[0] == '-') { |
|
609 | + return new Decimal(substr($this->val, 0, $decPoint)); |
|
610 | + } |
|
611 | + elseif ($this->isInteger()) { |
|
612 | + return new Decimal($this->val, 0); |
|
613 | + } |
|
614 | + else { |
|
615 | + // non-negative number with non-zero fractional part |
|
616 | + return new Decimal(bcadd(substr($this->val, 0, $decPoint), 1, 0)); |
|
617 | + } |
|
618 | + } |
|
619 | + |
|
620 | + /** |
|
621 | + * @param string|int|float|Decimal|object $number |
|
622 | + * @return Decimal the greater of this number and <tt>$number</tt>, preferably this number if numerically equal |
|
623 | + */ |
|
624 | + public function max($number) |
|
625 | + { |
|
626 | + $arg = self::fromNumber($number); |
|
627 | + if ($this->lessThan($arg)) { |
|
628 | + return $arg; |
|
629 | + } |
|
630 | + else { |
|
631 | + return $this; |
|
632 | + } |
|
633 | + } |
|
634 | + |
|
635 | + /** |
|
636 | + * @param string|int|float|Decimal|object $number |
|
637 | + * @return Decimal the smaller of this number and <tt>$number</tt>, preferably this number if numerically equal |
|
638 | + */ |
|
639 | + public function min($number) |
|
640 | + { |
|
641 | + $arg = self::fromNumber($number); |
|
642 | + if ($this->greaterThan($arg)) { |
|
643 | + return $arg; |
|
644 | + } |
|
645 | + else { |
|
646 | + return $this; |
|
647 | + } |
|
648 | + } |
|
649 | + |
|
650 | + /** |
|
651 | + * @return Decimal a new decimal number, representing the square root of this number |
|
652 | + * @throws UndefinedOperationException if this is a negative number |
|
653 | + */ |
|
654 | + public function sqrt() |
|
655 | + { |
|
656 | + if ($this->isNaN()) { |
|
657 | + return $this; |
|
658 | + } |
|
659 | + if ($this->isNegative()) { |
|
660 | + throw new UndefinedOperationException('square root of negative number'); |
|
661 | + } |
|
662 | + $scale = max($this->scale, self::MIN_SIG_DIGITS); |
|
663 | + return new Decimal(bcsqrt($this->val, $scale + 1)); |
|
664 | + } |
|
665 | + |
|
666 | + |
|
667 | + /** |
|
668 | + * @return int|null the value of this number cast explicitly to <tt>int</tt>; |
|
669 | + * for big numbers, this yields the maximal available integer value (i.e., <tt>PHP_INT_MAX</tt> or |
|
670 | + * <tt>PHP_INT_MIN</tt>); |
|
671 | 671 | * <tt>null</tt> is returned if this is the not-a-number |
672 | - */ |
|
673 | - public function toInt() |
|
674 | - { |
|
675 | - return ($this->val === null ? null : (int)$this->val); |
|
676 | - } |
|
677 | - |
|
678 | - /** |
|
679 | - * @return float the value of this number cast explicitly to <tt>float</tt>, or the float <tt>NAN</tt> value if this |
|
680 | - * is the not-a-number |
|
681 | - */ |
|
682 | - public function toFloat() |
|
683 | - { |
|
684 | - return ($this->val === null ? NAN : (float)$this->val); |
|
685 | - } |
|
686 | - |
|
687 | - /** |
|
688 | - * @return string the value of this number, or the special string <tt>'NaN'</tt> for the not-a-number value |
|
689 | - */ |
|
690 | - public function toString() |
|
691 | - { |
|
692 | - return ($this->val === null ? 'NaN' : $this->val); |
|
693 | - } |
|
694 | - |
|
695 | - /** |
|
696 | - * Converts the value to a {@link \GMP} object. |
|
697 | - * |
|
698 | - * Requires the `gmp` PHP extension. |
|
699 | - * |
|
700 | - * @return \GMP a {@link \GMP} object representing the same integer as this number |
|
701 | - * @throws UndefinedOperationException if this number is not an integer, i.e., has some non-zero fractional digits |
|
702 | - */ |
|
703 | - public function toGMP() |
|
704 | - { |
|
705 | - if (!$this->isInteger()) { |
|
706 | - throw new UndefinedOperationException('toGMP() is only defined for integers'); |
|
707 | - } |
|
708 | - $decPoint = strpos($this->val, '.'); |
|
709 | - $str = ($decPoint === false ? $this->val : substr($this->val, 0, $decPoint - 1)); |
|
710 | - return gmp_init($str); |
|
711 | - } |
|
712 | - |
|
713 | - public function __toString() |
|
714 | - { |
|
715 | - return $this->toString(); |
|
716 | - } |
|
672 | + */ |
|
673 | + public function toInt() |
|
674 | + { |
|
675 | + return ($this->val === null ? null : (int)$this->val); |
|
676 | + } |
|
677 | + |
|
678 | + /** |
|
679 | + * @return float the value of this number cast explicitly to <tt>float</tt>, or the float <tt>NAN</tt> value if this |
|
680 | + * is the not-a-number |
|
681 | + */ |
|
682 | + public function toFloat() |
|
683 | + { |
|
684 | + return ($this->val === null ? NAN : (float)$this->val); |
|
685 | + } |
|
686 | + |
|
687 | + /** |
|
688 | + * @return string the value of this number, or the special string <tt>'NaN'</tt> for the not-a-number value |
|
689 | + */ |
|
690 | + public function toString() |
|
691 | + { |
|
692 | + return ($this->val === null ? 'NaN' : $this->val); |
|
693 | + } |
|
694 | + |
|
695 | + /** |
|
696 | + * Converts the value to a {@link \GMP} object. |
|
697 | + * |
|
698 | + * Requires the `gmp` PHP extension. |
|
699 | + * |
|
700 | + * @return \GMP a {@link \GMP} object representing the same integer as this number |
|
701 | + * @throws UndefinedOperationException if this number is not an integer, i.e., has some non-zero fractional digits |
|
702 | + */ |
|
703 | + public function toGMP() |
|
704 | + { |
|
705 | + if (!$this->isInteger()) { |
|
706 | + throw new UndefinedOperationException('toGMP() is only defined for integers'); |
|
707 | + } |
|
708 | + $decPoint = strpos($this->val, '.'); |
|
709 | + $str = ($decPoint === false ? $this->val : substr($this->val, 0, $decPoint - 1)); |
|
710 | + return gmp_init($str); |
|
711 | + } |
|
712 | + |
|
713 | + public function __toString() |
|
714 | + { |
|
715 | + return $this->toString(); |
|
716 | + } |
|
717 | 717 | } |
@@ -72,16 +72,14 @@ discard block |
||
72 | 72 | if ($decimalNumber instanceof Decimal) { |
73 | 73 | if ($scale === null || $scale == $decimalNumber->scale) { |
74 | 74 | return $decimalNumber; |
75 | - } |
|
76 | - else { |
|
75 | + } else { |
|
77 | 76 | return $decimalNumber->round($scale); |
78 | 77 | } |
79 | 78 | } |
80 | 79 | |
81 | 80 | if (is_int($decimalNumber)) { |
82 | 81 | $val = (string)$decimalNumber; |
83 | - } |
|
84 | - elseif (is_float($decimalNumber)) { |
|
82 | + } elseif (is_float($decimalNumber)) { |
|
85 | 83 | $val = (string)$decimalNumber; |
86 | 84 | $ePos = stripos($val, 'e'); |
87 | 85 | if ($ePos > 0) { |
@@ -93,12 +91,10 @@ discard block |
||
93 | 91 | $dpLen = strlen($dp); |
94 | 92 | if ($exp >= $dpLen) { |
95 | 93 | $val = $wp . $dp . str_repeat('0', $exp - $dpLen); |
96 | - } |
|
97 | - else { |
|
94 | + } else { |
|
98 | 95 | $val = $wp . substr($dp, 0, $exp) . '.' . substr($dp, $exp); |
99 | 96 | } |
100 | - } |
|
101 | - else { |
|
97 | + } else { |
|
102 | 98 | $mn = ($wp[0] == '-' ? 1 : 0); |
103 | 99 | $ord = strlen($wp) - $mn; |
104 | 100 | if (-$exp >= $ord) { |
@@ -107,14 +103,12 @@ discard block |
||
107 | 103 | $prefix .= substr($wp, $mn); |
108 | 104 | } |
109 | 105 | $val = $prefix . $dp; |
110 | - } |
|
111 | - else { |
|
106 | + } else { |
|
112 | 107 | $val = substr($wp, 0, $mn + $ord + $exp) . '.' . substr($wp, $mn + $ord + $exp) . $dp; |
113 | 108 | } |
114 | 109 | } |
115 | 110 | } |
116 | - } |
|
117 | - else { |
|
111 | + } else { |
|
118 | 112 | if (!preg_match('~^\s*(-?)(?|(\.[0-9]+)|0*([0-9]+(?:\.[0-9]*)?))\s*$~', (string)$decimalNumber, $m)) { |
119 | 113 | throw new \InvalidArgumentException('decimalNumber'); |
120 | 114 | } |
@@ -178,8 +172,7 @@ discard block |
||
178 | 172 | $minus = ($this->val[0] == '-' ? 1 : 0); |
179 | 173 | if ($decPoint !== false) { |
180 | 174 | $digits = substr($this->val, $minus, $decPoint - $minus); |
181 | - } |
|
182 | - else { |
|
175 | + } else { |
|
183 | 176 | $digits = ($minus ? substr($this->val, $minus) : $this->val); |
184 | 177 | } |
185 | 178 | $len = strlen($digits); |
@@ -201,25 +194,21 @@ discard block |
||
201 | 194 | $digits = bcadd($digits, $augend, 0); |
202 | 195 | } |
203 | 196 | $this->val = ($minus ? '-' : '') . $digits; |
204 | - } |
|
205 | - elseif ($decPoint === false) { |
|
197 | + } elseif ($decPoint === false) { |
|
206 | 198 | if ($scale > 0) { |
207 | 199 | $this->val .= '.' . str_repeat('0', $scale); |
208 | 200 | $this->scale = $scale; |
209 | - } |
|
210 | - else { |
|
201 | + } else { |
|
211 | 202 | $this->scale = 0; |
212 | 203 | } |
213 | - } |
|
214 | - else { |
|
204 | + } else { |
|
215 | 205 | $len = strlen($this->val); |
216 | 206 | $this->scale = $len - $decPoint - 1; |
217 | 207 | $scale = min(self::SCALE_LIMIT, ($scale === null ? $this->scale : max(0, $scale))); |
218 | 208 | if ($this->scale < $scale) { |
219 | 209 | $this->val .= str_repeat('0', $scale - $this->scale); |
220 | 210 | $this->scale = $scale; |
221 | - } |
|
222 | - elseif ($this->scale > $scale) { |
|
211 | + } elseif ($this->scale > $scale) { |
|
223 | 212 | $newLen = $len - ($this->scale - $scale); |
224 | 213 | $inc = ($this->val[$newLen] >= 5); |
225 | 214 | if ($scale == 0) { |
@@ -236,8 +225,7 @@ discard block |
||
236 | 225 | |
237 | 226 | if ($this->val[0] == '.') { |
238 | 227 | $this->val = '0' . $this->val; |
239 | - } |
|
240 | - elseif ($this->val[0] == '-' && $this->val[1] == '.') { |
|
228 | + } elseif ($this->val[0] == '-' && $this->val[1] == '.') { |
|
241 | 229 | $this->val[0] = '0'; |
242 | 230 | $this->val = '-' . $this->val; |
243 | 231 | } |
@@ -305,15 +293,12 @@ discard block |
||
305 | 293 | if ($this->isNaN()) { |
306 | 294 | if ($arg->isNaN()) { |
307 | 295 | return 0; |
308 | - } |
|
309 | - else { |
|
296 | + } else { |
|
310 | 297 | return 1; |
311 | 298 | } |
312 | - } |
|
313 | - elseif ($arg->isNaN()) { |
|
299 | + } elseif ($arg->isNaN()) { |
|
314 | 300 | return -1; |
315 | - } |
|
316 | - else { |
|
301 | + } else { |
|
317 | 302 | return bccomp($this->val, $arg->val, max($this->scale, $arg->scale)); |
318 | 303 | } |
319 | 304 | } |
@@ -467,8 +452,7 @@ discard block |
||
467 | 452 | $m = $a->subtract($b->multiply($a->divide($b)->floor())); |
468 | 453 | if ($this->isNegative()) { |
469 | 454 | return $m->negate(); |
470 | - } |
|
471 | - else { |
|
455 | + } else { |
|
472 | 456 | return $m; |
473 | 457 | } |
474 | 458 | } |
@@ -495,8 +479,7 @@ discard block |
||
495 | 479 | // NOTE: bcpow() only takes integer powers |
496 | 480 | if ($arg->isInteger()) { |
497 | 481 | return new Decimal(bcpow($this->val, $arg->val, $scale + 1), $scale); |
498 | - } |
|
499 | - else { |
|
482 | + } else { |
|
500 | 483 | if ($this->isNegative()) { |
501 | 484 | throw new \RuntimeException('Negative number raised to a non-integer power yields a complex result'); |
502 | 485 | } |
@@ -511,8 +494,7 @@ discard block |
||
511 | 494 | { |
512 | 495 | if ($this->isNegative()) { |
513 | 496 | return $this->negate(); |
514 | - } |
|
515 | - else { |
|
497 | + } else { |
|
516 | 498 | return $this; |
517 | 499 | } |
518 | 500 | } |
@@ -524,11 +506,9 @@ discard block |
||
524 | 506 | { |
525 | 507 | if ($this->isNaN() || $this->isZero()) { |
526 | 508 | return $this; |
527 | - } |
|
528 | - elseif ($this->val[0] == '-') { |
|
509 | + } elseif ($this->val[0] == '-') { |
|
529 | 510 | return new Decimal(substr($this->val, 1)); |
530 | - } |
|
531 | - else { |
|
511 | + } else { |
|
532 | 512 | return new Decimal('-' . $this->val); |
533 | 513 | } |
534 | 514 | } |
@@ -586,11 +566,9 @@ discard block |
||
586 | 566 | } |
587 | 567 | if ($this->val[0] != '-') { |
588 | 568 | return new Decimal(substr($this->val, 0, $decPoint)); |
589 | - } |
|
590 | - elseif ($this->isInteger()) { |
|
569 | + } elseif ($this->isInteger()) { |
|
591 | 570 | return new Decimal($this->val, 0); |
592 | - } |
|
593 | - else { |
|
571 | + } else { |
|
594 | 572 | // negative number with non-zero fractional part |
595 | 573 | return new Decimal(bcsub(substr($this->val, 0, $decPoint), 1, 0)); |
596 | 574 | } |
@@ -607,11 +585,9 @@ discard block |
||
607 | 585 | } |
608 | 586 | if ($this->val[0] == '-') { |
609 | 587 | return new Decimal(substr($this->val, 0, $decPoint)); |
610 | - } |
|
611 | - elseif ($this->isInteger()) { |
|
588 | + } elseif ($this->isInteger()) { |
|
612 | 589 | return new Decimal($this->val, 0); |
613 | - } |
|
614 | - else { |
|
590 | + } else { |
|
615 | 591 | // non-negative number with non-zero fractional part |
616 | 592 | return new Decimal(bcadd(substr($this->val, 0, $decPoint), 1, 0)); |
617 | 593 | } |
@@ -626,8 +602,7 @@ discard block |
||
626 | 602 | $arg = self::fromNumber($number); |
627 | 603 | if ($this->lessThan($arg)) { |
628 | 604 | return $arg; |
629 | - } |
|
630 | - else { |
|
605 | + } else { |
|
631 | 606 | return $this; |
632 | 607 | } |
633 | 608 | } |
@@ -641,8 +616,7 @@ discard block |
||
641 | 616 | $arg = self::fromNumber($number); |
642 | 617 | if ($this->greaterThan($arg)) { |
643 | 618 | return $arg; |
644 | - } |
|
645 | - else { |
|
619 | + } else { |
|
646 | 620 | return $this; |
647 | 621 | } |
648 | 622 | } |