1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Traits; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
6
|
|
|
use seregazhuk\PinterestBot\Helpers\Pagination; |
7
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlBuilder; |
8
|
|
|
|
9
|
|
|
trait TryIt |
10
|
|
|
{ |
11
|
|
|
use HandlesRequest, UploadsImages; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Makes a DidIt activity record. |
15
|
|
|
* |
16
|
|
|
* @param string $pinId |
17
|
|
|
* @param string $comment |
18
|
|
|
* @param null|string $pathToImage |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public function tryIt($pinId, $comment, $pathToImage = null) |
22
|
|
|
{ |
23
|
|
|
$data = $this->makeRequest($pinId, $comment, $pathToImage); |
24
|
|
|
|
25
|
|
|
$this->post($data, UrlBuilder::RESOURCE_TRY_PIN_CREATE); |
26
|
|
|
|
27
|
|
|
return $this->getResponse()->getResponseData(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $pinId |
32
|
|
|
* @param string $tryItRecordId |
33
|
|
|
* @param string $comment |
34
|
|
|
* @param string|null $pathToImage |
35
|
|
|
* @return bool|Response |
36
|
|
|
*/ |
37
|
|
|
public function editTryIt($pinId, $tryItRecordId, $comment, $pathToImage = null) |
38
|
|
|
{ |
39
|
|
|
$data = $this->makeRequest($pinId, $comment, $pathToImage); |
40
|
|
|
$data['user_did_it_data_id'] = $tryItRecordId; |
41
|
|
|
|
42
|
|
|
return $this->post($data, UrlBuilder::RESOURCE_TRY_PIN_EDIT); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the pinners who have tied this pin |
47
|
|
|
* |
48
|
|
|
* @param string $pinId |
49
|
|
|
* @param int $limit |
50
|
|
|
* @return Pagination |
51
|
|
|
*/ |
52
|
|
|
public function tried($pinId, $limit = Pagination::DEFAULT_LIMIT) |
53
|
|
|
{ |
54
|
|
|
$data = [ |
55
|
|
|
'field_set_key' => 'did_it', |
56
|
|
|
'show_did_it_feed' => true, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
return $this->getAggregatedActivity($pinId, $data, $limit); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $tryItRecordId |
64
|
|
|
* @return bool|Response |
65
|
|
|
*/ |
66
|
|
|
public function deleteTryIt($tryItRecordId) |
67
|
|
|
{ |
68
|
|
|
return $this->post( |
69
|
|
|
['user_did_it_data_id' => $tryItRecordId], |
70
|
|
|
UrlBuilder::RESOURCE_TRY_PIN_DELETE |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $pinId |
76
|
|
|
* @param string $comment |
77
|
|
|
* @param string|null $pathToImage |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function makeRequest($pinId, $comment, $pathToImage = null) |
81
|
|
|
{ |
82
|
|
|
$data = [ |
83
|
|
|
'pin_id' => $pinId, |
84
|
|
|
'details' => $comment, |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
// If an image was specified try to upload it first to Pinterest simple upload to |
88
|
|
|
// receive and image url. Then we upload it to special DidIt resource to |
89
|
|
|
// get an image signature for the request. |
90
|
|
|
if ($pathToImage) { |
|
|
|
|
91
|
|
|
$request = ['image_url' => $this->upload($pathToImage)]; |
92
|
|
|
|
93
|
|
|
$this->post($request, UrlBuilder::RESOURCE_TRY_PIN_IMAGE_UPLOAD); |
94
|
|
|
|
95
|
|
|
$data['image_signatures'] = $this->getResponse()->getResponseData('image_signature'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $data; |
99
|
|
|
} |
100
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.