| Total Complexity | 34 |
| Total Lines | 179 |
| Duplicated Lines | 0 % |
| Coverage | 97.65% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 27 | class Number implements Rendering |
||
| 28 | { |
||
| 29 | |||
| 30 | const RANGE_DELIMITER_HYPHEN = "-"; |
||
| 31 | |||
| 32 | const RANGE_DELIMITER_AMPERSAND = "&"; |
||
| 33 | |||
| 34 | const RANGE_DELIMITER_COMMA = ","; |
||
| 35 | |||
| 36 | use FormattingTrait, |
||
| 37 | AffixesTrait, |
||
| 38 | TextCaseTrait, |
||
| 39 | DisplayTrait; |
||
| 40 | |||
| 41 | /** |
||
|
1 ignored issue
–
show
|
|||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $variable; |
||
| 45 | |||
| 46 | /** |
||
|
1 ignored issue
–
show
|
|||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $form; |
||
| 50 | |||
| 51 | 47 | public function __construct(SimpleXMLElement $node) |
|
| 68 | 47 | } |
|
| 69 | |||
| 70 | /** |
||
|
1 ignored issue
–
show
|
|||
| 71 | * @param stdClass $data |
||
|
2 ignored issues
–
show
|
|||
| 72 | * @param int|null $citationNumber |
||
|
2 ignored issues
–
show
|
|||
| 73 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 74 | */ |
||
| 75 | 26 | public function render($data, $citationNumber = null) |
|
| 76 | { |
||
| 77 | 26 | $lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en'; |
|
| 78 | |||
| 79 | 26 | if (empty($this->variable) || empty($data->{$this->variable})) { |
|
| 80 | 8 | return ""; |
|
| 81 | } |
||
| 82 | 20 | $number = $data->{$this->variable}; |
|
| 83 | 20 | $decimalNumber = $this->toDecimalNumber($number); |
|
| 84 | 20 | switch ($this->form) { |
|
| 85 | 20 | case 'ordinal': |
|
| 86 | 4 | if (preg_match("/\s*(\d+)\s*([\-\-&,])\s*(\d+)\s*/", $decimalNumber, $matches)) { |
|
| 87 | 2 | $num1 = self::ordinal($matches[1]); |
|
| 88 | 2 | $num2 = self::ordinal($matches[3]); |
|
| 89 | 2 | $text = $this->buildNumberRangeString($num1, $num2, $matches[2]); |
|
| 90 | } else { |
||
| 91 | 2 | $text = self::ordinal($decimalNumber); |
|
| 92 | } |
||
| 93 | 4 | break; |
|
| 94 | 17 | case 'long-ordinal': |
|
| 95 | 3 | if (preg_match("/\s*(\d+)\s*([\-\-&,])\s*(\d+)\s*/", $decimalNumber, $matches)) { |
|
| 96 | 2 | if ($this->textCase === "capitalize-first" || $this->textCase === "sentence") { |
|
| 97 | 1 | $num1 = self::longOrdinal($matches[1]); |
|
| 98 | 1 | $num2 = self::longOrdinal($matches[3]); |
|
| 99 | } else { |
||
| 100 | 2 | $num1 = $this->applyTextCase(self::longOrdinal($matches[1])); |
|
| 101 | 2 | $num2 = $this->applyTextCase(self::longOrdinal($matches[3])); |
|
| 102 | } |
||
| 103 | 2 | $text = $this->buildNumberRangeString($num1, $num2, $matches[2]); |
|
| 104 | } else { |
||
| 105 | 1 | $text = self::longOrdinal($decimalNumber); |
|
| 106 | } |
||
| 107 | 3 | break; |
|
| 108 | 15 | case 'roman': |
|
| 109 | 4 | if (preg_match("/\s*(\d+)\s*([\-\-&,])\s*(\d+)\s*/", $decimalNumber, $matches)) { |
|
| 110 | 1 | $num1 = Util\NumberHelper::dec2roman($matches[1]); |
|
| 111 | 1 | $num2 = Util\NumberHelper::dec2roman($matches[3]); |
|
| 112 | 1 | $text = $this->buildNumberRangeString($num1, $num2, $matches[2]); |
|
| 113 | } else { |
||
| 114 | 3 | $text = Util\NumberHelper::dec2roman($decimalNumber); |
|
| 115 | } |
||
| 116 | 4 | break; |
|
| 117 | 12 | case 'numeric': |
|
| 118 | default: |
||
| 119 | /* |
||
| 120 | During the extraction, numbers separated by a hyphen are stripped of intervening spaces (“2 - 4” |
||
| 121 | becomes “2-4”). Numbers separated by a comma receive one space after the comma (“2,3” and “2 , 3” |
||
| 122 | become “2, 3”), while numbers separated by an ampersand receive one space before and one after the |
||
| 123 | ampersand (“2&3” becomes “2 & 3”). |
||
| 124 | */ |
||
| 125 | 12 | $decimalNumber = $data->{$this->variable}; |
|
| 126 | 12 | if (preg_match("/\s*(\d+)\s*([\-\-&,])\s*(\d+)\s*/", $decimalNumber, $matches)) { |
|
| 127 | 9 | $text = $this->buildNumberRangeString($matches[1], $matches[3], $matches[2]); |
|
| 128 | } else { |
||
| 129 | 3 | $text = $decimalNumber; |
|
| 130 | } |
||
| 131 | 12 | break; |
|
| 132 | } |
||
| 133 | 20 | return $this->wrapDisplayBlock($this->addAffixes($this->format($this->applyTextCase($text, $lang)))); |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
|
1 ignored issue
–
show
|
|||
| 137 | * @param $num |
||
|
2 ignored issues
–
show
|
|||
| 138 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 139 | */ |
||
| 140 | 4 | public static function ordinal($num) { |
|
| 141 | 4 | if (($num / 10) % 10 == 1) { |
|
| 142 | 1 | $ordinalSuffix = CiteProc::getContext()->getLocale()->filter('terms', 'ordinal')->single; |
|
| 143 | 4 | } elseif ($num % 10 == 1) { |
|
| 144 | $ordinalSuffix = CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-01')->single; |
||
| 145 | 4 | } elseif ($num % 10 == 2) { |
|
| 146 | 2 | $ordinalSuffix = CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-02')->single; |
|
| 147 | 4 | } elseif ($num % 10 == 3) { |
|
| 148 | 1 | $ordinalSuffix = CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-03')->single; |
|
| 149 | } else { |
||
| 150 | 3 | $ordinalSuffix = CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-04')->single; |
|
| 151 | } |
||
| 152 | 4 | if (empty($ordinalSuffix)) { |
|
| 153 | 3 | $ordinalSuffix = CiteProc::getContext()->getLocale()->filter('terms', 'ordinal')->single; |
|
| 154 | } |
||
| 155 | 4 | return $num . $ordinalSuffix; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
|
1 ignored issue
–
show
|
|||
| 159 | * @param $num |
||
|
2 ignored issues
–
show
|
|||
| 160 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 161 | */ |
||
| 162 | 3 | public static function longOrdinal($num) { |
|
| 163 | 3 | $num = sprintf("%02d", $num); |
|
| 164 | 3 | $ret = CiteProc::getContext()->getLocale()->filter('terms', 'long-ordinal-' . $num)->single; |
|
| 165 | 3 | if (!$ret) { |
|
| 166 | return self::ordinal($num); |
||
| 167 | } |
||
| 168 | 3 | return $ret; |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
|
1 ignored issue
–
show
|
|||
| 172 | * @param string|int $num1 |
||
|
2 ignored issues
–
show
|
|||
| 173 | * @param string|int $num2 |
||
|
2 ignored issues
–
show
|
|||
| 174 | * @param string $delim |
||
|
3 ignored issues
–
show
|
|||
| 175 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 176 | */ |
||
| 177 | 11 | public function buildNumberRangeString($num1, $num2, $delim) { |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
|
1 ignored issue
–
show
|
|||
| 190 | * @param string $number |
||
|
2 ignored issues
–
show
|
|||
| 191 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 192 | */ |
||
| 193 | 20 | private function toDecimalNumber($number) |
|
| 206 | } |
||
| 207 | } |
||
| 208 |