Issues (118)

src/Lib/TimeZones.php (5 issues)

1
<?php
2
3
namespace Someshwer\WorldCountries\Lib;
4
5
use Someshwer\WorldCountries\Data\DataRepository;
6
7
/**
8
 * Author: Someshwer Bandapally
9
 * Date: 14-07-2018.
10
 *
11
 * This class provides timezones info
12
 *
13
 * Class TimeZones
14
 */
15
class TimeZones extends Currencies
16
{
17
    /**
18
     * @var DataRepository
19
     */
20
    private $data;
21
22
    /**
23
     * TimeZones constructor.
24
     *
25
     * @param DataRepository $dataRepository
26
     */
27
    public function __construct(DataRepository $dataRepository)
28
    {
29
        parent::__construct($dataRepository);
30
        $this->data = $dataRepository;
31
    }
32
33
    /**
34
     * This method prepares timezones.
35
     *
36
     * @return array
37
     */
38
    private function prepareTimezones()
39
    {
40
        $timezone_identifiers_list = $this->data->timezones();
41
        $zones_array = [];
42
        $timestamp = time();
43
        foreach ($timezone_identifiers_list as $key => $zone) {
44
            date_default_timezone_set($zone);
45
            $zones_array[$key]['region'] = $zones_array[$key]['region'] = substr($zone, 0, strpos($zone, '/'));
46
            $zones_array[$key]['zone'] = $zone;
47
            $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT '.date('P', $timestamp);
48
        }
49
50
        return $zones_array;
51
    }
52
53
    /**
54
     * Returns all timezones.
55
     *
56
     * @return array
57
     */
58
    public function timezones()
59
    {
60
        return $this->prepareTimezones();
61
    }
62
63
    /**
64
     * Returns all timezones regions.
65
     *
66
     * @return array
67
     */
68
    public function timezoneRegions()
69
    {
70
        $timezone_regions = $this->prepareTimezones();
71
        $timezone_regions_group = collect($timezone_regions)->groupBy('region')->keys();
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        $timezone_regions_group = /** @scrutinizer ignore-call */ collect($timezone_regions)->groupBy('region')->keys();
Loading history...
72
        $regions = [];
73
        foreach ($timezone_regions_group as $item) {
74
            if ($item != '') {
75
                $regions[] = $item;
76
            }
77
        }
78
79
        return $regions;
80
    }
81
82
    /**
83
     * Fetches and returns timezones by regions.
84
     *
85
     * @param null $region
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $region is correct as it would always require null to be passed?
Loading history...
86
     *
87
     * @return array|mixed
88
     */
89
    public function timezonesByRegion($region = null)
90
    {
91
        if (!$region) {
0 ignored issues
show
$region is of type null, thus it always evaluated to false.
Loading history...
92
            return [];
93
        }
94
        $timezone_regions = $this->prepareTimezones();
95
        $timezone_regions_group = collect($timezone_regions)->groupBy('region');
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        $timezone_regions_group = /** @scrutinizer ignore-call */ collect($timezone_regions)->groupBy('region');
Loading history...
96
        $regional_timezones = $timezone_regions_group->get($region);
97
        if (!$regional_timezones) {
98
            return [];
99
        }
100
101
        return $regional_timezones;
102
    }
103
104
    /**
105
     * Searches timezones by name.
106
     *
107
     * @param $search_key
108
     *
109
     * @return array|static
110
     */
111
    public function searchTimezone($search_key)
112
    {
113
        if (!$search_key || ($search_key == '/')) {
114
            return [];
115
        }
116
        $timezones = $this->prepareTimezones();
117
        $filtered_timezones = collect($timezones)->filter(function ($item) use ($search_key) {
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        $filtered_timezones = /** @scrutinizer ignore-call */ collect($timezones)->filter(function ($item) use ($search_key) {
Loading history...
118
            return strpos(strtolower($item['zone']), strtolower($search_key)) !== false;
119
        })->values();
120
121
        return $filtered_timezones;
122
    }
123
}
124