1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Model\ProfileSettings; |
6
|
|
|
|
7
|
|
|
use Kerox\Messenger\Exception\InvalidTypeException; |
8
|
|
|
use Kerox\Messenger\Helper\UtilityTrait; |
9
|
|
|
use Kerox\Messenger\Helper\ValidatorTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @deprecated Since version 3.3.0 and will be removed in version 4.0.0. |
13
|
|
|
*/ |
14
|
|
|
class TargetAudience implements \JsonSerializable |
15
|
|
|
{ |
16
|
|
|
use UtilityTrait; |
17
|
|
|
use ValidatorTrait; |
18
|
|
|
|
19
|
|
|
private const AUDIENCE_TYPE_ALL = 'all'; |
20
|
|
|
private const AUDIENCE_TYPE_CUSTOM = 'custom'; |
21
|
|
|
private const AUDIENCE_TYPE_NONE = 'none'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $audienceType; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $whitelistCountries; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $blacklistCountries; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* TargetAudience constructor. |
40
|
|
|
* |
41
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
42
|
|
|
*/ |
43
|
2 |
|
public function __construct( |
44
|
|
|
string $audienceType = self::AUDIENCE_TYPE_ALL, |
45
|
|
|
array $whitelistCountries = [], |
46
|
|
|
array $blacklistCountries = [] |
47
|
|
|
) { |
48
|
2 |
|
$this->isValidAudienceType($audienceType); |
49
|
1 |
|
$this->isValidCountries($whitelistCountries); |
50
|
1 |
|
$this->isValidCountries($blacklistCountries); |
51
|
|
|
|
52
|
1 |
|
$this->audienceType = $audienceType; |
53
|
1 |
|
$this->whitelistCountries = $whitelistCountries; |
54
|
1 |
|
$this->blacklistCountries = $blacklistCountries; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
59
|
|
|
* |
60
|
|
|
* @return \Kerox\Messenger\Model\ProfileSettings\TargetAudience |
61
|
|
|
*/ |
62
|
2 |
|
public static function create( |
63
|
|
|
string $audienceType = self::AUDIENCE_TYPE_ALL, |
64
|
|
|
array $whitelistCountries = [], |
65
|
|
|
array $blacklistCountries = [] |
66
|
|
|
): self { |
67
|
2 |
|
return new self($audienceType, $whitelistCountries, $blacklistCountries); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
72
|
|
|
* |
73
|
|
|
* @return \Kerox\Messenger\Model\ProfileSettings\TargetAudience |
74
|
|
|
*/ |
75
|
1 |
|
public function addWhitelistCountry(string $country): self |
76
|
|
|
{ |
77
|
1 |
|
$this->isValidCountry($country); |
78
|
|
|
|
79
|
1 |
|
$this->whitelistCountries[] = $country; |
80
|
|
|
|
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
86
|
|
|
* |
87
|
|
|
* @return \Kerox\Messenger\Model\ProfileSettings\TargetAudience |
88
|
|
|
*/ |
89
|
1 |
|
public function addBlacklistCountry(string $country): self |
90
|
|
|
{ |
91
|
1 |
|
$this->isValidCountry($country); |
92
|
|
|
|
93
|
1 |
|
$this->blacklistCountries[] = $country; |
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
100
|
|
|
*/ |
101
|
1 |
|
private function isValidCountries(array $countries): void |
102
|
|
|
{ |
103
|
1 |
|
if (!empty($countries)) { |
104
|
1 |
|
foreach ($countries as $country) { |
105
|
1 |
|
$this->isValidCountry($country); |
106
|
|
|
} |
107
|
|
|
} |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
112
|
|
|
*/ |
113
|
2 |
|
private function isValidAudienceType(string $audienceType): void |
114
|
|
|
{ |
115
|
2 |
|
$allowedAudienceType = $this->getAllowedAudienceType(); |
116
|
2 |
|
if (!\in_array($audienceType, $allowedAudienceType, true)) { |
117
|
1 |
|
throw new InvalidTypeException(sprintf('audienceType must be either "%s".', implode(', ', $allowedAudienceType))); |
118
|
|
|
} |
119
|
1 |
|
} |
120
|
|
|
|
121
|
2 |
|
private function getAllowedAudienceType(): array |
122
|
|
|
{ |
123
|
|
|
return [ |
124
|
2 |
|
self::AUDIENCE_TYPE_ALL, |
125
|
2 |
|
self::AUDIENCE_TYPE_CUSTOM, |
126
|
2 |
|
self::AUDIENCE_TYPE_NONE, |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
public function toArray(): array |
131
|
|
|
{ |
132
|
|
|
$array = [ |
133
|
1 |
|
'audience_type' => $this->audienceType, |
134
|
|
|
'countries' => [ |
135
|
1 |
|
'whitelist' => $this->whitelistCountries, |
136
|
1 |
|
'blacklist' => $this->blacklistCountries, |
137
|
|
|
], |
138
|
|
|
]; |
139
|
|
|
|
140
|
1 |
|
return $this->arrayFilter($array); |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
public function jsonSerialize(): array |
144
|
|
|
{ |
145
|
1 |
|
return $this->toArray(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.