|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of handelsgids/sales-periods. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Handelsgids NV |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Handelsgids\SalesPeriods; |
|
12
|
|
|
|
|
13
|
|
|
use Carbon\Carbon; |
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use Handelsgids\SalesPeriods\Exception\NoPeriodsFoundForRegionException; |
|
17
|
|
|
use Handelsgids\SalesPeriods\Model\SalesPeriod; |
|
18
|
|
|
use ReflectionException; |
|
19
|
|
|
|
|
20
|
|
|
class SalesPeriods |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $region; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int */ |
|
26
|
|
|
private $year; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $region |
|
30
|
|
|
* @param int $year |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($region = null, $year = null) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($region === null) { |
|
35
|
|
|
$region = Regions::DEFAULT_REGION; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($year === null) { |
|
39
|
|
|
$year = (int) date('Y'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->region = $region; |
|
43
|
|
|
$this->year = $year; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param DateTime $date |
|
48
|
|
|
* @param bool $returnPeriodMeta |
|
49
|
|
|
* @return bool|SalesPeriod |
|
50
|
|
|
* @throws NoPeriodsFoundForRegionException |
|
51
|
|
|
* @throws ReflectionException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function inSalesPeriod(DateTime $date, $returnPeriodMeta = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$carbon = Carbon::instance($date); |
|
56
|
|
|
|
|
57
|
|
|
if ($returnPeriodMeta === null) { |
|
58
|
|
|
$returnPeriodMeta = false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @var SalesPeriod[] $periods */ |
|
62
|
|
|
$periods = $this->getSalesPeriods(); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($periods as $period) { |
|
65
|
|
|
if ($carbon->between($period->getStartDate(), $period->getEndDate())) { |
|
66
|
|
|
if ($returnPeriodMeta) { |
|
67
|
|
|
return $period; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return SalesPeriod[] |
|
79
|
|
|
* @throws NoPeriodsFoundForRegionException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getSalesPeriods() |
|
82
|
|
|
{ |
|
83
|
|
|
$namespace = sprintf('Handelsgids\\SalesPeriods\\Region\\%s\\', $this->region); |
|
84
|
|
|
$path = sprintf(__DIR__ . '/Region/%s/', $this->region); |
|
85
|
|
|
|
|
86
|
|
|
$periods = []; |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
$periodFiles = scandir($path, SCANDIR_SORT_ASCENDING); |
|
90
|
|
|
|
|
91
|
|
|
if ($periodFiles !== false) { |
|
92
|
|
|
$periodClasses = array_diff($periodFiles, array('.', '..')); |
|
93
|
|
|
$periodClasses = array_map(function($value) { |
|
94
|
|
|
return str_replace('.php', '', $value); |
|
95
|
|
|
}, $periodClasses); |
|
96
|
|
|
|
|
97
|
|
|
foreach ($periodClasses as $periodClass) { |
|
98
|
|
|
$class = $namespace . $periodClass; |
|
99
|
|
|
$periods[] = new $class($this->year); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} catch (Exception $e) { |
|
103
|
|
|
throw new NoPeriodsFoundForRegionException( |
|
104
|
|
|
'No sales periods found for set region. Make sure the region you set actually exists. ' . PHP_EOL . |
|
105
|
|
|
'Available regions are: ' . implode(', ', Regions::all()) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $periods; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|