1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Teazee package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @license MIT License |
9
|
|
|
*/ |
10
|
|
|
namespace Teazee\Provider; |
11
|
|
|
|
12
|
|
|
use RuntimeException; |
13
|
|
|
use Teazee\Exception\ChainNoResult; |
14
|
|
|
use Teazee\ZoneInfo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Query multiple providers until a result is found. |
18
|
|
|
* |
19
|
|
|
* @author Michael Crumm <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Chain implements Provider |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Provider[] |
25
|
|
|
*/ |
26
|
|
|
private $providers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Chain constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $providers |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $providers = []) |
34
|
|
|
{ |
35
|
7 |
|
$this->providers = $providers; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the name of this provider. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getName() |
44
|
|
|
{ |
45
|
1 |
|
return 'chain'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Finds a TimeZone for a location on the surface of the earth. |
50
|
|
|
* |
51
|
|
|
* @param string|float $lat Coordinate latitude. |
52
|
|
|
* @param string|float $lng Coordinate longitude. |
53
|
|
|
* @param int|null $timestamp Seconds since Jan 1, 1970 UTC. |
54
|
|
|
* |
55
|
|
|
* @return ZoneInfo |
56
|
|
|
*/ |
57
|
|
|
public function find($lat, $lng, $timestamp = null) |
58
|
|
|
{ |
59
|
5 |
|
$exceptions = []; |
60
|
|
|
|
61
|
|
|
/** @var Provider $provider */ |
62
|
|
|
foreach ($this->providers as $provider) { |
63
|
|
|
try { |
64
|
4 |
|
return $provider->find($lat, $lng, $timestamp); |
65
|
|
|
} catch (RuntimeException $ex) { |
66
|
2 |
|
$exceptions[] = $ex; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new ChainNoResult( |
71
|
|
|
sprintf('No provider could find %f,%f%s', $lat, $lng, $timestamp ? ' @'.$timestamp : ''), |
72
|
|
|
$exceptions |
73
|
2 |
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|