|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\FacebookPoster; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use NotificationChannels\FacebookPoster\Attachments\Image; |
|
7
|
|
|
use NotificationChannels\FacebookPoster\Attachments\Video; |
|
8
|
|
|
use NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent; |
|
9
|
|
|
|
|
10
|
|
|
class FacebookPosterPost |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The post message. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $message; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The post link. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $link; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The post media. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \NotificationChannels\FacebookPoster\Attachments\Attachment |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $media; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Additional post parameters. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $params; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The post API endpoint. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $endpoint = 'me/feed'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Create a new post instance. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $message |
|
51
|
|
|
* @return void |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
7 |
|
public function __construct($message) |
|
54
|
|
|
{ |
|
55
|
7 |
|
$this->message = $message; |
|
56
|
7 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the post message. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
7 |
|
public function getMessage() |
|
64
|
|
|
{ |
|
65
|
7 |
|
return $this->message; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set a post link. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $link |
|
72
|
|
|
* |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function withLink($link) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$this->link = $link; |
|
78
|
|
|
|
|
79
|
1 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set a post image. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $path |
|
86
|
|
|
* @param string $endpoint |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function withImage($path, $endpoint = 'me/photos') |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->media = new Image($path); |
|
92
|
1 |
|
$this->endpoint = $endpoint; |
|
93
|
|
|
|
|
94
|
1 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set a post video. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $path |
|
101
|
|
|
* @param string $title |
|
102
|
|
|
* @param string $description |
|
103
|
|
|
* @param string $endpoint |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function withVideo($path, $title = null, $description = null, $endpoint = 'me/videos') |
|
107
|
|
|
{ |
|
108
|
1 |
|
$this->media = new Video($path); |
|
109
|
1 |
|
$this->params['title'] = $title; |
|
110
|
1 |
|
$this->params['description'] = $description; |
|
111
|
1 |
|
$this->endpoint = $endpoint; |
|
112
|
|
|
|
|
113
|
1 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Schedule the post for a date in the future. |
|
118
|
|
|
* |
|
119
|
|
|
* @param \DateTimeInterface|int $timestamp |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function scheduledFor($timestamp) |
|
123
|
|
|
{ |
|
124
|
3 |
|
$timestamp = $timestamp instanceof DateTimeInterface |
|
125
|
2 |
|
? $timestamp->getTimestamp() |
|
126
|
3 |
|
: $timestamp; |
|
127
|
|
|
|
|
128
|
3 |
|
$this->params['published'] = false; |
|
129
|
3 |
|
$this->params['scheduled_publish_time'] = $timestamp; |
|
130
|
|
|
|
|
131
|
3 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Return Facebook API endpoint. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
4 |
|
public function getEndpoint() |
|
140
|
|
|
{ |
|
141
|
4 |
|
return $this->endpoint; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the media attached to the post. |
|
146
|
|
|
* |
|
147
|
|
|
* @return \NotificationChannels\FacebookPoster\Attachment |
|
148
|
|
|
*/ |
|
149
|
4 |
|
public function getMedia() |
|
150
|
|
|
{ |
|
151
|
4 |
|
return $this->media; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Build Facebook post request body. |
|
156
|
|
|
* |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
7 |
|
public function getBody() |
|
160
|
|
|
{ |
|
161
|
7 |
|
$this->validate(); |
|
162
|
|
|
|
|
163
|
|
|
$body = [ |
|
164
|
7 |
|
'message' => $this->getMessage(), |
|
165
|
|
|
]; |
|
166
|
|
|
|
|
167
|
7 |
|
if ($this->link != null) { |
|
168
|
1 |
|
$body['link'] = $this->link; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
7 |
|
if ($this->params != null) { |
|
172
|
4 |
|
$body = array_merge($body, $this->params); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
7 |
|
return $body; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Validate that there is acceptable post content. |
|
180
|
|
|
* |
|
181
|
|
|
* @throws \NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent |
|
182
|
|
|
*/ |
|
183
|
7 |
|
protected function validate() |
|
184
|
|
|
{ |
|
185
|
7 |
|
if ($this->message || $this->link || $this->media) { |
|
186
|
7 |
|
return; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
throw InvalidPostContent::noContentSet(); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.