Issues (10)

src/Timezone.php (3 issues)

1
<?php
2
3
namespace JamesMills\LaravelTimezone;
4
5
use Carbon\Carbon;
6
7
class Timezone
8
{
9
    /**
10
     * @param  Carbon\Carbon|null  $date
0 ignored issues
show
The type Carbon\Carbon\Carbon was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
     * @param  null  $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
12
     * @param  bool  $format_timezone
13
     * @return string
14
     */
15
    public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false, $enableTranslation = null) : string
16
    {
17
        if (is_null($date)) {
18
            return __('Empty');
19
        }
20
21
        $timezone = (auth()->user()->timezone) ?? config('app.timezone');
22
        
23
        $enableTranslation = $enableTranslation !== null ? $enableTranslation : config('timezone.enableTranslation');
24
        
25
        $date->setTimezone($timezone);
26
27
        if (is_null($format)) {
0 ignored issues
show
The condition is_null($format) is always true.
Loading history...
28
            return $enableTranslation ? $date->translatedFormat(config('timezone.format')) : $date->format(config('timezone.format'));
29
        }
30
31
        $formatted_date_time = $enableTranslation ? $date->translatedFormat($format) : $date->format($format);
32
33
        if ($format_timezone) {
34
            return $formatted_date_time . ' ' . $this->formatTimezone($date);
35
        }
36
37
        return $formatted_date_time;
38
    }
39
40
    /**
41
     * @param $date
42
     * @return Carbon\Carbon
43
     */
44
    public function convertFromLocal($date) : Carbon
45
    {
46
        return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC');
47
    }
48
49
    /**
50
     * @param  Carbon\Carbon  $date
51
     * @return string
52
     */
53
    private function formatTimezone(Carbon $date) : string
54
    {
55
        $timezone = $date->format('e');
56
        $parts = explode('/', $timezone);
57
58
        if (count($parts) > 1) {
59
            return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0];
60
        }
61
62
        return str_replace('_', ' ', $parts[0]);
63
    }
64
}
65