-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConvention.php
More file actions
163 lines (141 loc) · 3.81 KB
/
Convention.php
File metadata and controls
163 lines (141 loc) · 3.81 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
154
155
156
157
158
159
160
161
162
163
<?php
namespace UniMapper;
use UniMapper\Adapter\IConvention;
use UniMapper\Exception\InvalidArgumentException;
class Convention
{
const ENTITY_MASK = 1;
const REPOSITORY_MASK = 2;
/** @var string */
private static $masks = [
self::ENTITY_MASK => "*",
self::REPOSITORY_MASK => "*Repository"
];
private static $adapterConventions = [];
/**
* Register adapter naming convention
*
* @param string $name
* @param IConvention $convention
*/
public static function registerAdapterConvention($name, IConvention $convention)
{
self::$adapterConventions[$name] = $convention;
}
/**
* Has adapter naming convention registered
*
* @param $name
*
* @return bool
*/
public static function hasAdapterConvention($name)
{
return isset(self::$adapterConventions[$name]);
}
/**
* Get adapter naming convention
*
* @param $name
*
* @return IConvention
*/
public static function getAdapterConvention($name)
{
return self::$adapterConventions[$name];
}
/**
* Converts class to name
*
* @param string $class
* @param integer $type Mask type
*
* @return string
*
* @throws InvalidArgumentException
*/
public static function classToName($class, $type)
{
if (!class_exists($class)) {
throw new InvalidArgumentException(
"Class '" . $class . "' not found!"
);
}
$class = self::_trimNamespace($class);
if (!isset(self::$masks[$type])) {
throw new InvalidArgumentException(
"Invalid mask type " . $type . "!"
);
}
$mask = self::_trimNamespace(self::$masks[$type]);
if ($mask === "*") {
return $class;
}
preg_match("/" . str_replace("*", "(.*)", $mask) . "/", $class, $match);
return $match[1];
}
/**
* Converts name to class
*
* @param string $name
* @param integer $type
*
* @return string
*
* @throws InvalidArgumentException
*/
public static function nameToClass($name, $type)
{
if (!isset(self::$masks[$type])) {
throw new InvalidArgumentException(
"Invalid mask type " . $type . "!"
);
}
return str_replace("*", $name, self::$masks[$type]);
}
private static function _isValidMask($mask)
{
if (substr_count($mask, "*") <> 1) {
return false;
}
$mask = self::_trimNamespace($mask);
return $mask === "*" || self::_startsWith($mask, "*")
|| self::_endsWith($mask, "*");
}
private static function _trimNamespace($class)
{
$parts = explode("\\", $class);
return end($parts);
}
public static function getMask($type)
{
if (!isset(self::$masks[$type])) {
throw new InvalidArgumentException(
"Invalid mask type " . $type . "!"
);
}
return self::$masks[$type];
}
public static function setMask($mask, $type)
{
if (!isset(self::$masks[$type])) {
throw new InvalidArgumentException(
"Invalid mask type " . $type . "!"
);
}
if (!self::_isValidMask($mask)) {
throw new InvalidArgumentException(
"Invalid mask '" . $mask . "'!"
);
}
self::$masks[$type] = $mask;
}
private static function _startsWith($haystack, $needle)
{
return $needle === "" || strpos($haystack, $needle) === 0;
}
private static function _endsWith($haystack, $needle)
{
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
}