1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
7
|
|
|
* form for use in connection with the web services and APIs provided by |
8
|
|
|
* Facebook. |
9
|
|
|
* |
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace FacebookAds\Object; |
26
|
|
|
|
27
|
|
|
use FacebookAds\Http\RequestInterface; |
28
|
|
|
use FacebookAds\Object\Values\CustomAudienceTypes; |
29
|
|
|
use FacebookAds\Object\Fields\CustomAudienceFields; |
30
|
|
|
use FacebookAds\Object\CustomAudienceNormalizers\HashNormalizer; |
31
|
|
|
use FacebookAds\Object\Fields\CustomAudienceMultikeySchemaFields; |
32
|
|
|
|
33
|
|
|
class CustomAudienceMultiKey extends AbstractCrudObject { |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
const HASH_TYPE_SHA256 = 'sha256'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \ArrayObject |
42
|
|
|
*/ |
43
|
|
|
protected $normalizers; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function getEndpoint() { |
49
|
|
|
return 'customaudiences'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return CustomAudienceFields |
54
|
|
|
*/ |
55
|
|
|
public static function getFieldsEnum() { |
56
|
|
|
return CustomAudienceFields::getInstance(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return \ArrayObject |
61
|
|
|
*/ |
62
|
|
|
public function getNormalizers() { |
63
|
|
|
if ($this->normalizers === null) { |
64
|
|
|
$this->normalizers = new \ArrayObject(array( |
65
|
|
|
new CustomAudienceNormalizers\EmailNormalizer(), |
66
|
|
|
new CustomAudienceNormalizers\PhoneNormalizer(), |
67
|
|
|
new CustomAudienceNormalizers\MadidNormalizer(), |
68
|
|
|
new CustomAudienceNormalizers\GenderNormalizer(), |
69
|
|
|
new CustomAudienceNormalizers\BirthYearNormalizer(), |
70
|
|
|
new CustomAudienceNormalizers\DateNormalizer(), |
71
|
|
|
new CustomAudienceNormalizers\FirstNameNormalizer(), |
72
|
|
|
new CustomAudienceNormalizers\LastNameNormalizer(), |
73
|
|
|
new CustomAudienceNormalizers\FirstNameInitialNormalizer(), |
74
|
|
|
new CustomAudienceNormalizers\StateNormalizer(), |
75
|
|
|
new CustomAudienceNormalizers\CityNormalizer(), |
76
|
|
|
new CustomAudienceNormalizers\ZipNormalizer(), |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
return $this->normalizers; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Add users to the AdCustomAudiences. There is no max on the total number of |
84
|
|
|
* users that can be added to an audience, but up to 10000 users can be added |
85
|
|
|
* at a given time. |
86
|
|
|
* |
87
|
|
|
* @param array $users |
88
|
|
|
* @param array $types |
89
|
|
|
* @param bool $is_hashed |
90
|
|
|
* @param bool $is_normalized |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function addUsers( |
94
|
|
|
array $users, |
95
|
|
|
array $types, |
96
|
|
|
$is_hashed = false, |
97
|
|
|
$is_normalized = false) { |
98
|
|
|
|
99
|
|
|
$params = $this->formatParams($users, $types, $is_hashed, $is_normalized); |
100
|
|
|
return $this->getApi()->call( |
101
|
|
|
'/'.$this->assureId().'/users', |
102
|
|
|
RequestInterface::METHOD_POST, |
103
|
|
|
$params)->getContent(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Delete users from AdCustomAudiences |
108
|
|
|
* |
109
|
|
|
* @param array $users |
110
|
|
|
* @param array $types |
111
|
|
|
* @param bool $is_hashed |
112
|
|
|
* @param bool $is_normalized |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function removeUsers( |
116
|
|
|
array $users, |
117
|
|
|
array $types, |
118
|
|
|
$is_hashed = false, |
119
|
|
|
$is_normalized = false) { |
120
|
|
|
|
121
|
|
|
$params = $this->formatParams($users, $types, $is_hashed, $is_normalized); |
122
|
|
|
return $this->getApi()->call( |
123
|
|
|
'/'.$this->assureId().'/users', |
124
|
|
|
RequestInterface::METHOD_DELETE, |
125
|
|
|
$params)->getContent(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Take users and format them correctly for the request |
130
|
|
|
* |
131
|
|
|
* @param array $users |
132
|
|
|
* @param array $types |
133
|
|
|
* @param bool $is_hashed |
134
|
|
|
* @param bool $is_normalized |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
protected function formatParams( |
138
|
|
|
array $users, |
139
|
|
|
array $types, |
140
|
|
|
$is_hashed = false, |
141
|
|
|
$is_normalized = false) { |
142
|
|
|
|
143
|
|
|
if (!$is_hashed) { |
144
|
|
|
if ($is_normalized) { |
145
|
|
|
$normalizers = new \ArrayObject(array( |
146
|
|
|
new HashNormalizer() |
147
|
|
|
)); |
148
|
|
|
} |
149
|
|
|
else { |
150
|
|
|
$normalizers = clone $this->getNormalizers(); |
151
|
|
|
$normalizers->append(new HashNormalizer()); |
152
|
|
|
} |
153
|
|
|
foreach ($users as &$user) { |
154
|
|
|
if (count($types) != count($user)) { |
155
|
|
|
throw new \InvalidArgumentException( |
156
|
|
|
"Number of keys in each list in the data should ". |
157
|
|
|
"match the number of keys specified in scheme"); |
158
|
|
|
break; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
foreach ($user as $index => &$key_value) { |
161
|
|
|
foreach ($normalizers as $normalizer) { |
162
|
|
|
if ($normalizer->shouldNormalize($types[$index], $key_value)) { |
163
|
|
|
$key_value = $normalizer->normalize($types[$index], $key_value); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$payload = array( |
171
|
|
|
'schema' => $types, |
172
|
|
|
'is_raw' => true, |
173
|
|
|
'data' => $users, |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
return array('payload' => $payload); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.