1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CybozuHttp\Api\User; |
4
|
|
|
|
5
|
|
|
use CybozuHttp\Api\Kintone\File; |
6
|
|
|
use CybozuHttp\Client; |
7
|
|
|
use CybozuHttp\Api\UserApi; |
8
|
|
|
use CybozuHttp\Middleware\JsonStream; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author ochi51 <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Csv |
14
|
|
|
{ |
15
|
|
|
private static $type = [ |
16
|
|
|
'user', |
17
|
|
|
'organization', |
18
|
|
|
'title', |
19
|
|
|
'group', |
20
|
|
|
'userOrganizations', |
21
|
|
|
'userGroups', |
22
|
|
|
'userServices' |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Client |
27
|
|
|
*/ |
28
|
|
|
private $client; |
29
|
|
|
|
30
|
1 |
|
public function __construct(Client $client) |
31
|
|
|
{ |
32
|
1 |
|
$this->client = $client; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get csv file |
37
|
|
|
* |
38
|
|
|
* @param string $type |
39
|
|
|
* @return string |
40
|
|
|
* @throws \InvalidArgumentException |
41
|
|
|
*/ |
42
|
9 |
|
public function get($type): string |
43
|
|
|
{ |
44
|
9 |
|
if (!in_array($type, self::$type, true)) { |
45
|
1 |
|
throw new \InvalidArgumentException('Invalid type parameter'); |
46
|
|
|
} |
47
|
|
|
|
48
|
9 |
|
$content = (string)$this->client |
49
|
9 |
|
->get(UserApi::generateUrl("csv/{$type}.csv")) |
50
|
9 |
|
->getBody(); |
51
|
|
|
|
52
|
9 |
|
return $content; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Post csv file |
57
|
|
|
* |
58
|
|
|
* @param string $type |
59
|
|
|
* @param string $filename |
60
|
|
|
* @return int |
61
|
|
|
* @throws \InvalidArgumentException |
62
|
|
|
*/ |
63
|
9 |
|
public function post($type, $filename): int |
64
|
|
|
{ |
65
|
9 |
|
return $this->postKey($type, $this->file($filename)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Post file key |
70
|
|
|
* |
71
|
|
|
* @param string $type |
72
|
|
|
* @param string $fileKey |
73
|
|
|
* @return int |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
10 |
|
public function postKey($type, $fileKey): int |
77
|
|
|
{ |
78
|
10 |
|
if (!in_array($type, self::$type, true)) { |
79
|
1 |
|
throw new \InvalidArgumentException('Invalid type parameter'); |
80
|
|
|
} |
81
|
|
|
|
82
|
9 |
|
$options = ['json' => ['fileKey' => $fileKey]]; |
83
|
|
|
|
84
|
|
|
/** @var JsonStream $stream */ |
85
|
9 |
|
$stream = $this->client |
86
|
9 |
|
->post(UserApi::generateUrl("csv/{$type}.json"), $options) |
87
|
9 |
|
->getBody(); |
88
|
|
|
|
89
|
9 |
|
return $stream->jsonSerialize()['id']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Post file |
94
|
|
|
* https://cybozudev.zendesk.com/hc/ja/articles/202350470 |
95
|
|
|
* |
96
|
|
|
* @param string $filename |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
9 |
|
public function file($filename): string |
100
|
|
|
{ |
101
|
9 |
|
$options = ['multipart' => [ |
102
|
9 |
|
[ |
103
|
9 |
|
'name' => 'file', |
104
|
9 |
|
'filename' => File::getFilename($filename), |
105
|
9 |
|
'contents' => fopen($filename, 'rb'), |
106
|
9 |
|
'headers' => [ |
107
|
9 |
|
'Content-Type' => mime_content_type($filename) |
108
|
9 |
|
] |
109
|
9 |
|
] |
110
|
9 |
|
]]; |
111
|
|
|
|
112
|
|
|
/** @var JsonStream $stream */ |
113
|
9 |
|
$stream = $this->client |
114
|
9 |
|
->post(UserApi::generateUrl('file.json'), $options) |
115
|
9 |
|
->getBody(); |
116
|
|
|
|
117
|
9 |
|
return $stream->jsonSerialize()['fileKey']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get post csv result |
122
|
|
|
* https://cybozudev.zendesk.com/hc/ja/articles/202361320 |
123
|
|
|
* |
124
|
|
|
* @param int $id |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
9 |
|
public function result($id): array |
128
|
|
|
{ |
129
|
9 |
|
$options = ['query' => ['id' => $id]]; |
130
|
|
|
|
131
|
|
|
/** @var JsonStream $stream */ |
132
|
9 |
|
$stream = $this->client |
133
|
9 |
|
->get(UserApi::generateUrl('csv/result.json'), $options) |
134
|
9 |
|
->getBody(); |
135
|
|
|
|
136
|
9 |
|
return $stream->jsonSerialize(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|