| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CybozuHttp\Api\Kintone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use CybozuHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use CybozuHttp\Api\KintoneApi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author ochi51 <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class Records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const MAX_GET_RECORDS = 500; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const MAX_POST_RECORDS = 100; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |     public function __construct(Client $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Get records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * https://cybozudev.zendesk.com/hc/ja/articles/202331474#step2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param integer $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param integer $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param boolean $totalCount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param array|null $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 3 |  |     public function get($appId, $query = '', $guestSpaceId = null, $totalCount = true, array $fields = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 3 |  |         $options = ['json' => ['app' => $appId, 'query' => $query]]; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 3 |  |         if ($totalCount) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 3 |  |             $options['json']['totalCount'] = $totalCount; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 3 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 3 |  |         if ($fields) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 2 |  |             $options['json']['fields'] = $fields; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 3 |  |         return $this->client | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 3 |  |             ->get(KintoneApi::generateUrl('records.json', $guestSpaceId), $options) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 3 |  |             ->getBody()->jsonSerialize(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Get all records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param integer $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param integer $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param array|null $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |     public function all($appId, $query = '', $guestSpaceId = null, array $fields = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         $result[0] = $this->get($appId, $query . ' limit ' . self::MAX_GET_RECORDS, $guestSpaceId, true, $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         $totalCount = $result[0]['totalCount']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         if ($totalCount <= self::MAX_GET_RECORDS) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             return $result[0]['records']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         $concurrency = $this->client->getConfig('concurrency'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         $requests = $this->createGetRequestsCallback($appId, $query, $guestSpaceId, $fields, $totalCount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         $pool = new Pool($this->client, $requests(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             'concurrency' => $concurrency ?: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'fulfilled' => function (ResponseInterface $response, $index) use (&$result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |                 $result[$index+1] = array_merge($response->getBody()->jsonSerialize()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         $pool->promise()->wait(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         ksort($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         $allRecords = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         foreach ($result as $r) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             /** @var array $records */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             $records = $r['records']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             foreach ($records as $record) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |                 $allRecords[] = $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         return $allRecords; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param integer $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param integer $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param array|null $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param integer $totalCount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |     private function createGetRequestsCallback($appId, $query, $guestSpaceId, $fields, $totalCount) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |         $headers = $this->client->getConfig('headers'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         $headers['Content-Type'] = 'application/json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         return function () use ($appId, $query, $guestSpaceId, $fields, $totalCount, $headers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |             $num = ceil($totalCount / self::MAX_GET_RECORDS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |             for ($i = 1; $i < $num; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $body = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |                     'app' => $appId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |                     'query' => $query . ' limit ' . self::MAX_GET_RECORDS . ' offset ' . $i * self::MAX_GET_RECORDS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |                 if ($fields) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |                     $body['fields'] = $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |                 yield new Request( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |                     'GET', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |                     KintoneApi::generateUrl('records.json', $guestSpaceId), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |                     $headers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |                     \GuzzleHttp\json_encode($body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * Post records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * https://cybozudev.zendesk.com/hc/ja/articles/202166160#step2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param integer $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param array $records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param integer $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 2 |  |     public function post($appId, array $records, $guestSpaceId = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 2 |  |         $options = ['json' => ['app' => $appId, 'records' => $records]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 2 |  |         return $this->client | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 2 |  |             ->post(KintoneApi::generateUrl('records.json', $guestSpaceId), $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  |             ->getBody()->jsonSerialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * Put records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * https://cybozudev.zendesk.com/hc/ja/articles/201941784#step2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param integer $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param array $records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param integer $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |     public function put($appId, array $records, $guestSpaceId = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |         $options = ['json' => ['app' => $appId, 'records' => $records]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 1 |  |         return $this->client | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 1 |  |             ->put(KintoneApi::generateUrl('records.json', $guestSpaceId), $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 1 |  |             ->getBody()->jsonSerialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * Delete records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * https://cybozudev.zendesk.com/hc/ja/articles/201941794 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param integer $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @param array $ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param integer $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param array $revisions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 1 |  |     public function delete($appId, array $ids, $guestSpaceId = null, array $revisions = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 1 |  |         $options = ['json' => ['app' => $appId, 'ids' => $ids]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 1 |  |         if (count($revisions) && count($ids) === count($revisions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             $options['json']['revisions'] = $revisions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 1 |  |         return $this->client | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 1 |  |             ->delete(KintoneApi::generateUrl('records.json', $guestSpaceId), $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 1 |  |             ->getBody()->jsonSerialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * Put records status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * https://cybozudev.zendesk.com/hc/ja/articles/204791550#anchor_changeRecordStatusBulk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @param integer $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @param array $records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @param integer $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 1 |  |     public function putStatus($appId, array $records, $guestSpaceId = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 1 |  |         $options = ['json' => ['app' => $appId, 'records' => $records]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 1 |  |         return $this->client | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 1 |  |             ->put(KintoneApi::generateUrl('records/status.json', $guestSpaceId), $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             ->getBody()->jsonSerialize(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 202 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  | } | 
            
                        
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: