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
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
public static function createFromTrend( Vehicle $vehicle, array $fuelup, InputConversion $input = null ) { |
21
|
|
|
// Trend is always in real numbers, and only its natives are reliable so use those |
22
|
|
|
$input or $input = $client->createTrendInputConversion(); |
|
|
|
|
23
|
|
|
|
24
|
|
|
// @todo Parse date correctly |
25
|
|
|
$date = DateTime::createFromFormat('Y-m-d H:i:s', $fuelup['fuelup_date']); |
26
|
|
|
return new static($vehicle, $date, $fuelup['miles_last_fuelup'], $fuelup['amount'], $input); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
public static function createFromDetail( Vehicle $vehicle, array $fuelup, InputConversion $input = null ) { |
|
|
|
|
33
|
|
|
// @todo Parse date correctly |
34
|
|
|
$date = DateTime::createFromFormat('d-m-y', $fuelup['fuelup_date']); |
35
|
|
|
return new static($vehicle, $date, $fuelup['miles_last_fuelup'], $fuelup['amount']); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
|
protected function __construct( Vehicle $vehicle, DateTime $date, $raw_distance, $raw_amount, InputConversion $input = null ) { |
42
|
|
|
$this->vehicle = $vehicle; |
43
|
|
|
|
44
|
|
|
$input or $input = $vehicle->client->input; |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->date = $date; |
47
|
|
|
|
48
|
|
|
$this->raw_amount = $input->convertNumber($raw_amount); |
|
|
|
|
49
|
|
|
$this->raw_distance = $input->convertNumber($raw_distance); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->amount = $input->convertVolume($this->raw_amount); |
|
|
|
|
52
|
|
|
$this->distance = $input->convertDistance($this->raw_distance); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$this->mileage = static::createMileage($this->distance, $this->amount); |
|
|
|
|
55
|
|
|
|
56
|
|
|
// $this->original = $fuelup; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
public static function createMileage( Length $distance, Volume $amount ) { |
63
|
|
|
// Since we don't know the original mileage from here, we'll construct a known unit from known values |
64
|
|
|
return new Mileage($distance->to('km') / $amount->to('l'), 'kmpl'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
public static function dateCmp( FuelUp $a, FuelUp $b ) { |
71
|
|
|
return $b->date->getTimestamp() - $a->date->getTimestamp(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.