1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class References |
9
|
|
|
* |
10
|
|
|
* Utility class for pipeline references (ids) definition and validation |
11
|
|
|
* |
12
|
|
|
* pipeline : that what can be referenced (id): default, branches/**, custom/foo etc. |
13
|
|
|
* section : branches, tags, bookmarks, pull-requests, custom |
14
|
|
|
* pattern section : a section that can match by pattern to some input (all sections but custom) |
15
|
|
|
* segments : constants for reference segments, default for the default pipeline and custom |
16
|
|
|
* for custom pipelines. they come with no mapping to any vcs nomenclature. |
17
|
|
|
* |
18
|
|
|
* @package Ktomk\Pipelines\File |
19
|
|
|
*/ |
20
|
|
|
class ReferenceTypes |
21
|
|
|
{ |
22
|
|
|
const REF_BRANCHES = 'branches'; |
23
|
|
|
const REF_TAGS = 'tags'; |
24
|
|
|
const REF_BOOKMARKS = 'bookmarks'; |
25
|
|
|
const REF_PULL_REQUESTS = 'pull-requests'; |
26
|
|
|
|
27
|
|
|
const SEG_DEFAULT = 'default'; |
28
|
|
|
const SEG_CUSTOM = 'custom'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* pipelines sections on first level that contain pipelines on second level |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private static $sections = array( |
36
|
|
|
self::REF_BRANCHES, self::REF_TAGS, self::REF_BOOKMARKS, self::REF_PULL_REQUESTS, self::SEG_CUSTOM, |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $id |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
7 |
|
public static function isValidId($id) |
45
|
|
|
{ |
46
|
7 |
|
return (bool)preg_match( |
47
|
7 |
|
'~^(' . self::SEG_DEFAULT . '|(' . implode('|', self::$sections) . ')/[^\x00-\x1F\x7F-\xFF]*)$~', |
48
|
|
|
$id |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* a section that contains pipelines that can match by pattern |
54
|
|
|
* |
55
|
|
|
* @param null|int|string $section |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
* |
59
|
|
|
* @psalm-assert-if-true string $section |
60
|
|
|
*/ |
61
|
9 |
|
public static function isPatternSection($section) |
62
|
|
|
{ |
63
|
9 |
|
return is_string($section) && \in_array($section, array_slice(self::$sections, 0, 4), true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param null|int|string $section |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
9 |
|
public static function isSection($section) |
72
|
|
|
{ |
73
|
9 |
|
return is_string($section) && in_array($section, self::$sections, true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* get sections |
78
|
|
|
* |
79
|
|
|
* @return array|string[] |
80
|
|
|
*/ |
81
|
1 |
|
public static function getSections() |
82
|
|
|
{ |
83
|
1 |
|
return self::$sections; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|