1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ianrizky\MoslemPray; |
4
|
|
|
|
5
|
|
|
use Ianrizky\MoslemPray\Drivers\AbstractDriver; |
6
|
|
|
use Ianrizky\MoslemPray\Drivers\MyQuran; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method static \Ianrizky\MoslemPray\Drivers\MyQuran myquran(array $config = []) Create an instance of the PrayerTimes driver. |
13
|
|
|
* @method static \Ianrizky\MoslemPray\Contracts\Response\HasPrayerTime getPrayerTime(mixed $city, \Illuminate\Support\Carbon|string|null $date = null) Return prayer time based on the given city and date. |
14
|
|
|
* @method static \Ianrizky\MoslemPray\Contracts\Response\HasPrayerTimeCollection getPrayerTimePerMonth(mixed $city, \Illuminate\Support\Carbon|string|int|null $year = null, int|null $month = null) Return list of prayer time based on the given city and month. |
15
|
|
|
* |
16
|
|
|
* @see \Ianrizky\MoslemPray\Contracts\Driverable |
17
|
|
|
*/ |
18
|
|
|
class MoslemPray |
19
|
|
|
{ |
20
|
1 |
|
use ForwardsCalls; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Driver instance object. |
24
|
|
|
* |
25
|
|
|
* @var \Ianrizky\MoslemPray\Drivers\AbstractDriver |
26
|
|
|
*/ |
27
|
|
|
protected $driver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* List of available driver name. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
public const DRIVERS = [ |
35
|
|
|
'myquran' => MyQuran::class, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Default driver class name. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public static $defaultDriverName = 'myquran'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new instance class. |
47
|
|
|
* |
48
|
|
|
* @param string|array|null $driverName |
49
|
|
|
* @param array $config |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
12 |
|
public function __construct($driverName = null, array $config = []) |
53
|
|
|
{ |
54
|
12 |
|
if (is_array($driverName)) { |
55
|
|
|
$driverName = $this->getDriverNameFromConfig($driverName); |
56
|
|
|
|
57
|
|
|
$config = Arr::except($config, 'driver'); |
58
|
12 |
|
} elseif (is_null($driverName)) { |
59
|
12 |
|
$driverName = static::$defaultDriverName; |
60
|
|
|
} |
61
|
|
|
|
62
|
12 |
|
$this->driver = static::createDriverInstance($driverName, $config); |
63
|
12 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return driver name value from given config. |
67
|
|
|
* |
68
|
|
|
* @param array $config |
69
|
|
|
* @return string |
70
|
|
|
* |
71
|
|
|
* @throws \InvalidArgumentException |
72
|
|
|
*/ |
73
|
|
|
protected function getDriverNameFromConfig(array $config): string |
74
|
|
|
{ |
75
|
|
|
$driverName = $config['driver'] ?? null; |
76
|
|
|
|
77
|
|
|
if (!static::isDriverAvailable($driverName)) { |
78
|
|
|
throw new InvalidArgumentException('Driver name is unidentified'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $driverName; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Determine whether the given driver name is available or not. |
86
|
|
|
* |
87
|
|
|
* @param string|null $name |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
12 |
|
public static function isDriverAvailable(?string $name): bool |
91
|
|
|
{ |
92
|
12 |
|
return in_array($name, array_keys(static::DRIVERS), true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create driver instance based on given driver name. |
97
|
|
|
* |
98
|
|
|
* @param string $driverName |
99
|
|
|
* @param array $config |
100
|
|
|
* @return \Ianrizky\MoslemPray\Drivers\AbstractDriver |
101
|
|
|
* |
102
|
|
|
* @throws \InvalidArgumentException |
103
|
|
|
*/ |
104
|
12 |
|
public static function createDriverInstance(string $driverName, array $config = []): AbstractDriver |
105
|
|
|
{ |
106
|
12 |
|
if (static::isDriverAvailable($driverName)) { |
107
|
12 |
|
$driver = static::DRIVERS[$driverName]; |
108
|
|
|
|
109
|
12 |
|
return new $driver($config); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw new InvalidArgumentException("Driver [$driverName] is not supported."); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Dynamically handle calls to the class. |
117
|
|
|
* |
118
|
|
|
* @param string $method |
119
|
|
|
* @param array $parameters |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
12 |
|
public function __call($method, $parameters) |
123
|
|
|
{ |
124
|
12 |
|
if (static::isDriverAvailable($method)) { |
125
|
12 |
|
return static::createDriverInstance($method, ...$parameters); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->forwardCallTo($this->driver, $method, $parameters); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Dynamically handle static calls to the class. |
133
|
|
|
* |
134
|
|
|
* @param string $method |
135
|
|
|
* @param array $parameters |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
12 |
|
public static function __callStatic($method, $parameters) |
139
|
|
|
{ |
140
|
12 |
|
return (new static)->{$method}(...$parameters); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|