Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
19 / 19 |
|
85.71% |
12 / 14 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
Magnitude | |
100.00% |
16 / 16 |
|
100.00% |
19 / 19 |
|
85.71% |
12 / 14 |
|
100.00% |
9 / 9 |
14.57 | |
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% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
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 | |||
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 | * @return true, if every check is passed, false otherwise |
11 | */ |
12 | |
13 | class Magnitude { |
14 | private string $type; |
15 | private float $value; |
16 | |
17 | private $validTypes = array('ml', 'md', 'mb', 'ms', 'mw', 'mwp'); |
18 | private $validValues = array('min' => -12, 'max' => 12); |
19 | |
20 | function __construct( string $type, float $value) { |
21 | |
22 | if( empty($type) ) |
23 | throw new \InvalidArgumentException("Magnitude type unset or empty string"); |
24 | |
25 | if( ! $this->isValidType($type) ) |
26 | throw new \InvalidArgumentException("Magnitude type invalid"); |
27 | |
28 | if( ! $this->isValidValue($value) ) |
29 | throw new \InvalidArgumentException("Magnitude value invalid"); |
30 | |
31 | $this->type = $type; |
32 | $this->value = $value; |
33 | } |
34 | |
35 | function __destruct(){ } |
36 | |
37 | /** |
38 | * Returns magnitude |
39 | * |
40 | * @return string Magnitude type and value in "%s %.2f" format |
41 | */ |
42 | function __toString(){ return sprintf("%s %.2f", $this->type, $this->value); } |
43 | |
44 | /** |
45 | * Get magnitude value |
46 | * |
47 | * @return float Magnitude value |
48 | */ |
49 | public function value():float{ return $this->value; } |
50 | |
51 | /** |
52 | * Get magnitude type |
53 | * |
54 | * @return string Magnitude type |
55 | */ |
56 | public function type():string{ return $this->type; } |
57 | |
58 | /** |
59 | * Get magnitude type in lower case |
60 | * |
61 | * @return string Magnitude type in lowercase |
62 | */ |
63 | public function typeLowerCase():string{ return strtolower($this->type); } |
64 | |
65 | /** |
66 | * Check is type is supported |
67 | * |
68 | * @return bool True is supported, false otherwise |
69 | */ |
70 | private function isValidType(string $type){ return in_array(strtolower($type), $this->validTypes); } |
71 | |
72 | /** |
73 | * Check is value is valid |
74 | * |
75 | * @return bool True is valid, false otherwise |
76 | */ |
77 | private function isValidValue(float $value){ return ($this->validValues['min'] <= $value && $value <= $this->validValues['max']); } |
78 | |
79 | /** |
80 | * Compare two objects |
81 | * @return bool true are equals, false otherwise |
82 | */ |
83 | public function equals(self $other):bool{ return $this->value == $other->value && $this->type == $other->type; } |
84 | } |