1 | <?php |
||||||
2 | |||||||
3 | namespace AlibabaCloud\ImageSearch\V20180120\Traits; |
||||||
4 | |||||||
5 | /** |
||||||
6 | * Trait AddItemTrait |
||||||
7 | * |
||||||
8 | * @package AlibabaCloud\ImageSearch\V20180120\Traits |
||||||
9 | */ |
||||||
10 | trait AddItemTrait |
||||||
11 | { |
||||||
12 | /** |
||||||
13 | * @var string |
||||||
14 | */ |
||||||
15 | private $itemId; |
||||||
16 | |||||||
17 | /** |
||||||
18 | * @var string |
||||||
19 | */ |
||||||
20 | private $cateId; |
||||||
21 | |||||||
22 | /** |
||||||
23 | * @var string |
||||||
24 | */ |
||||||
25 | private $custContent; |
||||||
26 | |||||||
27 | /** |
||||||
28 | * @var array |
||||||
29 | */ |
||||||
30 | private $picMap = []; |
||||||
31 | |||||||
32 | /** |
||||||
33 | * @param $picName |
||||||
34 | * @param $picContent |
||||||
35 | * |
||||||
36 | * @return $this |
||||||
37 | */ |
||||||
38 | 1 | public function addPicture($picName, $picContent) |
|||||
39 | { |
||||||
40 | 1 | $encodePicName = base64_encode($picName); |
|||||
41 | 1 | $encodePicContent = base64_encode($picContent); |
|||||
42 | 1 | $this->picMap[$encodePicName] = $encodePicContent; |
|||||
43 | 1 | $this->buildPostContent(); |
|||||
44 | |||||||
45 | 1 | return $this; |
|||||
46 | } |
||||||
47 | |||||||
48 | /** |
||||||
49 | * @param mixed $itemId |
||||||
50 | * |
||||||
51 | * @return $this |
||||||
52 | */ |
||||||
53 | 1 | public function withItemId($itemId) |
|||||
54 | { |
||||||
55 | 1 | $this->itemId = $itemId; |
|||||
56 | 1 | $this->buildPostContent(); |
|||||
57 | |||||||
58 | 1 | return $this; |
|||||
59 | } |
||||||
60 | |||||||
61 | /** |
||||||
62 | * @param mixed $cateId |
||||||
63 | * |
||||||
64 | * @return $this |
||||||
65 | */ |
||||||
66 | 1 | public function withCateId($cateId) |
|||||
67 | { |
||||||
68 | 1 | $this->cateId = $cateId; |
|||||
69 | 1 | $this->buildPostContent(); |
|||||
70 | |||||||
71 | 1 | return $this; |
|||||
72 | } |
||||||
73 | |||||||
74 | /** |
||||||
75 | * @param mixed $custContent |
||||||
76 | * |
||||||
77 | * @return $this |
||||||
78 | */ |
||||||
79 | 1 | public function withCustContent($custContent) |
|||||
80 | { |
||||||
81 | 1 | $this->custContent = $custContent; |
|||||
82 | 1 | $this->buildPostContent(); |
|||||
83 | |||||||
84 | 1 | return $this; |
|||||
85 | } |
||||||
86 | |||||||
87 | /** |
||||||
88 | * @return bool |
||||||
89 | */ |
||||||
90 | 1 | private function buildPostContent() |
|||||
91 | { |
||||||
92 | 1 | $map = []; |
|||||
93 | |||||||
94 | 1 | if ($this->itemId === null || $this->itemId === '' |
|||||
95 | 1 | || $this->cateId === null |
|||||
96 | 1 | || $this->cateId === '' |
|||||
97 | 1 | || empty($this->picMap)) { |
|||||
98 | 1 | return false; |
|||||
99 | } |
||||||
100 | |||||||
101 | 1 | $map['item_id'] = $this->itemId; |
|||||
102 | 1 | $map['cat_id'] = $this->cateId; |
|||||
103 | 1 | $map['cust_content'] = $this->custContent; |
|||||
104 | |||||||
105 | 1 | $picListStr = ''; |
|||||
106 | 1 | foreach ($this->picMap as $key => $value) { |
|||||
107 | 1 | if ($key === null || $key === '' |
|||||
108 | 1 | || $value === null |
|||||
109 | 1 | || $value === '') { |
|||||
110 | return false; |
||||||
111 | } |
||||||
112 | 1 | $picListStr .= $key . ','; |
|||||
113 | 1 | $map[$key] = $value; |
|||||
114 | 1 | } |
|||||
115 | |||||||
116 | 1 | $map['pic_list'] = substr($picListStr, 0, -1); |
|||||
117 | |||||||
118 | 1 | $this->body($this->buildContent($map)); |
|||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
119 | 1 | $this->format('JSON'); |
|||||
0 ignored issues
–
show
It seems like
format() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
120 | |||||||
121 | 1 | return true; |
|||||
122 | } |
||||||
123 | |||||||
124 | /** |
||||||
125 | * @param $map |
||||||
126 | * |
||||||
127 | * @return string |
||||||
128 | */ |
||||||
129 | 1 | private function buildContent($map) |
|||||
130 | { |
||||||
131 | 1 | $meta = ''; |
|||||
132 | 1 | $body = ''; |
|||||
133 | 1 | $start = 0; |
|||||
134 | |||||||
135 | 1 | foreach ($map as $key => $value) { |
|||||
136 | 1 | if ($meta !== '') { |
|||||
137 | 1 | $meta .= '#'; |
|||||
138 | 1 | } |
|||||
139 | 1 | $meta .= $key . ',' . $start . ',' . ($start + strlen($value)); |
|||||
140 | 1 | $body .= $value; |
|||||
141 | 1 | $start += strlen($value); |
|||||
142 | 1 | } |
|||||
143 | |||||||
144 | 1 | return $meta . '^' . $body; |
|||||
145 | } |
||||||
146 | } |
||||||
147 |