eymengunay /
php-passbook
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Passbook package. |
||
| 5 | * |
||
| 6 | * (c) Eymen Gunay <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Passbook\Pass; |
||
| 13 | |||
| 14 | use InvalidArgumentException; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Class NumberField |
||
| 18 | * |
||
| 19 | * @package Passbook\Pass |
||
| 20 | * @author Florian Morello <[email protected]> |
||
| 21 | * @phpcs:disable Generic.NamingConventions.UpperCaseConstantName |
||
| 22 | */ |
||
| 23 | class NumberField extends Field |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @deprecated please use ::NUMBER_STYLE_DECIMAL instead. |
||
| 27 | */ |
||
| 28 | public const PKNumberStyleDecimal = 'PKNumberStyleDecimal'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @deprecated please use ::NUMBER_STYLE_PERCENT instead. |
||
| 32 | */ |
||
| 33 | public const PKNumberStylePercent = 'PKNumberStylePercent'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @deprecated please use ::NUMBER_STYLE_SCIENTIFIC instead. |
||
| 37 | */ |
||
| 38 | public const PKNumberStyleScientific = 'PKNumberStyleScientific'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | public const NUMBER_STYLE_DECIMAL = 'PKNumberStyleDecimal'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public const NUMBER_STYLE_PERCENT = 'PKNumberStylePercent'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | public const NUMBER_STYLE_SCIENTIFIC = 'PKNumberStyleScientific'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * ISO 4217 |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $currencyCode = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $numberStyle = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * NumberField constructor. |
||
| 69 | * @param $key |
||
| 70 | * @param $value |
||
| 71 | * @throws InvalidArgumentException |
||
| 72 | */ |
||
| 73 | 3 | public function __construct($key, $value) |
|
| 74 | { |
||
| 75 | 3 | if (!is_numeric($value)) { |
|
| 76 | throw new InvalidArgumentException("Value has to be numeric. '$value' given!"); |
||
| 77 | } |
||
| 78 | |||
| 79 | 3 | parent::__construct($key, $value); |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | 2 | public function toArray() |
|
| 86 | { |
||
| 87 | 2 | $array = parent::toArray(); |
|
| 88 | 2 | if ($this->getCurrencyCode() !== null) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 89 | 2 | $array['currencyCode'] = $this->getCurrencyCode(); |
|
| 90 | } |
||
| 91 | 2 | if ($this->getNumberStyle() !== null) { |
|
|
0 ignored issues
–
show
|
|||
| 92 | 1 | $array['numberStyle'] = $this->getNumberStyle(); |
|
| 93 | } |
||
| 94 | |||
| 95 | 2 | return $array; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $currencyCode an ISO 4217 currency code |
||
| 100 | * |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | 2 | public function setCurrencyCode($currencyCode) |
|
| 104 | { |
||
| 105 | 2 | $this->currencyCode = $currencyCode; |
|
| 106 | |||
| 107 | 2 | return $this; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | 2 | public function getCurrencyCode() |
|
| 114 | { |
||
| 115 | 2 | return $this->currencyCode; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $numberStyle |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | 1 | public function setNumberStyle($numberStyle) |
|
| 124 | { |
||
| 125 | 1 | $this->numberStyle = $numberStyle; |
|
| 126 | |||
| 127 | 1 | return $this; |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | 2 | public function getNumberStyle() |
|
| 134 | { |
||
| 135 | 2 | return $this->numberStyle; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | * |
||
| 141 | * @return int|float |
||
| 142 | */ |
||
| 143 | 3 | public function getValue() |
|
| 144 | { |
||
| 145 | // Ensure value is int or float; adding 0 will convert type from string |
||
| 146 | 3 | return 0 + parent::getValue(); |
|
| 147 | } |
||
| 148 | } |
||
| 149 |