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
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.