1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexWells\ApiDocsGenerator; |
4
|
|
|
|
5
|
|
|
class Helpers |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Clears \n characters (and similar) in the string. |
9
|
|
|
* |
10
|
|
|
* @param string $string |
11
|
|
|
* |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
|
|
public static function clearNewlines($string) |
15
|
|
|
{ |
16
|
|
|
return trim(preg_replace('/\s+/', ' ', $string)); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Converts any case text into normal string (see tests for examples). |
21
|
|
|
* |
22
|
|
|
* @param string $string |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public static function functionNameToText($string) |
27
|
|
|
{ |
28
|
|
|
// Firstly we need to make sure that the string is in camelCase, |
29
|
|
|
// otherwise splitting into words will not work |
30
|
|
|
$string = camel_case($string); |
31
|
|
|
|
32
|
|
|
// Then convert camelCase into normal text |
33
|
|
|
$string = static::camelCaseToLabelCase($string); |
34
|
|
|
|
35
|
|
|
// After that we need to make sure that each word starts with lower case |
36
|
|
|
// character (except for abbreviations) |
37
|
|
|
$string = static::lcwords($string); |
38
|
|
|
|
39
|
|
|
// Finally, we convert first character of the string into upper case |
40
|
|
|
return ucfirst($string); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Replaces camelCase string with label case (normal text), preserving abbreviations. |
45
|
|
|
* |
46
|
|
|
* @param string $string |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public static function camelCaseToLabelCase($string) |
51
|
|
|
{ |
52
|
|
|
$re = '/(?#! splitCamelCase Rev:20140412) |
53
|
|
|
# Split camelCase "words". Two global alternatives. Either g1of2: |
54
|
|
|
(?<=[a-z]) # Position is after a lowercase, |
55
|
|
|
(?=[A-Z]) # and before an uppercase letter. |
56
|
|
|
| (?<=[A-Z]) # Or g2of2; Position is after uppercase, |
57
|
|
|
(?=[A-Z][a-z]) # and before upper-then-lower case. |
58
|
|
|
/x'; |
59
|
|
|
|
60
|
|
|
return preg_replace($re, ' ', $string); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Works similar to ucwords (replaces first character of each word to lower case). |
65
|
|
|
* |
66
|
|
|
* @param string $string |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public static function lcwords($string) |
71
|
|
|
{ |
72
|
|
|
return preg_replace_callback('/\b([A-Z])(?(?=\s)(\s)|([a-z]))/', function ($match) { |
73
|
|
|
return str_replace($match[1], strtolower($match[1]), $match[0]); |
74
|
|
|
}, $string); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Wraps provided regex pattern so it does not work inside double quote pairs (works between them). |
79
|
|
|
* |
80
|
|
|
* Note: delimiters are also returned. |
81
|
|
|
* |
82
|
|
|
* @param string $regex |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public static function regexExcludeInQuotes($regex) |
87
|
|
|
{ |
88
|
|
|
return '/' . $regex . '\s*(?=([^"]*"[^"]*")*[^"]*$)' . '/'; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|