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