-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathPackage.php
More file actions
153 lines (124 loc) · 4.49 KB
/
Copy pathPackage.php
File metadata and controls
153 lines (124 loc) · 4.49 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Tags\Model\Entity\Tag;
/**
* 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 array<\Tags\Model\Entity\Tag> $cake_php_tags
* @property array<int, array<\Tags\Model\Entity\Tag>> $cake_php_tag_groups
* @property array<\Tags\Model\Entity\Tag> $php_tags
* @property array<int, array<\Tags\Model\Entity\Tag>> $php_tag_groups
* @property array<\Tags\Model\Entity\Tagged> $tagged
* @property array<\Tags\Model\Entity\Tag> $tags
* @property \Tags\Model\Entity\Tagged $_joinData
*/
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>
* @see \App\Model\Entity\Package::$cake_php_tags
*/
protected function _getCakePhpTags(): array
{
return $this->extractVersionTags('CakePHP');
}
/**
* @return array<int, array<\Tags\Model\Entity\Tag>>
* @see \App\Model\Entity\Package::$cake_php_tag_groups
*/
protected function _getCakePhpTagGroups(): array
{
return $this->groupVersionTags($this->cake_php_tags, 'CakePHP');
}
/**
* @return array<\Tags\Model\Entity\Tag>
* @see \App\Model\Entity\Package::$php_tags
*/
protected function _getPhpTags(): array
{
return $this->extractVersionTags('PHP');
}
/**
* @return array<int, array<\Tags\Model\Entity\Tag>>
* @see \App\Model\Entity\Package::$php_tag_groups
*/
protected function _getPhpTagGroups(): array
{
return $this->groupVersionTags($this->php_tags, 'PHP');
}
/**
* @return array<\Tags\Model\Entity\Tag>
*/
protected function extractVersionTags(string $prefix): array
{
return array_filter($this->tags, static function (Tag $tag) use ($prefix): bool {
return str_starts_with($tag->label, $prefix . ':');
});
}
/**
* @param array<\Tags\Model\Entity\Tag> $tags
* @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 = intval($matches[1]);
$groups[$majorVersion][] = $tag;
}
uksort($groups, static function (int $left, int $right): int {
return version_compare((string)$right, (string)$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;
}
public const FIELD_ID = 'id';
public const FIELD_PACKAGE = 'package';
public const FIELD_DESCRIPTION = 'description';
public const FIELD_REPO_URL = 'repo_url';
public const FIELD_DOWNLOADS = 'downloads';
public const FIELD_STARS = 'stars';
public const FIELD_LATEST_STABLE_VERSION = 'latest_stable_version';
public const FIELD_LATEST_STABLE_RELEASE_DATE = 'latest_stable_release_date';
public const FIELD_CAKE_PHP_TAGS = 'cake_php_tags';
public const FIELD_CAKE_PHP_TAG_GROUPS = 'cake_php_tag_groups';
public const FIELD_PHP_TAGS = 'php_tags';
public const FIELD_PHP_TAG_GROUPS = 'php_tag_groups';
public const FIELD_TAGGED = 'tagged';
public const FIELD_TAGS = 'tags';
}