|
8 | 8 | use Composer\Pcre\Preg; |
9 | 9 | use Doctrine\Common\Collections\ArrayCollection; |
10 | 10 | use Doctrine\Common\Collections\Collection; |
| 11 | +use Doctrine\Common\Collections\Criteria; |
| 12 | +use Doctrine\Common\Collections\Order; |
| 13 | +use Doctrine\Common\Collections\Selectable; |
11 | 14 | use Doctrine\DBAL\Types\Types; |
12 | 15 | use Doctrine\ORM\Mapping as ORM; |
13 | 16 |
|
@@ -98,51 +101,45 @@ class Metadata extends TrackedEntity implements \Stringable |
98 | 101 | private Package $package; |
99 | 102 |
|
100 | 103 | /** |
101 | | - * @var Collection<int, MetadataRequireLink> |
| 104 | + * @var Collection<int, MetadataRequireLink>&Selectable |
102 | 105 | */ |
103 | 106 | #[ORM\OneToMany(targetEntity: MetadataRequireLink::class, mappedBy: 'metadata', cascade: ['persist', 'detach'])] |
104 | | - #[ORM\OrderBy(['index' => 'ASC'])] |
105 | 107 | private Collection $requireLinks; |
106 | 108 |
|
107 | 109 | /** |
108 | | - * @var Collection<int, MetadataDevRequireLink> |
| 110 | + * @var Collection<int, MetadataDevRequireLink>&Selectable |
109 | 111 | */ |
110 | 112 | #[ORM\OneToMany(targetEntity: MetadataDevRequireLink::class, mappedBy: 'metadata', cascade: ['persist', 'detach'])] |
111 | | - #[ORM\OrderBy(['index' => 'ASC'])] |
112 | 113 | private Collection $devRequireLinks; |
113 | 114 |
|
114 | 115 | /** |
115 | | - * @var Collection<int, MetadataConflictLink> |
| 116 | + * @var Collection<int, MetadataConflictLink>&Selectable |
116 | 117 | */ |
117 | 118 | #[ORM\OneToMany(targetEntity: MetadataConflictLink::class, mappedBy: 'metadata', cascade: ['persist', 'detach'])] |
118 | | - #[ORM\OrderBy(['index' => 'ASC'])] |
119 | 119 | private Collection $conflictLinks; |
120 | 120 |
|
121 | 121 | /** |
122 | | - * @var Collection<int, MetadataProvideLink> |
| 122 | + * @var Collection<int, MetadataProvideLink>&Selectable |
123 | 123 | */ |
124 | 124 | #[ORM\OneToMany(targetEntity: MetadataProvideLink::class, mappedBy: 'metadata', cascade: ['persist', 'detach'])] |
125 | | - #[ORM\OrderBy(['index' => 'ASC'])] |
126 | 125 | private Collection $provideLinks; |
127 | 126 |
|
128 | 127 | /** |
129 | | - * @var Collection<int, MetadataReplaceLink> |
| 128 | + * @var Collection<int, MetadataReplaceLink>&Selectable |
130 | 129 | */ |
131 | 130 | #[ORM\OneToMany(targetEntity: MetadataReplaceLink::class, mappedBy: 'metadata', cascade: ['persist', 'detach'])] |
132 | | - #[ORM\OrderBy(['index' => 'ASC'])] |
133 | 131 | private Collection $replaceLinks; |
134 | 132 |
|
135 | 133 | /** |
136 | | - * @var Collection<int, MetadataSuggestLink> |
| 134 | + * @var Collection<int, MetadataSuggestLink>&Selectable |
137 | 135 | */ |
138 | 136 | #[ORM\OneToMany(targetEntity: MetadataSuggestLink::class, mappedBy: 'metadata', cascade: ['persist', 'detach'])] |
139 | | - #[ORM\OrderBy(['index' => 'ASC'])] |
140 | 137 | private Collection $suggestLinks; |
141 | 138 |
|
142 | 139 | /** |
143 | | - * @var Collection<int, MetadataKeyword> |
| 140 | + * @var Collection<int, MetadataKeyword>&Selectable |
144 | 141 | */ |
145 | | - #[ORM\OneToMany(mappedBy: 'metadata', targetEntity: MetadataKeyword::class, cascade: ['persist', 'detach'])] |
| 142 | + #[ORM\OneToMany(targetEntity: MetadataKeyword::class, mappedBy: 'metadata', cascade: ['persist', 'detach'])] |
146 | 143 | private Collection $keywords; |
147 | 144 |
|
148 | 145 | public function __construct(Version $version) |
@@ -416,57 +413,57 @@ public function getPackage(): Package |
416 | 413 | /** |
417 | 414 | * @return Collection<int, MetadataRequireLink> |
418 | 415 | */ |
419 | | - public function getRequireLinks(): Collection |
| 416 | + public function getRequireLinks(bool $raw = false): Collection |
420 | 417 | { |
421 | | - return $this->requireLinks; |
| 418 | + return self::getOrderedCollection($this->requireLinks, $raw); |
422 | 419 | } |
423 | 420 |
|
424 | 421 | /** |
425 | 422 | * @return Collection<int, MetadataDevRequireLink> |
426 | 423 | */ |
427 | | - public function getDevRequireLinks(): Collection |
| 424 | + public function getDevRequireLinks(bool $raw = false): Collection |
428 | 425 | { |
429 | | - return $this->devRequireLinks; |
| 426 | + return self::getOrderedCollection($this->devRequireLinks, $raw); |
430 | 427 | } |
431 | 428 |
|
432 | 429 | /** |
433 | 430 | * @return Collection<int, MetadataConflictLink> |
434 | 431 | */ |
435 | | - public function getConflictLinks(): Collection |
| 432 | + public function getConflictLinks(bool $raw = false): Collection |
436 | 433 | { |
437 | | - return $this->conflictLinks; |
| 434 | + return self::getOrderedCollection($this->conflictLinks, $raw); |
438 | 435 | } |
439 | 436 |
|
440 | 437 | /** |
441 | 438 | * @return Collection<int, MetadataProvideLink> |
442 | 439 | */ |
443 | | - public function getProvideLinks(): Collection |
| 440 | + public function getProvideLinks(bool $raw = false): Collection |
444 | 441 | { |
445 | | - return $this->provideLinks; |
| 442 | + return self::getOrderedCollection($this->provideLinks, $raw); |
446 | 443 | } |
447 | 444 |
|
448 | 445 | /** |
449 | 446 | * @return Collection<int, MetadataReplaceLink> |
450 | 447 | */ |
451 | | - public function getReplaceLinks(): Collection |
| 448 | + public function getReplaceLinks(bool $raw = false): Collection |
452 | 449 | { |
453 | | - return $this->replaceLinks; |
| 450 | + return self::getOrderedCollection($this->replaceLinks, $raw); |
454 | 451 | } |
455 | 452 |
|
456 | 453 | /** |
457 | 454 | * @return Collection<int, MetadataSuggestLink> |
458 | 455 | */ |
459 | | - public function getSuggestLinks(): Collection |
| 456 | + public function getSuggestLinks(bool $raw = false): Collection |
460 | 457 | { |
461 | | - return $this->suggestLinks; |
| 458 | + return self::getOrderedCollection($this->suggestLinks, $raw); |
462 | 459 | } |
463 | 460 |
|
464 | 461 | /** |
465 | 462 | * @return Collection<int, MetadataKeyword> |
466 | 463 | */ |
467 | | - public function getKeywords(): Collection |
| 464 | + public function getKeywords(bool $raw = false): Collection |
468 | 465 | { |
469 | | - return $this->keywords; |
| 466 | + return self::getOrderedCollection($this->keywords, $raw); |
470 | 467 | } |
471 | 468 |
|
472 | 469 | public function hasSource(): bool |
@@ -675,4 +672,22 @@ public function toComposerArray(): array |
675 | 672 |
|
676 | 673 | return $data; |
677 | 674 | } |
| 675 | + |
| 676 | + /** |
| 677 | + * Sorts a Doctrine collection by an index field. If the $raw parameter is true, the collection is returned as-is. |
| 678 | + */ |
| 679 | + private static function getOrderedCollection(Collection&Selectable $collection, bool $raw): Collection |
| 680 | + { |
| 681 | + if ($raw) { |
| 682 | + return $collection; |
| 683 | + } |
| 684 | + |
| 685 | + static $criteria = Criteria::create() |
| 686 | + ->orderBy(['index' => Order::Ascending]); |
| 687 | + |
| 688 | + /** @var Collection $collection */ |
| 689 | + $collection = $collection->matching($criteria); |
| 690 | + |
| 691 | + return $collection; |
| 692 | + } |
678 | 693 | } |
0 commit comments