1
|
|
|
<?php namespace Arcanedev\Units\Traits; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Units\Contracts\UnitMeasure; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait Calculatable |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\Units\Traits |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
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
|
|
|
public function add(UnitMeasure $unit) |
25
|
|
|
{ |
26
|
|
|
return $this->setValue( |
|
|
|
|
27
|
|
|
static::calculate( |
28
|
|
|
$this->value(), '+', $unit->to($this->unit())->value() |
|
|
|
|
29
|
|
|
) |
30
|
|
|
); |
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
|
|
|
public function sub(UnitMeasure $unit) |
41
|
|
|
{ |
42
|
|
|
return $this->setValue( |
|
|
|
|
43
|
|
|
static::calculate( |
44
|
|
|
$this->value(), '-', $unit->to($this->unit())->value() |
|
|
|
|
45
|
|
|
) |
46
|
|
|
); |
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
|
|
|
public function multiply($number) |
57
|
|
|
{ |
58
|
|
|
return $this->setValue( |
|
|
|
|
59
|
|
|
static::calculate($this->value(), 'x', $number) |
|
|
|
|
60
|
|
|
); |
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
|
|
|
public function divide($number) |
71
|
|
|
{ |
72
|
|
|
return $this->setValue( |
|
|
|
|
73
|
|
|
static::calculate($this->value(), '/', $number) |
|
|
|
|
74
|
|
|
); |
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) |
87
|
|
|
{ |
88
|
|
|
$operations = $operations = [ |
89
|
|
|
'+' => function ($a, $b) { return $a + $b; }, |
90
|
|
|
'-' => function ($a, $b) { return $a - $b; }, |
91
|
|
|
'x' => function ($a, $b) { return $a * $b; }, |
92
|
|
|
'*' => function ($a, $b) { return $a * $b; }, |
93
|
|
|
'/' => function ($a, $b) { return $a / $b; }, |
94
|
|
|
'^' => function ($a, $b) { return pow($a, $b); }, |
95
|
174 |
|
]; |
96
|
|
|
|
97
|
232 |
|
return array_key_exists($operator, $operations) |
98
|
230 |
|
? call_user_func_array($operations[$operator], [$a, $b]) |
99
|
216 |
|
: $a; |
100
|
|
|
} |
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.