Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/util/AlmanacNames.php
12256 views
1
<?php
2
3
final class AlmanacNames extends Phobject {
4
5
public static function validateName($name) {
6
if (strlen($name) < 3) {
7
throw new Exception(
8
pht(
9
'Almanac service, device, property, network and namespace names '.
10
'must be at least 3 characters long.'));
11
}
12
13
if (strlen($name) > 100) {
14
throw new Exception(
15
pht(
16
'Almanac service, device, property, network and namespace names '.
17
'may not be more than 100 characters long.'));
18
}
19
20
if (!preg_match('/^[a-z0-9.-]+\z/', $name)) {
21
throw new Exception(
22
pht(
23
'Almanac service, device, property, network and namespace names '.
24
'may only contain lowercase letters, numbers, hyphens, and '.
25
'periods.'));
26
}
27
28
if (preg_match('/(^|\\.)\d+(\z|\\.)/', $name)) {
29
throw new Exception(
30
pht(
31
'Almanac service, device, network, property and namespace names '.
32
'may not have any segments containing only digits.'));
33
}
34
35
if (preg_match('/\.\./', $name)) {
36
throw new Exception(
37
pht(
38
'Almanac service, device, property, network and namespace names '.
39
'may not contain multiple consecutive periods.'));
40
}
41
42
if (preg_match('/\\.-|-\\./', $name)) {
43
throw new Exception(
44
pht(
45
'Almanac service, device, property, network and namespace names '.
46
'may not contain hyphens adjacent to periods.'));
47
}
48
49
if (preg_match('/--/', $name)) {
50
throw new Exception(
51
pht(
52
'Almanac service, device, property, network and namespace names '.
53
'may not contain multiple consecutive hyphens.'));
54
}
55
56
if (!preg_match('/^[a-z0-9].*[a-z0-9]\z/', $name)) {
57
throw new Exception(
58
pht(
59
'Almanac service, device, property, network and namespace names '.
60
'must begin and end with a letter or number.'));
61
}
62
}
63
64
}
65
66