| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace ElevenLabs\Api\Service\Pagination\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ElevenLabs\Api\Definition\ResponseDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ElevenLabs\Api\Service\Pagination\Pagination; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ElevenLabs\Api\Service\Pagination\PaginationLinks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class HalProvider. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class HalProvider implements PaginationProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const DEFAULT_PAGINATION_VALUE = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         // current page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         'page' => '_links.self.href.page', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         // number of items per page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'perPage' => 'itemsPerPage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         // number of items available in total | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'totalPages' => '_links.last.href.page', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         // number of pages available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'totalItems' => 'totalItems', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $paginationName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param array $config | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 11 |  |     public function __construct(array $config = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 11 |  |         foreach (self::DEFAULT_PAGINATION_VALUE as $name => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 11 |  |             if (isset($config[$name])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 11 |  |                 $value = $config[$name]; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 11 |  |             $this->paginationName[$name] = explode('.', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param array              $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param ResponseInterface  $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param ResponseDefinition $responseDefinition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 7 |  |     public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 7 |  |         $totalItems = $this->getValue($data, $this->paginationName['totalItems']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 7 |  |         $perPage = $this->getValue($data, $this->paginationName['perPage']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 7 |  |         foreach ($this->paginationName as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 7 |  |             if ('totalPages' === $key && $totalItems < $perPage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 7 |  |             if (null === $this->getValue($data, $value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 5 |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |         return isset($data['_embedded']['item']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param array              $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param ResponseInterface  $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param ResponseDefinition $responseDefinition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return Pagination | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |     public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition): Pagination | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |         $paginationLinks = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  |         $links = $data['_links']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |         $paginationLinks = new PaginationLinks( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |             $links['first']['href'] ?? $links['self']['href'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  |             $links['last']['href'] ?? $links['self']['href'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 4 |  |             $links['next']['href'] ?? null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 4 |  |             $links['prev']['href'] ?? null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $pagination = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |             'page' => $this->getValue($data, $this->paginationName['page']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 4 |  |             'perPage' => $this->getValue($data, $this->paginationName['perPage']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |             'totalItems' => $this->getValue($data, $this->paginationName['totalItems']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $data = array_map(function ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $relations = array_map(function ($item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 4 |  |                 unset($item['_links']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  |                 return $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 4 |  |             }, $data['_embedded'] ?? []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 4 |  |             unset($data['_links'], $data['_embedded']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 4 |  |             return array_merge($relations, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 4 |  |         }, $data['_embedded']['item']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 4 |  |         return new Pagination( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 4 |  |             $pagination['page'], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 4 |  |             $pagination['perPage'], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 4 |  |             $pagination['totalItems'], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 4 |  |             (int) ceil($pagination['totalItems'] / $pagination['perPage']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 4 |  |             $paginationLinks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param array $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param array $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return array|int|mixed|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 11 |  |     private function getValue(array $items, array $values) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 11 |  |         $value = $items; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 11 |  |         foreach ($values as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 11 |  |             if (isset($value[$item])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 9 |  |                 $value = $value[$item]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 11 |  |             } elseif (is_string($value) && 1 === preg_match('#'.$item.'=([^&]*)#', $value, $tab)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 6 |  |                 $value = isset($tab[1]) ? (int) $tab[1] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 6 |  |             } elseif (is_string($value) && 0 === preg_match('#'.$item.'=([^&]*)#', $value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |                 return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 5 |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 9 |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |  |