a9f9fdab9bdc4ef5d47f59ffb3c366e0d9f1568b
[civicrm-core.git] /
1 <?php
2
3 define(CCRM_EXTENSIONVALIDATION_PAD, 7);
4
5 /**
6 * Determine if a version is valid
7 *
8 * @return bool
9 */
10 function ccrm_extensionvalidation_version_isValid($ver) {
11 $parts = ccrm_extensionvalidation_version_split($ver);
12 foreach ($parts as $part) {
13 if (!preg_match('/^(z[0-9]+|pre[0-9]+)$/', $part)) {
14 return FALSE;
15 }
16 }
17 return TRUE;
18 }
19
20 /**
21 * Normalize version, producing a code that can be correclty
22 * sorted in SQL.
23 *
24 * @return string
25 */
26 function ccrm_extensionvalidation_version_normalize($ver) {
27 return implode('-', ccrm_extensionvalidation_version_split($ver));
28 }
29
30
31 /**
32 * Clean version number:
33 * - Use consistent delimiter
34 * - Change revision #s (r456) to straight numbers (456)
35 * - Explode into numeric and alphabetic parts
36 * - Make numerals higher than alphas (1.2.0 > 1.2.alpha)
37 *
38 * @return array of version parts
39 */
40 function ccrm_extensionvalidation_version_split($ver, $pad = CCRM_EXTENSIONVALIDATION_PAD) {
41 $ver = strtolower($ver);
42 $ver = preg_replace('/-/', '.', $ver);
43 $ver = preg_replace('/^r/', '', $ver);
44 $ver .= '.';
45
46 $parts = array();
47 $len = strlen($ver);
48 $buf = '';
49 $state = 'NEW';
50 for ($i = 0; $i < $len; $i++) {
51 if ($ver{$i} == '.') {
52 $newCharType = 'SEP';
53 } elseif (is_numeric($ver{$i})) {
54 $newCharType = 'NUM';
55 } else {
56 $newCharType = 'ALPHA';
57 }
58
59 // printf("ver=[%s] state=%-5s newCharType=%-5s char=[%s] parts=[%-12s] buf=[%s]\n", $ver, $state, $newCharType, $ver{$i}, implode('/', $parts), $buf);
60 switch ($state) {
61 case 'NEW':
62 $buf .= $ver{$i};
63 $state = $newCharType;
64 break;
65 case 'NUM':
66 case 'ALPHA':
67 default:
68 if ($state == $newCharType) {
69 $buf .= $ver{$i};
70 } elseif ($newCharType == 'SEP') {
71 $parts[] = $buf;
72 $buf = '';
73 $state = 'NEW';
74 } elseif ($newCharType == 'NUM') {
75 $parts[] = $buf;
76 $buf = $ver{$i};
77 $state = $newCharType;
78 } elseif ($newCharType == 'ALPHA') {
79 $parts[] = $buf;
80 $buf = $ver{$i};
81 $state = $newCharType;
82 }
83 break;
84 }
85 }
86
87 $codes = array(
88 'alpha' => 'pre010',
89 'beta' => 'pre020',
90 'rc' => 'pre030',
91 );
92 foreach ($parts as $i => &$part) {
93 if (is_numeric($part)) {
94 if (strlen($part) > $pad) {
95 $part = 'invalid';
96 } else {
97 $part = sprintf("z%0${pad}s", $part);
98 }
99 } else {
100 if (isset($codes[$part])) {
101 $part = $codes[$part];
102 }
103 }
104 }
105
106 return $parts;
107 }