1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rdx\fuelly; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use rdx\fuelly\Vehicle; |
7
|
|
|
use rdx\units\Length; |
8
|
|
|
use rdx\units\Mileage; |
9
|
|
|
use rdx\units\Volume; |
10
|
|
|
|
11
|
|
|
class FuelUp { |
12
|
|
|
|
13
|
|
|
public $vehicle; // rdx\fuelly\Vehicle |
14
|
|
|
|
15
|
|
|
public $date; // DateTime |
16
|
|
|
public $volume; // rdx\units\Volume |
17
|
|
|
public $distance; // rdx\units\Length |
18
|
|
|
public $mileage; // rdx\units\Mileage |
19
|
|
|
|
20
|
|
|
public $raw_volume; |
21
|
|
|
public $raw_distance; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
public static function createFromTrend( Vehicle $vehicle, array $fuelup, InputConversion $input = null ) { |
27
|
|
|
// Trend is always in real numbers, and only its natives are reliable so use those |
28
|
|
|
$input or $input = $vehicle->client->createTrendInputConversion(); |
|
|
|
|
29
|
|
|
|
30
|
|
|
// @todo Parse date correctly |
31
|
|
|
$date = DateTime::createFromFormat('Y-m-d H:i:s', $fuelup['fuelup_date']); |
32
|
|
|
return new static($vehicle, $date, $fuelup['miles_last_fuelup'], $fuelup['amount'], $input); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
|
|
public static function createFromDetail( Vehicle $vehicle, array $fuelup, InputConversion $input = null ) { |
39
|
|
|
// @todo Parse date correctly |
40
|
|
|
$date = DateTime::createFromFormat('d-m-y', $fuelup['fuelup_date']); |
41
|
|
|
return new static($vehicle, $date, $fuelup['miles_last_fuelup'], $fuelup['amount'], $input); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
*/ |
47
|
|
|
protected function __construct( Vehicle $vehicle, DateTime $date, $raw_distance, $raw_volume, InputConversion $input = null ) { |
48
|
|
|
$this->vehicle = $vehicle; |
49
|
|
|
|
50
|
|
|
$input or $input = $vehicle->client->input; |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->date = $date; |
53
|
|
|
|
54
|
|
|
$this->raw_volume = $input->convertNumber($raw_volume); |
55
|
|
|
$this->raw_distance = $input->convertNumber($raw_distance); |
56
|
|
|
|
57
|
|
|
$this->volume = $input->convertVolume($this->raw_volume); |
58
|
|
|
$this->distance = $input->convertDistance($this->raw_distance); |
59
|
|
|
|
60
|
|
|
$this->mileage = static::createMileage($this->distance, $this->volume); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
public static function createMileage( Length $distance, Volume $volume ) { |
67
|
|
|
// Since we don't know the original mileage from here, we'll construct a known unit from known values |
68
|
|
|
return new Mileage($distance->to('km') / $volume->to('l'), 'kmpl'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
|
|
public static function dateCmp( FuelUp $a, FuelUp $b ) { |
75
|
|
|
return $b->date->getTimestamp() - $a->date->getTimestamp(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.