| 1 |  |  | <?php namespace Arcanedev\LaravelMetrics\Expressions\TrendDateFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Arcanedev\LaravelMetrics\Exceptions\InvalidTrendUnitException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Arcanedev\LaravelMetrics\Expressions\TrendDateFormat\Expression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Arcanedev\LaravelMetrics\Metrics\Trend; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class     SqliteExpression | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package  Arcanedev\LaravelMetrics\Expressions\TrendDateFormat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author   ARCANEDEV <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class SqliteExpression extends Expression | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      |  Main Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Get the value of the expression. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 48 |  |     public function getValue() | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 48 |  |         $interval = $this->interval(); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 48 |  |         switch ($this->unit) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 24 |  |             case Trend::BY_MONTHS: | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 8 |  |                 return "strftime('%Y-%m', datetime({$this->wrap($this->value)}, {$interval}))"; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 20 |  |             case Trend::BY_WEEKS: | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 8 |  |                 return "strftime('%Y-%W', datetime({$this->wrap($this->value)}, {$interval}))"; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 16 |  |             case Trend::BY_DAYS: | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 16 |  |                 return "strftime('%Y-%m-%d', datetime({$this->wrap($this->value)}, {$interval}))"; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 8 |  |             case Trend::BY_HOURS: | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 8 |  |                 return "strftime('%Y-%m-%d %H:00', datetime({$this->wrap($this->value)}, {$interval}))"; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 4 |  |             case Trend::BY_MINUTES: | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 8 |  |                 return "strftime('%Y-%m-%d %H:%M:00', datetime({$this->wrap($this->value)}, {$interval}))"; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             default: | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                 throw InvalidTrendUnitException::make($this->unit); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Get the interval. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 48 |  |     private function interval(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 48 |  |         $offset = $this->offset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 48 |  |         if ($offset > 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             return '\'+'.$offset.' hour\''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 48 |  |         elseif ($offset === 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 28 |  |             return '\'+0 hour\''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 20 |  |             return '\'-'.($offset * -1).' hour\''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  |