1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* vipnytt/RobotsTxtParser |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/VIPnytt/RobotsTxtParser |
6
|
|
|
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace vipnytt\RobotsTxtParser\Client\Directives; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RequestRateClient |
13
|
|
|
* |
14
|
|
|
* @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/RequestRateClient.md for documentation |
15
|
|
|
* @package vipnytt\RobotsTxtParser\Client\Directives |
16
|
|
|
*/ |
17
|
|
|
class RequestRateClient extends DelayCore |
18
|
|
|
{ |
19
|
|
|
use DirectiveClientTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Rates |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $rates = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Fallback value |
29
|
|
|
* @var float|int |
30
|
|
|
*/ |
31
|
|
|
private $fallbackValue; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* RequestRateClient constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $baseUri |
37
|
|
|
* @param string $product |
38
|
|
|
* @param array $rates |
39
|
|
|
* @param float|int $fallbackValue |
40
|
|
|
*/ |
41
|
|
|
public function __construct($baseUri, $product, array $rates, $fallbackValue = 0) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($baseUri, $product); |
44
|
|
|
$this->rates = $rates; |
45
|
|
|
$this->fallbackValue = $fallbackValue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Export |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function export() |
54
|
|
|
{ |
55
|
|
|
return $this->rates; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get rate |
60
|
|
|
* |
61
|
|
|
* @param int|null $timestamp |
62
|
|
|
* @return float|int |
63
|
|
|
*/ |
64
|
|
|
public function getValue($timestamp = null) |
65
|
|
|
{ |
66
|
|
|
$values = $this->determine(is_int($timestamp) ? $timestamp : time()); |
67
|
|
|
if (count($values) > 0 && |
68
|
|
|
($rate = max($values)) > 0 |
69
|
|
|
) { |
70
|
|
|
return $rate; |
71
|
|
|
} |
72
|
|
|
return $this->fallbackValue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Determine rates |
77
|
|
|
* |
78
|
|
|
* @param int $timestamp |
79
|
|
|
* @return float[]|int[] |
80
|
|
|
*/ |
81
|
|
|
private function determine($timestamp) |
82
|
|
|
{ |
83
|
|
|
$values = []; |
84
|
|
|
foreach ($this->rates as $array) { |
85
|
|
|
if (!isset($array['from']) || |
86
|
|
|
!isset($array['to']) |
87
|
|
|
) { |
88
|
|
|
$values[] = $array['rate']; |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
if ($this->isBetween($timestamp, $array['from'], $array['to'], 'Hi')) { |
92
|
|
|
$values[] = $array['rate']; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return $values; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|