@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | UnitConverter $unitConverter = null |
47 | 47 | ) { |
48 | 48 | $this->config = $config; |
49 | - $this->timeParser = ( new TimeParserFactory() )->getTimeParser(); |
|
49 | + $this->timeParser = (new TimeParserFactory())->getTimeParser(); |
|
50 | 50 | $this->timeCalculator = new TimeValueCalculator(); |
51 | 51 | $this->unitConverter = $unitConverter; |
52 | 52 | } |
@@ -55,10 +55,10 @@ discard block |
||
55 | 55 | * @param UnboundedQuantityValue $value |
56 | 56 | * @return UnboundedQuantityValue $value converted to standard units if possible, otherwise unchanged $value. |
57 | 57 | */ |
58 | - private function standardize( UnboundedQuantityValue $value ) { |
|
59 | - if ( $this->unitConverter !== null ) { |
|
60 | - $standard = $this->unitConverter->toStandardUnits( $value ); |
|
61 | - if ( $standard !== null ) { |
|
58 | + private function standardize(UnboundedQuantityValue $value) { |
|
59 | + if ($this->unitConverter !== null) { |
|
60 | + $standard = $this->unitConverter->toStandardUnits($value); |
|
61 | + if ($standard !== null) { |
|
62 | 62 | return $standard; |
63 | 63 | } else { |
64 | 64 | return $value; |
@@ -80,23 +80,23 @@ discard block |
||
80 | 80 | * when $lhs is respectively less than, equal to, or greater than $rhs. |
81 | 81 | * (In other words, just like the “spaceship” operator <=>.) |
82 | 82 | */ |
83 | - public function getComparison( DataValue $lhs = null, DataValue $rhs = null ) { |
|
84 | - if ( $lhs === null || $rhs === null ) { |
|
83 | + public function getComparison(DataValue $lhs = null, DataValue $rhs = null) { |
|
84 | + if ($lhs === null || $rhs === null) { |
|
85 | 85 | return 0; |
86 | 86 | } |
87 | 87 | |
88 | - if ( $lhs->getType() !== $rhs->getType() ) { |
|
89 | - throw new InvalidArgumentException( 'Different data value types' ); |
|
88 | + if ($lhs->getType() !== $rhs->getType()) { |
|
89 | + throw new InvalidArgumentException('Different data value types'); |
|
90 | 90 | } |
91 | 91 | |
92 | - switch ( $lhs->getType() ) { |
|
92 | + switch ($lhs->getType()) { |
|
93 | 93 | case 'time': |
94 | 94 | /** @var TimeValue $lhs */ |
95 | 95 | /** @var TimeValue $rhs */ |
96 | - $lhsTimestamp = $this->timeCalculator->getTimestamp( $lhs ); |
|
97 | - $rhsTimestamp = $this->timeCalculator->getTimestamp( $rhs ); |
|
96 | + $lhsTimestamp = $this->timeCalculator->getTimestamp($lhs); |
|
97 | + $rhsTimestamp = $this->timeCalculator->getTimestamp($rhs); |
|
98 | 98 | |
99 | - if ( $lhsTimestamp === $rhsTimestamp ) { |
|
99 | + if ($lhsTimestamp === $rhsTimestamp) { |
|
100 | 100 | return 0; |
101 | 101 | } |
102 | 102 | |
@@ -104,12 +104,12 @@ discard block |
||
104 | 104 | case 'quantity': |
105 | 105 | /** @var QuantityValue|UnboundedQuantityValue $lhs */ |
106 | 106 | /** @var QuantityValue|UnboundedQuantityValue $rhs */ |
107 | - $lhsStandard = $this->standardize( $lhs ); |
|
108 | - $rhsStandard = $this->standardize( $rhs ); |
|
109 | - return $lhsStandard->getAmount()->compare( $rhsStandard->getAmount() ); |
|
107 | + $lhsStandard = $this->standardize($lhs); |
|
108 | + $rhsStandard = $this->standardize($rhs); |
|
109 | + return $lhsStandard->getAmount()->compare($rhsStandard->getAmount()); |
|
110 | 110 | } |
111 | 111 | |
112 | - throw new InvalidArgumentException( 'Unsupported data value type' ); |
|
112 | + throw new InvalidArgumentException('Unsupported data value type'); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
@@ -124,67 +124,67 @@ discard block |
||
124 | 124 | * @throws InvalidArgumentException if the values do not both have the same, supported data value type |
125 | 125 | * @return UnboundedQuantityValue |
126 | 126 | */ |
127 | - public function getDifference( DataValue $minuend, DataValue $subtrahend ) { |
|
128 | - if ( $minuend->getType() === 'time' && $subtrahend->getType() === 'time' ) { |
|
129 | - $minuendSeconds = $this->timeCalculator->getTimestamp( $minuend ); |
|
130 | - $subtrahendSeconds = $this->timeCalculator->getTimestamp( $subtrahend ); |
|
127 | + public function getDifference(DataValue $minuend, DataValue $subtrahend) { |
|
128 | + if ($minuend->getType() === 'time' && $subtrahend->getType() === 'time') { |
|
129 | + $minuendSeconds = $this->timeCalculator->getTimestamp($minuend); |
|
130 | + $subtrahendSeconds = $this->timeCalculator->getTimestamp($subtrahend); |
|
131 | 131 | return UnboundedQuantityValue::newFromNumber( |
132 | 132 | $minuendSeconds - $subtrahendSeconds, |
133 | - $this->config->get( 'WBQualityConstraintsSecondUnit' ) |
|
133 | + $this->config->get('WBQualityConstraintsSecondUnit') |
|
134 | 134 | ); |
135 | 135 | } |
136 | - if ( $minuend->getType() === 'quantity' && $subtrahend->getType() === 'quantity' ) { |
|
137 | - $minuendStandard = $this->standardize( $minuend ); |
|
138 | - $subtrahendStandard = $this->standardize( $subtrahend ); |
|
136 | + if ($minuend->getType() === 'quantity' && $subtrahend->getType() === 'quantity') { |
|
137 | + $minuendStandard = $this->standardize($minuend); |
|
138 | + $subtrahendStandard = $this->standardize($subtrahend); |
|
139 | 139 | $minuendValue = $minuendStandard->getAmount()->getValueFloat(); |
140 | 140 | $subtrahendValue = $subtrahendStandard->getAmount()->getValueFloat(); |
141 | 141 | $diff = $minuendValue - $subtrahendValue; |
142 | 142 | // we don’t check whether both quantities have the same standard unit – |
143 | 143 | // that’s the job of a different constraint type, Units (T164372) |
144 | - return UnboundedQuantityValue::newFromNumber( $diff, $minuendStandard->getUnit() ); |
|
144 | + return UnboundedQuantityValue::newFromNumber($diff, $minuendStandard->getUnit()); |
|
145 | 145 | } |
146 | 146 | |
147 | - throw new InvalidArgumentException( 'Unsupported or different data value types' ); |
|
147 | + throw new InvalidArgumentException('Unsupported or different data value types'); |
|
148 | 148 | } |
149 | 149 | |
150 | - public function getDifferenceInYears( TimeValue $minuend, TimeValue $subtrahend ) { |
|
151 | - if ( !preg_match( '/^([-+]\d{1,16})-(.*)$/', $minuend->getTime(), $minuendMatches ) || |
|
152 | - !preg_match( '/^([-+]\d{1,16})-(.*)$/', $subtrahend->getTime(), $subtrahendMatches ) |
|
150 | + public function getDifferenceInYears(TimeValue $minuend, TimeValue $subtrahend) { |
|
151 | + if (!preg_match('/^([-+]\d{1,16})-(.*)$/', $minuend->getTime(), $minuendMatches) || |
|
152 | + !preg_match('/^([-+]\d{1,16})-(.*)$/', $subtrahend->getTime(), $subtrahendMatches) |
|
153 | 153 | ) { |
154 | - throw new InvalidArgumentException( 'TimeValue::getTime() did not match expected format' ); |
|
154 | + throw new InvalidArgumentException('TimeValue::getTime() did not match expected format'); |
|
155 | 155 | } |
156 | - $minuendYear = (float)$minuendMatches[1]; |
|
157 | - $subtrahendYear = (float)$subtrahendMatches[1]; |
|
156 | + $minuendYear = (float) $minuendMatches[1]; |
|
157 | + $subtrahendYear = (float) $subtrahendMatches[1]; |
|
158 | 158 | $minuendRest = $minuendMatches[2]; |
159 | 159 | $subtrahendRest = $subtrahendMatches[2]; |
160 | 160 | |
161 | 161 | // calculate difference of years |
162 | 162 | $diff = $minuendYear - $subtrahendYear; |
163 | - if ( $minuendYear > 0.0 && $subtrahendYear < 0.0 ) { |
|
163 | + if ($minuendYear > 0.0 && $subtrahendYear < 0.0) { |
|
164 | 164 | $diff -= 1.0; // there is no year 0, remove it from difference |
165 | - } elseif ( $minuendYear < 0.0 && $subtrahendYear > 0.0 ) { |
|
165 | + } elseif ($minuendYear < 0.0 && $subtrahendYear > 0.0) { |
|
166 | 166 | $diff -= -1.0; // there is no year 0, remove it from negative difference |
167 | 167 | } |
168 | 168 | |
169 | 169 | // adjust for date within year by parsing the month-day part within the same year |
170 | - $minuendDateValue = $this->timeParser->parse( '+0000000000001970-' . $minuendRest ); |
|
171 | - $subtrahendDateValue = $this->timeParser->parse( '+0000000000001970-' . $subtrahendRest ); |
|
172 | - $minuendDateSeconds = $this->timeCalculator->getTimestamp( $minuendDateValue ); |
|
173 | - $subtrahendDateSeconds = $this->timeCalculator->getTimestamp( $subtrahendDateValue ); |
|
174 | - if ( $minuendDateSeconds < $subtrahendDateSeconds ) { |
|
170 | + $minuendDateValue = $this->timeParser->parse('+0000000000001970-'.$minuendRest); |
|
171 | + $subtrahendDateValue = $this->timeParser->parse('+0000000000001970-'.$subtrahendRest); |
|
172 | + $minuendDateSeconds = $this->timeCalculator->getTimestamp($minuendDateValue); |
|
173 | + $subtrahendDateSeconds = $this->timeCalculator->getTimestamp($subtrahendDateValue); |
|
174 | + if ($minuendDateSeconds < $subtrahendDateSeconds) { |
|
175 | 175 | // difference in the last year is actually less than one full year |
176 | 176 | // e. g. 1975-03-01 - 1974-09-01 is just six months |
177 | 177 | // (we don’t need sub-year precision in the difference, adjusting by 0.5 is enough) |
178 | 178 | $diff -= 0.5; |
179 | - } elseif ( $minuendDateSeconds > $subtrahendDateSeconds ) { |
|
179 | + } elseif ($minuendDateSeconds > $subtrahendDateSeconds) { |
|
180 | 180 | // difference in the last year is actually more than one full year |
181 | 181 | // e. g. 1975-09-01 - 1974-03-01 is 18 months |
182 | 182 | // (we don’t need sub-year precision in the difference, adjusting by 0.5 is enough) |
183 | 183 | $diff += 0.5; |
184 | 184 | } |
185 | 185 | |
186 | - $unit = $this->config->get( 'WBQualityConstraintsYearUnit' ); |
|
187 | - return UnboundedQuantityValue::newFromNumber( $diff, $unit ); |
|
186 | + $unit = $this->config->get('WBQualityConstraintsYearUnit'); |
|
187 | + return UnboundedQuantityValue::newFromNumber($diff, $unit); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | } |