|
6 | 6 | use Composer\DependencyResolver\Operation\OperationInterface; |
7 | 7 | use Composer\DependencyResolver\Operation\UninstallOperation; |
8 | 8 | use Composer\DependencyResolver\Operation\UpdateOperation; |
| 9 | +use Composer\Package\CompletePackageInterface; |
9 | 10 | use Composer\Package\PackageInterface; |
| 11 | +use IonBazan\ComposerDiff\Url\UrlGenerator; |
10 | 12 |
|
11 | 13 | class DiffEntry |
12 | 14 | { |
@@ -117,6 +119,104 @@ public function getPackage() |
117 | 119 | return null; |
118 | 120 | } |
119 | 121 |
|
| 122 | + /** |
| 123 | + * @return string[] |
| 124 | + */ |
| 125 | + public function getLicenses() |
| 126 | + { |
| 127 | + $package = $this->getPackage(); |
| 128 | + |
| 129 | + if (!$package instanceof CompletePackageInterface) { |
| 130 | + return array(); |
| 131 | + } |
| 132 | + |
| 133 | + return $package->getLicense(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @return array<string, string|bool|null|string[]> |
| 138 | + */ |
| 139 | + public function toArray(UrlGenerator $generator) |
| 140 | + { |
| 141 | + $array = $this->toBaseArray(); |
| 142 | + $array['compare'] = $this->getUrl($generator); |
| 143 | + $array['link'] = $this->getProjectUrl($generator); |
| 144 | + |
| 145 | + return $array; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @return array<string, string|bool|null|string[]> |
| 150 | + */ |
| 151 | + public function toBaseArray() |
| 152 | + { |
| 153 | + $operation = $this->getOperation(); |
| 154 | + |
| 155 | + if ($operation instanceof InstallOperation) { |
| 156 | + return array( |
| 157 | + 'name' => $operation->getPackage()->getName(), |
| 158 | + 'direct' => $this->isDirect(), |
| 159 | + 'operation' => $this->getType(), |
| 160 | + 'version_base' => null, |
| 161 | + 'version_target' => $operation->getPackage()->getFullPrettyVersion(), |
| 162 | + 'licenses' => $this->getLicenses(), |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + if ($operation instanceof UpdateOperation) { |
| 167 | + return array( |
| 168 | + 'name' => $operation->getInitialPackage()->getName(), |
| 169 | + 'direct' => $this->isDirect(), |
| 170 | + 'operation' => $this->getType(), |
| 171 | + 'version_base' => $operation->getInitialPackage()->getFullPrettyVersion(), |
| 172 | + 'version_target' => $operation->getTargetPackage()->getFullPrettyVersion(), |
| 173 | + 'licenses' => $this->getLicenses(), |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + if ($operation instanceof UninstallOperation) { |
| 178 | + return array( |
| 179 | + 'name' => $operation->getPackage()->getName(), |
| 180 | + 'direct' => $this->isDirect(), |
| 181 | + 'operation' => $this->getType(), |
| 182 | + 'version_base' => $operation->getPackage()->getFullPrettyVersion(), |
| 183 | + 'version_target' => null, |
| 184 | + 'licenses' => $this->getLicenses(), |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + throw new \InvalidArgumentException('Invalid operation'); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @return string|null |
| 193 | + */ |
| 194 | + public function getUrl(UrlGenerator $generator) |
| 195 | + { |
| 196 | + $operation = $this->getOperation(); |
| 197 | + |
| 198 | + if ($operation instanceof UpdateOperation) { |
| 199 | + return $generator->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage()); |
| 200 | + } |
| 201 | + |
| 202 | + if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) { |
| 203 | + return $generator->getReleaseUrl($operation->getPackage()); |
| 204 | + } |
| 205 | + |
| 206 | + return null; |
| 207 | + } |
| 208 | + |
| 209 | + public function getProjectUrl(UrlGenerator $generator) |
| 210 | + { |
| 211 | + $package = $this->getPackage(); |
| 212 | + |
| 213 | + if (!isset($package)) { |
| 214 | + return null; |
| 215 | + } |
| 216 | + |
| 217 | + return $generator->getProjectUrl($package); |
| 218 | + } |
| 219 | + |
120 | 220 | /** |
121 | 221 | * @return string |
122 | 222 | */ |
|
0 commit comments