Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
23 / 23 |
|
87.50% |
14 / 16 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| Magnitude | |
100.00% |
17 / 17 |
|
100.00% |
23 / 23 |
|
87.50% |
14 / 16 |
|
100.00% |
10 / 10 |
16.50 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| __destruct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| value | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| type | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| typeLowerCase | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| typeUppercaseFirst | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isValidType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isValidValue | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| equals | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| 1 | <?php |
| 2 | namespace Fdsn\Webservices\Event\Structs; |
| 3 | |
| 4 | /** |
| 5 | * Data structure to handle magnitude type and value |
| 6 | * |
| 7 | * @param string $magnitudeType Magnitude type |
| 8 | * @param float $magnitudeValue Magniude value |
| 9 | */ |
| 10 | class Magnitude { |
| 11 | private string $type; |
| 12 | private float $value; |
| 13 | |
| 14 | private $validTypes = array('ml', 'md', 'mb', 'ms', 'mw', 'mwp', 'mwpd'); |
| 15 | private $validValues = array('min' => -12, 'max' => 12); |
| 16 | |
| 17 | function __construct( string $type, float $value) { |
| 18 | |
| 19 | if( empty($type) ) |
| 20 | throw new \InvalidArgumentException("Magnitude type unset or empty string"); |
| 21 | |
| 22 | if( ! $this->isValidType($type) ) |
| 23 | throw new \InvalidArgumentException( sprintf("Magnitude '%s' type invalid", $type) ); |
| 24 | |
| 25 | if( ! $this->isValidValue($value) ) |
| 26 | throw new \InvalidArgumentException("Magnitude value invalid"); |
| 27 | |
| 28 | $this->type = $type; |
| 29 | $this->value = $value; |
| 30 | } |
| 31 | |
| 32 | function __destruct(){ } |
| 33 | |
| 34 | /** |
| 35 | * Returns magnitude |
| 36 | * |
| 37 | * @return string Magnitude type and value in "%s %.1f" format |
| 38 | */ |
| 39 | function __toString(){ return sprintf("%s %.1f", $this->type, $this->value); } |
| 40 | |
| 41 | /** |
| 42 | * Get magnitude value |
| 43 | * |
| 44 | * @param int $roundTo (nullable) if set, return value rounded to `roundTo` decimals |
| 45 | * @return float Magnitude value |
| 46 | */ |
| 47 | public function value(?int $roundTo = null):float{ return is_null($roundTo) ? $this->value : round($this->value, $roundTo); } |
| 48 | |
| 49 | /** |
| 50 | * Get magnitude type |
| 51 | * |
| 52 | * @return string Magnitude type |
| 53 | */ |
| 54 | public function type():string{ return $this->type; } |
| 55 | |
| 56 | /** |
| 57 | * Get magnitude type in lower case |
| 58 | * |
| 59 | * @return string Magnitude type in lowercase |
| 60 | */ |
| 61 | public function typeLowerCase():string{ return strtolower($this->type() ); } |
| 62 | |
| 63 | /** |
| 64 | * Get magnitude type in upper case first letter |
| 65 | * |
| 66 | * @return string Magnitude type upper case first letter |
| 67 | */ |
| 68 | public function typeUppercaseFirst():string{ return ucfirst( $this->typeLowerCase() ) ; } |
| 69 | |
| 70 | /** |
| 71 | * Check is type is supported |
| 72 | * |
| 73 | * @return bool True is supported, false otherwise |
| 74 | */ |
| 75 | private function isValidType(string $type){ return in_array(strtolower($type), $this->validTypes); } |
| 76 | |
| 77 | /** |
| 78 | * Check is value is valid |
| 79 | * |
| 80 | * @return bool True is valid, false otherwise |
| 81 | */ |
| 82 | private function isValidValue(float $value){ return ($this->validValues['min'] <= $value && $value <= $this->validValues['max']); } |
| 83 | |
| 84 | /** |
| 85 | * Compare two objects |
| 86 | * @return bool true are equals, false otherwise |
| 87 | */ |
| 88 | public function equals(self $other):bool{ return $this->value == $other->value && $this->type == $other->type; } |
| 89 | } |