1 | <?php namespace Arcanedev\Units\Traits; |
||
11 | trait Calculatable |
||
12 | { |
||
13 | /* ------------------------------------------------------------------------------------------------ |
||
14 | | Main Functions |
||
15 | | ------------------------------------------------------------------------------------------------ |
||
16 | */ |
||
17 | /** |
||
18 | * Add the unit instance. |
||
19 | * |
||
20 | * @param \Arcanedev\Units\Contracts\UnitMeasure $unit |
||
21 | * |
||
22 | * @return \Arcanedev\Units\Contracts\UnitMeasure |
||
23 | */ |
||
24 | 32 | public function add(UnitMeasure $unit) |
|
25 | { |
||
26 | 32 | return $this->setValue( |
|
|
|||
27 | 32 | static::calculate( |
|
28 | 32 | $this->value(), '+', $unit->to($this->unit())->value() |
|
29 | 24 | ) |
|
30 | 24 | ); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * Sub the unit instance. |
||
35 | * |
||
36 | * @param \Arcanedev\Units\Contracts\UnitMeasure $unit |
||
37 | * |
||
38 | * @return \Arcanedev\Units\Contracts\UnitMeasure |
||
39 | */ |
||
40 | 16 | public function sub(UnitMeasure $unit) |
|
41 | { |
||
42 | 16 | return $this->setValue( |
|
43 | 16 | static::calculate( |
|
44 | 16 | $this->value(), '-', $unit->to($this->unit())->value() |
|
45 | 12 | ) |
|
46 | 12 | ); |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Multiply unit by the given number. |
||
51 | * |
||
52 | * @param float|int $number |
||
53 | * |
||
54 | * @return \Arcanedev\Units\Contracts\UnitMeasure |
||
55 | */ |
||
56 | 16 | public function multiply($number) |
|
57 | { |
||
58 | 16 | return $this->setValue( |
|
59 | 16 | static::calculate($this->value(), 'x', $number) |
|
60 | 12 | ); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Divide unit by the given number. |
||
65 | * |
||
66 | * @param float|int $number |
||
67 | * |
||
68 | * @return \Arcanedev\Units\Contracts\Weight |
||
69 | */ |
||
70 | 16 | public function divide($number) |
|
71 | { |
||
72 | 16 | return $this->setValue( |
|
73 | 16 | static::calculate($this->value(), '/', $number) |
|
74 | 6 | ); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Calculate the value. |
||
79 | * |
||
80 | * @param float|int $a |
||
81 | * @param string $operator |
||
82 | * @param float|int $b |
||
83 | * |
||
84 | * @return float|int |
||
85 | */ |
||
86 | protected static function calculate($a, $operator, $b) |
||
101 | } |
||
102 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.