66use Composer \DependencyResolver \Operation \OperationInterface ;
77use Composer \DependencyResolver \Operation \UninstallOperation ;
88use Composer \DependencyResolver \Operation \UpdateOperation ;
9+ use Composer \Package \CompletePackageInterface ;
910use Composer \Package \PackageInterface ;
11+ use IonBazan \ComposerDiff \Url \UrlGenerator ;
1012
1113class DiffEntry
1214{
@@ -25,14 +27,25 @@ class DiffEntry
2527 /** @var string */
2628 private $ type ;
2729
30+ /** @var string|null */
31+ private $ compareUrl ;
32+
33+ /** @var string|null */
34+ private $ projectUrl ;
35+
2836 /**
29- * @param bool $direct
37+ * @param UrlGenerator|null $urlGenerator
38+ * @param bool $direct
3039 */
31- public function __construct (OperationInterface $ operation , $ direct = false )
40+ public function __construct (OperationInterface $ operation , $ urlGenerator = null , $ direct = false )
3241 {
3342 $ this ->operation = $ operation ;
3443 $ this ->direct = $ direct ;
3544 $ this ->type = $ this ->determineType ();
45+
46+ if ($ urlGenerator instanceof UrlGenerator) {
47+ $ this ->setUrls ($ urlGenerator );
48+ }
3649 }
3750
3851 /**
@@ -100,7 +113,15 @@ public function isChange()
100113 }
101114
102115 /**
103- * @return PackageInterface|null
116+ * @return string
117+ */
118+ public function getPackageName ()
119+ {
120+ return $ this ->getPackage ()->getName ();
121+ }
122+
123+ /**
124+ * @return PackageInterface
104125 */
105126 public function getPackage ()
106127 {
@@ -114,9 +135,116 @@ public function getPackage()
114135 return $ operation ->getPackage ();
115136 }
116137
138+ throw new \InvalidArgumentException ('Invalid operation ' );
139+ }
140+
141+ /**
142+ * @return string[]
143+ */
144+ public function getLicenses ()
145+ {
146+ $ package = $ this ->getPackage ();
147+
148+ if (!$ package instanceof CompletePackageInterface) {
149+ return array ();
150+ }
151+
152+ return $ package ->getLicense ();
153+ }
154+
155+ /**
156+ * @return array{
157+ * name: string,
158+ * direct: bool,
159+ * operation: string,
160+ * version_base: string|null,
161+ * version_target: string|null,
162+ * licenses: string[],
163+ * compare: string|null,
164+ * link: string|null,
165+ * }
166+ */
167+ public function toArray ()
168+ {
169+ return array (
170+ 'name ' => $ this ->getPackageName (),
171+ 'direct ' => $ this ->isDirect (),
172+ 'operation ' => $ this ->getType (),
173+ 'version_base ' => $ this ->getBaseVersion (),
174+ 'version_target ' => $ this ->getTargetVersion (),
175+ 'licenses ' => $ this ->getLicenses (),
176+ 'compare ' => $ this ->getUrl (),
177+ 'link ' => $ this ->getProjectUrl (),
178+ );
179+ }
180+
181+ /**
182+ * @return string|null
183+ */
184+ public function getBaseVersion ()
185+ {
186+ if ($ this ->operation instanceof UpdateOperation) {
187+ return $ this ->operation ->getInitialPackage ()->getFullPrettyVersion ();
188+ }
189+
190+ if ($ this ->operation instanceof UninstallOperation) {
191+ return $ this ->operation ->getPackage ()->getFullPrettyVersion ();
192+ }
193+
117194 return null ;
118195 }
119196
197+ /**
198+ * @return string|null
199+ */
200+ public function getTargetVersion ()
201+ {
202+ if ($ this ->operation instanceof UpdateOperation) {
203+ return $ this ->operation ->getTargetPackage ()->getFullPrettyVersion ();
204+ }
205+
206+ if ($ this ->operation instanceof InstallOperation) {
207+ return $ this ->operation ->getPackage ()->getFullPrettyVersion ();
208+ }
209+
210+ return null ;
211+ }
212+
213+ /**
214+ * @return string|null
215+ */
216+ public function getUrl ()
217+ {
218+ return $ this ->compareUrl ;
219+ }
220+
221+ /**
222+ * @return string|null
223+ */
224+ public function getProjectUrl ()
225+ {
226+ return $ this ->projectUrl ;
227+ }
228+
229+ /**
230+ * @return void
231+ */
232+ private function setUrls (UrlGenerator $ generator )
233+ {
234+ $ package = $ this ->getPackage ();
235+ $ this ->projectUrl = $ generator ->getProjectUrl ($ package );
236+
237+ $ operation = $ this ->getOperation ();
238+
239+ if ($ operation instanceof UpdateOperation) {
240+ $ this ->compareUrl = $ generator ->getCompareUrl ($ operation ->getInitialPackage (), $ operation ->getTargetPackage ());
241+
242+ return ;
243+ }
244+
245+ $ this ->compareUrl = $ generator ->getReleaseUrl ($ package );
246+ }
247+
120248 /**
121249 * @return string
122250 */
0 commit comments