1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers\Requests; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
6
|
|
|
|
7
|
|
|
class PinHelper |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Create Pinterest API request form commenting pin |
11
|
|
|
* |
12
|
|
|
* @param int $pinId |
13
|
|
|
* @param string $text |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public static function createCommentRequest($pinId, $text) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$dataJson = self::createPinIdRequest($pinId); |
19
|
|
|
$dataJson["options"]["text"] = $text; |
20
|
|
|
|
21
|
|
|
return self::createPinRequestData($dataJson); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create Pinterest API request form commenting pin |
26
|
|
|
* |
27
|
|
|
* @param int $pinId |
28
|
|
|
* @param int $commentId |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public static function createCommentDeleteRequest($pinId, $commentId) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$dataJson = self::createPinIdRequest($pinId); |
34
|
|
|
$dataJson["options"]["comment_id"] = $commentId; |
35
|
|
|
|
36
|
|
|
return self::createPinRequestData($dataJson); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Creates Pinterest API request for Pin creation |
41
|
|
|
* |
42
|
|
|
* @param string $description |
43
|
|
|
* @param string $imageUrl |
44
|
|
|
* @param int $boardId |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public static function createPinCreationRequest($imageUrl, $boardId, $description = "") |
48
|
|
|
{ |
49
|
|
|
$dataJson = [ |
50
|
|
|
"options" => [ |
51
|
|
|
"method" => "scraped", |
52
|
|
|
"description" => $description, |
53
|
|
|
"link" => $imageUrl, |
54
|
|
|
"image_url" => $imageUrl, |
55
|
|
|
"board_id" => $boardId, |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
return self::createPinRequestData($dataJson, "/pin/create/bookmarklet/?url=" . urlencode($imageUrl)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Creates Pinterest API request for Pin repin |
65
|
|
|
* |
66
|
|
|
* @param string $description |
67
|
|
|
* @param int $repinId |
68
|
|
|
* @param int $boardId |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public static function createRepinRequest($repinId, $boardId, $description) |
72
|
|
|
{ |
73
|
|
|
$dataJson = [ |
74
|
|
|
"options" => [ |
75
|
|
|
"board_id" => $boardId, |
76
|
|
|
"description" => stripslashes($description), |
77
|
|
|
"link" => stripslashes($repinId), |
78
|
|
|
"is_video" => null, |
79
|
|
|
"pin_id" => $repinId, |
80
|
|
|
], |
81
|
|
|
"context" => [], |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
return self::createPinRequestData($dataJson); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Creates Pinterest API request to get Pin info |
90
|
|
|
* |
91
|
|
|
* @param int $pinId |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public static function createInfoRequest($pinId) |
95
|
|
|
{ |
96
|
|
|
$dataJson = [ |
97
|
|
|
"options" => [ |
98
|
|
|
"field_set_key" => "detailed", |
99
|
|
|
"id" => $pinId, |
100
|
|
|
"pin_id" => $pinId, |
101
|
|
|
"allow_stale" => true, |
102
|
|
|
], |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
return self::createPinRequestData($dataJson); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Creates common pin request data by PinId |
110
|
|
|
* |
111
|
|
|
* @param int $pinId |
112
|
|
|
* @param string $template |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public static function createPinRequest($pinId, $template = 'id') |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
"options" => [ |
119
|
|
|
"$template" => $pinId, |
120
|
|
|
], |
121
|
|
|
"context" => [], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Creates simple Pin request by PinId (used by delete and like requests) |
127
|
|
|
* |
128
|
|
|
* @param int $pinId |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public static function createSimplePinRequest($pinId) |
132
|
|
|
{ |
133
|
|
|
$dataJson = self::createPinRequest($pinId); |
134
|
|
|
return self::createPinRequestData($dataJson); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string|null $sourceUrl |
139
|
|
|
* @param array $data |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public static function createPinRequestData($data, $sourceUrl = null) |
143
|
|
|
{ |
144
|
|
|
if ($sourceUrl === null) { |
145
|
|
|
reset($data); |
146
|
|
|
$sourceUrl = "/pin/" . end($data["options"]) . "/"; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return Request::createRequestData($data, $sourceUrl); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $pinId |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public static function createPinIdRequest($pinId) |
157
|
|
|
{ |
158
|
|
|
return self::createPinRequest($pinId, 'pin_id'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.