| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Conia\Chuck\Routing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Conia\Chuck\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** @psalm-api */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     use AddsMiddleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected array $attrs = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected string $name = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected string $renderer = 'json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected array $rendererArgs = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @psalm-param class-string $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 11 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         protected readonly RouteAdder $adder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         protected readonly array|string $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         protected readonly string $controller, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         protected readonly string|array $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 11 |  |     public function add(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 11 |  |         if (is_array($this->args)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |             $args = '/' . implode('/', array_map(fn ($arg) => '{' . (string)$arg . '}', $this->args)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 9 |  |             $args = '/{' . $this->args . '}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 11 |  |         if (is_array($this->path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |             assert(is_string($this->path[0])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |             assert(is_string($this->path[1])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |             $plural = $this->path[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |             $singular = $this->path[1] . $args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 10 |  |             $plural = $this->path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 10 |  |             $singular = $this->path . $args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 11 |  |         $this->addRoutes($plural, $singular); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 4 |  |     public function name(string $name): static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 4 |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 4 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 1 |  |     public function render(string $renderer, mixed ...$args): static | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  |         $this->renderer = $renderer; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |         $this->rendererArgs = $args; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |     public function attrs(mixed ...$attrs): static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         $this->attrs = $attrs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 11 |  |     protected function addRoutes( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         string $plural, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         string $singular, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     ): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 11 |  |         $this->addRoute('DELETE', $plural, 'deleteList'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 10 |  |         $this->addRoute('DELETE', $singular, 'delete'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 10 |  |         $this->addRoute('GET', $plural, 'list'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 10 |  |         $this->addRoute('GET', $singular, 'get'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 10 |  |         $this->addRoute('HEAD', $plural, 'headList'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 10 |  |         $this->addRoute('HEAD', $singular, 'head'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 10 |  |         $this->addRoute('OPTIONS', $plural, 'optionsList'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 10 |  |         $this->addRoute('OPTIONS', $singular, 'options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 10 |  |         $this->addRoute('PATCH', $singular, 'patch'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 10 |  |         $this->addRoute('POST', $plural, 'post'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 10 |  |         $this->addRoute('PUT', $singular, 'put'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 11 |  |     protected function addRoute(string $httpMethod, string $path, string $controllerMethod): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 11 |  |         if (method_exists($this->controller, $controllerMethod)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 11 |  |             $name = $this->name ? $this->name . '-' . $controllerMethod : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 11 |  |             $this->adder->addRoute( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 11 |  |                 (new Route($path, [$this->controller, $controllerMethod], $name)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 11 |  |                     ->method($httpMethod) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 11 |  |                     ->middleware(...$this->middleware) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 11 |  |                     ->render($this->renderer, ...$this->rendererArgs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 11 |  |                     ->attrs(...$this->attrs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 11 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |