| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CybozuHttp\Api\Kintone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CybozuHttp\Api\KintoneApi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use CybozuHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use CybozuHttp\Middleware\JsonStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author ochi51 <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Cursor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public const MAX_GET_RECORDS = 500; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |     public function __construct(Client $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Get all records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * https://developer.cybozu.io/hc/ja/articles/360029152012 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param int $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param int|null $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param array|null $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |     public function all($appId, $query = '', $guestSpaceId = null, $fields = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |         $records = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         $cursorId = $this->create($appId, $query, $guestSpaceId, $fields)['id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |         while(true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |             $result = $this->proceed($cursorId, $guestSpaceId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |             array_push($records, ...$result['records']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             if (!$result['next']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         return $records; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * https://developer.cybozu.io/hc/ja/articles/360029152012#step1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param int $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param int|null $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param array|null $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return array ['id' => $cursorId, 'totalCount' => $totalCount] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |     public function create($appId, $query = '', $guestSpaceId = null, $fields = null): array | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 1 |  |         $options = ['json' => ['app' => $appId, 'query' => $query, 'size' => self::MAX_GET_RECORDS]]; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1 |  |         if ($fields) { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             $options['json']['fields'] = $fields; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         /** @var JsonStream $stream */ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |         $stream = $this->client | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |             ->post(KintoneApi::generateUrl('records/cursor.json', $guestSpaceId), $options) | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  |             ->getBody(); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |         return $stream->jsonSerialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * https://developer.cybozu.io/hc/ja/articles/360029152012#step2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string $cursorId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param int|null $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |     public function proceed(string $cursorId, $guestSpaceId = null): ?array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         $options = ['json' => ['id' => $cursorId]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         /** @var JsonStream $stream */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         $stream = $this->client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             ->get(KintoneApi::generateUrl('records/cursor.json', $guestSpaceId), $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             ->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         return $stream->jsonSerialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * https://developer.cybozu.io/hc/ja/articles/360029152012#step3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param string $cursorId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param int|null $guestSpaceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |     public function delete(string $cursorId, $guestSpaceId = null): ?array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |         $options = ['json' => ['id' => $cursorId]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         /** @var JsonStream $stream */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         $stream = $this->client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |             ->delete(KintoneApi::generateUrl('records/cursor.json', $guestSpaceId), $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |             ->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         return $stream->jsonSerialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |