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