|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CybozuHttp\Api\Kintone; |
|
4
|
|
|
|
|
5
|
|
|
use CybozuHttp\Client; |
|
6
|
|
|
use CybozuHttp\Api\KintoneApi; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author ochi51 <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class File |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Client |
|
16
|
|
|
*/ |
|
17
|
|
|
private $client; |
|
18
|
|
|
|
|
19
|
1 |
|
public function __construct(Client $client) |
|
20
|
|
|
{ |
|
21
|
1 |
|
$this->client = $client; |
|
22
|
1 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get file |
|
26
|
|
|
* https://cybozudev.zendesk.com/hc/ja/articles/202166180#step1 |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $fileKey |
|
29
|
|
|
* @param int $guestSpaceId |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function get($fileKey, $guestSpaceId = null) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$options = ['json' => ['fileKey' => $fileKey]]; |
|
35
|
1 |
|
$response = $this->client->get(KintoneApi::generateUrl('file.json', $guestSpaceId), $options); |
|
36
|
|
|
|
|
37
|
1 |
|
return (string)$response->getBody(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Post file |
|
42
|
|
|
* https://cybozudev.zendesk.com/hc/ja/articles/201941824#step1 |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $filename |
|
45
|
|
|
* @param int $guestSpaceId |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function post($filename, $guestSpaceId = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$options = ['multipart' => [ |
|
51
|
|
|
[ |
|
52
|
1 |
|
'name' => 'file', |
|
53
|
1 |
|
'filename' => self::getFilename($filename), |
|
54
|
1 |
|
'contents' => fopen($filename, 'rb'), |
|
55
|
|
|
'headers' => [ |
|
56
|
1 |
|
'Content-Type' => mime_content_type($filename) |
|
57
|
1 |
|
] |
|
58
|
1 |
|
] |
|
59
|
1 |
|
]]; |
|
60
|
|
|
|
|
61
|
1 |
|
return $this->client |
|
|
|
|
|
|
62
|
1 |
|
->post(KintoneApi::generateUrl('file.json', $guestSpaceId), $options) |
|
63
|
1 |
|
->getBody()->jsonSerialize()['fileKey']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns locale independent base name of the given path. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $name The new file name |
|
70
|
|
|
* @return string containing |
|
71
|
|
|
*/ |
|
72
|
10 |
|
public static function getFilename($name) |
|
73
|
|
|
{ |
|
74
|
10 |
|
$originalName = str_replace('\\', '/', $name); |
|
75
|
10 |
|
$pos = strrpos($originalName, '/'); |
|
76
|
10 |
|
$originalName = false === $pos ? $originalName : substr($originalName, $pos + 1); |
|
77
|
|
|
|
|
78
|
10 |
|
return $originalName; |
|
79
|
|
|
} |
|
80
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: