1 | <?php |
||
17 | class File |
||
18 | { |
||
19 | /** |
||
20 | * @var Client |
||
21 | */ |
||
22 | private $client; |
||
23 | |||
24 | 1 | public function __construct(Client $client) |
|
28 | |||
29 | /** |
||
30 | * Get file |
||
31 | * https://cybozudev.zendesk.com/hc/ja/articles/202166180#step1 |
||
32 | * |
||
33 | * @param string $fileKey |
||
34 | * @param int $guestSpaceId |
||
35 | * @return string |
||
36 | */ |
||
37 | 1 | public function get($fileKey, $guestSpaceId = null) |
|
44 | |||
45 | /** |
||
46 | * @param array $fileKeys |
||
47 | * @param int|null $guestSpaceId |
||
48 | * @return array |
||
49 | */ |
||
50 | 1 | public function multiGet(array $fileKeys, $guestSpaceId = null) |
|
51 | { |
||
52 | 1 | $result = []; |
|
53 | 1 | $concurrency = $this->client->getConfig('concurrency'); |
|
54 | 1 | $headers = $this->client->getConfig('headers'); |
|
55 | 1 | $headers['Content-Type'] = 'application/json'; |
|
56 | 1 | $url = KintoneApi::generateUrl('file.json', $guestSpaceId); |
|
57 | $requests = function () use ($fileKeys, $url, $headers) { |
||
58 | 1 | foreach ($fileKeys as $fileKey) { |
|
59 | 1 | $body = \GuzzleHttp\json_encode(['fileKey' => $fileKey]); |
|
60 | 1 | yield new Request('GET', $url, $headers, $body); |
|
61 | 1 | } |
|
62 | 1 | }; |
|
63 | 1 | $pool = new Pool($this->client, $requests(), [ |
|
64 | 1 | 'concurrency' => $concurrency ?: 1, |
|
65 | 'fulfilled' => function (ResponseInterface $response, $index) use (&$result) { |
||
66 | 1 | $result[$index] = (string)$response->getBody(); |
|
67 | 1 | } |
|
68 | 1 | ]); |
|
69 | 1 | $pool->promise()->wait(); |
|
70 | |||
71 | 1 | return $result; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Post file |
||
76 | * https://cybozudev.zendesk.com/hc/ja/articles/201941824#step1 |
||
77 | * |
||
78 | * @param string $filename |
||
79 | * @param int $guestSpaceId |
||
80 | * @return string |
||
81 | */ |
||
82 | 1 | public function post($filename, $guestSpaceId = null) |
|
83 | { |
||
84 | 1 | $options = ['multipart' => [self::createMultipart($filename)]]; |
|
85 | 1 | $this->changeLocale(); |
|
86 | |||
87 | /** @var JsonStream $stream */ |
||
88 | 1 | $stream = $this->client |
|
89 | 1 | ->post(KintoneApi::generateUrl('file.json', $guestSpaceId), $options) |
|
90 | 1 | ->getBody(); |
|
91 | |||
92 | 1 | return $stream->jsonSerialize()['fileKey']; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param array $fileNames |
||
97 | * @param int|null $guestSpaceId |
||
98 | * @return array |
||
99 | * @throws \InvalidArgumentException |
||
100 | */ |
||
101 | 1 | public function multiPost(array $fileNames, $guestSpaceId = null) |
|
102 | { |
||
103 | 1 | $this->changeLocale(); |
|
104 | |||
105 | 1 | $result = []; |
|
106 | 1 | $concurrency = $this->client->getConfig('concurrency'); |
|
107 | 1 | $headers = $this->client->getConfig('headers'); |
|
108 | 1 | $url = KintoneApi::generateUrl('file.json', $guestSpaceId); |
|
109 | $requests = function () use ($fileNames, $url, $headers) { |
||
110 | 1 | foreach ($fileNames as $filename) { |
|
111 | 1 | $body = new MultipartStream([self::createMultipart($filename)]); |
|
112 | 1 | yield new Request('POST', $url, $headers, $body); |
|
113 | 1 | } |
|
114 | 1 | }; |
|
115 | 1 | $pool = new Pool($this->client, $requests(), [ |
|
116 | 1 | 'concurrency' => $concurrency ?: 1, |
|
117 | 1 | 'fulfilled' => function (ResponseInterface $response, $index) use (&$result) { |
|
118 | /** @var JsonStream $stream */ |
||
119 | 1 | $stream = $response->getBody(); |
|
120 | 1 | $result[$index] = $stream->jsonSerialize()['fileKey']; |
|
121 | 1 | } |
|
122 | 1 | ]); |
|
123 | 1 | $pool->promise()->wait(); |
|
124 | 1 | ksort($result); |
|
125 | |||
126 | 1 | return $result; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Returns locale independent base name of the given path. |
||
131 | * |
||
132 | * @param string $name The new file name |
||
133 | * @return string containing |
||
134 | */ |
||
135 | 11 | public static function getFilename($name) |
|
143 | |||
144 | 2 | private function changeLocale() |
|
145 | { |
||
146 | 2 | $baseUri = $this->client->getConfig('base_uri'); |
|
151 | |||
152 | /** |
||
153 | * @param string $filename |
||
154 | * @return array |
||
155 | */ |
||
156 | 2 | private static function createMultipart($filename) |
|
165 | } |