forked from sonata-project/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validators.php
156 lines (136 loc) · 3.85 KB
/
Validators.php
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
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Command;
/**
* Class Validators.
*
* @author Thomas Rabaix <[email protected]>
*/
class Validators
{
/**
* @static
*
* @param string $username
*
* @return mixed
*
* @throws \InvalidArgumentException
*/
public static function validateUsername($username)
{
if (is_null($username)) {
throw new \InvalidArgumentException('The username must be set');
}
return $username;
}
/**
* @static
*
* @param string $shortcut
*
* @return array
*
* @throws \InvalidArgumentException
*/
public static function validateEntityName($shortcut)
{
$entity = str_replace('/', '\\', $shortcut);
if (false === $pos = strpos($entity, ':')) {
throw new \InvalidArgumentException(sprintf('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Post)', $entity));
}
return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
}
/**
* @static
*
* @param string $class
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function validateClass($class)
{
$class = str_replace('/', '\\', $class);
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('The class "%s" does not exist.', $class));
}
return $class;
}
/**
* @static
*
* @param string $adminClassBasename
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function validateAdminClassBasename($adminClassBasename)
{
$adminClassBasename = str_replace('/', '\\', $adminClassBasename);
if (false !== strpos($adminClassBasename, ':')) {
throw new \InvalidArgumentException(sprintf('The admin class name must not contain a : ("%s" given, expecting something like PostAdmin")', $adminClassBasename));
}
return $adminClassBasename;
}
/**
* @static
*
* @param string $controllerClassBasename
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function validateControllerClassBasename($controllerClassBasename)
{
$controllerClassBasename = str_replace('/', '\\', $controllerClassBasename);
if (false !== strpos($controllerClassBasename, ':')) {
throw new \InvalidArgumentException(sprintf('The controller class name must not contain a : ("%s" given, expecting something like PostAdminController")', $controllerClassBasename));
}
if (substr($controllerClassBasename, -10) != 'Controller') {
throw new \InvalidArgumentException('The controller class name must end with Controller.');
}
return $controllerClassBasename;
}
/**
* @static
*
* @param string $servicesFile
*
* @return string
*/
public static function validateServicesFile($servicesFile)
{
return trim($servicesFile, '/');
}
/**
* @static
*
* @param string $serviceId
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function validateServiceId($serviceId)
{
if (preg_match('/[^A-Za-z\._0-9]/', $serviceId, $matches)) {
throw new \InvalidArgumentException(sprintf(
'Service ID "%s" contains invalid character "%s".',
$serviceId,
$matches[0]
));
}
return $serviceId;
}
}