Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
22 / 22
86.67% covered (warning)
86.67%
13 / 15
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Magnitude
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
22 / 22
86.67% covered (warning)
86.67%
13 / 15
100.00% covered (success)
100.00%
9 / 9
15.53
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 __destruct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 value
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 typeLowerCase
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isValidType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isValidValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
2.50
 equals
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
2.50
1<?php
2namespace 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
13class Magnitude {
14    private string $type;
15    private float $value;
16
17    private $validTypes = array('ml', 'md', 'mb', 'ms', 'mw', 'mwp', 'mwpd');
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( sprintf("Magnitude '%s' type invalid", $type) );
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 %.1f" format
41     */
42    function __toString(){ return sprintf("%s %.1f", $this->type, $this->value); }
43
44    /**
45     * Get magnitude value
46     *
47     * @param int $roundTo (nullable) if set, return value rounded to `roundTo` decimals
48     * @return float Magnitude value
49     */
50    public function value(?int $roundTo = null):float{ return is_null($roundTo) ? $this->value : round($this->value, $roundTo); }
51
52    /**
53     * Get magnitude type
54     *
55     * @return string     Magnitude type
56     */
57    public function type():string{ return $this->type; }
58
59    /**
60     * Get magnitude type in lower case
61     *
62     * @return string     Magnitude type in lowercase
63     */
64    public function typeLowerCase():string{ return strtolower($this->type); }
65
66    /**
67     * Check is type is supported
68     *
69     * @return bool  True is supported, false otherwise
70     */
71    private function isValidType(string $type){ return in_array(strtolower($type), $this->validTypes); } 
72
73    /**
74     * Check is value is valid 
75     *
76     * @return bool  True is valid, false otherwise
77     */
78    private function isValidValue(float $value){ return ($this->validValues['min'] <= $value && $value <= $this->validValues['max']); }
79
80    /**
81     * Compare two objects
82     * @return bool        true are equals, false otherwise
83     */
84    public function equals(self $other):bool{ return $this->value == $other->value && $this->type == $other->type; }
85}