| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace ChangelogGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use function array_merge; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use function sprintf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use function str_replace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function urlencode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class ChangelogConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private const DEFAULT_ROOT_GITHUB_URL = 'https://api.github.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $milestone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var string[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $labels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var mixed[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $options = ['rootGitHubUrl' => self::DEFAULT_ROOT_GITHUB_URL]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param string[] $labels | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param mixed[]  $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 29 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         string $user = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         string $repository = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         string $milestone = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         array $labels = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         array $options = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 29 |  |         $this->user       = $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 29 |  |         $this->repository = $repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 29 |  |         $this->milestone  = $milestone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 29 |  |         $this->labels     = $labels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 29 |  |         $this->options    = array_merge($this->options, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 29 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |     public function getUser() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         return $this->user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 4 |  |     public function setUser(string $user) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |         $this->user = $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |     public function getRepository() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         return $this->repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |     public function setRepository(string $repository) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 4 |  |         $this->repository = $repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |     public function getMilestone() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |         return $this->milestone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |     public function setMilestone(string $milestone) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |         $this->milestone = $milestone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |     public function getLabels() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |         return $this->labels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string[] $labels | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |     public function setLabels(array $labels) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |         $this->labels = $labels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return mixed[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |     public function getOptions() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         return $this->options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param mixed[] $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |     public function setOptions(array $options) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  |         $this->options = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 1 |  |     public function getOption(string $name) | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 1 |  |         return $this->options[$name] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |     public function setOption(string $name, $value) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |         $this->options[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 5 |  |     public function getMilestoneIssuesUrl(string $label) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 5 |  |         $query = urlencode(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 5 |  |             'milestone:"%s" repo:%s/%s state:closed%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 5 |  |             str_replace('"', '\"', $this->milestone), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 5 |  |             $this->user, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 5 |  |             $this->repository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 5 |  |             $label !== '' ? ' label:' . $label : '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 5 |  |         return sprintf('%s/search/issues?q=%s', $this->getRootGitHubUrl(), $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 13 |  |     public function isValid() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 13 |  |         return $this->user !== '' && $this->repository !== '' && $this->milestone !== ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 5 |  |     private function getRootGitHubUrl() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 5 |  |         return $this->options['rootGitHubUrl'] ?? self::DEFAULT_ROOT_GITHUB_URL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 161 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |  |