-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathPackage.php
More file actions
121 lines (106 loc) · 3.44 KB
/
Package.php
File metadata and controls
121 lines (106 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* Package Entity
*
* @property int $id
* @property string $package
* @property string $description
* @property string $repo_url
* @property int $downloads
* @property int $stars
* @property string|null $latest_stable_version
* @property \Cake\I18n\Date|null $latest_stable_release_date
*
* @property \Tags\Model\Entity\Tag[] $tags
*
* @property \Tags\Model\Entity\Tag[] $cake_php_tags
* @property array<int, array<\Tags\Model\Entity\Tag>> $cake_php_tag_groups
* @property \Tags\Model\Entity\Tag[] $php_tags
* @property array<int, array<\Tags\Model\Entity\Tag>> $php_tag_groups
*/
class Package extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array<string, bool>
*/
protected array $_accessible = [
'id' => false,
'*' => true,
];
/**
* @return array<\Tags\Model\Entity\Tag>
*/
protected function _getCakePhpTags(): array
{
return $this->extractVersionTags('CakePHP');
}
/**
* @return array<int, array<\Tags\Model\Entity\Tag>>
*/
protected function _getCakePhpTagGroups(): array
{
return $this->groupVersionTags($this->cake_php_tags, 'CakePHP');
}
/**
* @return array<\Tags\Model\Entity\Tag>
*/
protected function _getPhpTags(): array
{
return $this->extractVersionTags('PHP');
}
/**
* @return array<int, array<\Tags\Model\Entity\Tag>>
*/
protected function _getPhpTagGroups(): array
{
return $this->groupVersionTags($this->php_tags, 'PHP');
}
/**
* @param string $prefix
* @return array<\Tags\Model\Entity\Tag>
*/
protected function extractVersionTags(string $prefix): array
{
return array_filter($this->tags, static function ($tag) use ($prefix) {
return str_starts_with($tag->label, $prefix . ':');
});
}
/**
* @param array<\Tags\Model\Entity\Tag> $tags
* @param string $prefix
* @return array<int, array<\Tags\Model\Entity\Tag>>
*/
protected function groupVersionTags(array $tags, string $prefix): array
{
$groups = [];
$quotedPrefix = preg_quote($prefix, '/');
foreach ($tags as $tag) {
if (!preg_match('/^' . $quotedPrefix . ':\s*(\d+)(?:\.\d+)?$/', $tag->label, $matches)) {
continue;
}
$majorVersion = $matches[1];
$groups[$majorVersion][] = $tag;
}
uksort($groups, static function (string $left, string $right): int {
return version_compare($right, $left);
});
foreach ($groups as &$groupedTags) {
usort($groupedTags, static function ($left, $right) use ($prefix): int {
$leftVersion = preg_replace('/^' . preg_quote($prefix, '/') . ':\s*/', '', $left->label) ?: $left->label;
$rightVersion = preg_replace('/^' . preg_quote($prefix, '/') . ':\s*/', '', $right->label) ?: $right->label;
return version_compare($rightVersion, $leftVersion);
});
}
unset($groupedTags);
return $groups;
}
}