1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMetrics\Expressions\TrendDateFormat; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMetrics\Expressions\Expression as BaseExpression; |
8
|
|
|
use Cake\Chronos\Chronos; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Expression |
13
|
|
|
* |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class Expression extends BaseExpression |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Properties |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The query builder being used to build the trend. |
25
|
|
|
* |
26
|
|
|
* @var \Illuminate\Database\Eloquent\Builder |
27
|
|
|
*/ |
28
|
|
|
public $query; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The unit being measured. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $unit; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The users's local timezone. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $timezone; |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Constructor |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new raw query expression. |
51
|
|
|
* |
52
|
|
|
* @param mixed $value |
53
|
|
|
* @param string $unit |
54
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
55
|
|
|
* @param string|null $timezone |
56
|
|
|
*/ |
57
|
256 |
|
public function __construct($value, string $unit, Builder $query, $timezone = null) |
58
|
|
|
{ |
59
|
256 |
|
parent::__construct($value); |
60
|
|
|
|
61
|
256 |
|
$this->unit = $unit; |
62
|
256 |
|
$this->query = $query; |
63
|
256 |
|
$this->timezone = $timezone; |
64
|
256 |
|
} |
65
|
|
|
|
66
|
|
|
/* ----------------------------------------------------------------- |
67
|
|
|
| Other Methods |
68
|
|
|
| ----------------------------------------------------------------- |
69
|
|
|
*/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the timezone offset for the user's timezone. |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
256 |
|
public function offset() |
77
|
|
|
{ |
78
|
256 |
|
return $this->timezone |
79
|
108 |
|
? Chronos::now()->setTimezone($this->timezone)->getOffset() / 60 / 60 |
80
|
256 |
|
: 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Wrap the given value using the query's grammar. |
85
|
|
|
* |
86
|
|
|
* @param string $value |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
240 |
|
protected function wrap($value) |
91
|
|
|
{ |
92
|
240 |
|
return $this->query->getQuery()->getGrammar()->wrap($value); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|