-
Notifications
You must be signed in to change notification settings - Fork 11
/
Utils.php
68 lines (64 loc) · 1.79 KB
/
Utils.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
<?php
namespace Navitia\Component;
use Navitia\Component\Exception\NavitiaCreationException;
/**
* Description of Utils
*
* @copyright (c) 2013, CANALTP
* @author rndiaye
*/
class Utils
{
/**
* Fonction permettant de supprimer les underscores
*
* @param string $param
* @return string
*/
public static function deleteUnderscore($param)
{
$parts = explode('_', $param);
$result = array_shift($parts);
foreach ($parts as $part) {
$result .= ucfirst($part);
}
return $result;
}
/**
* Fonction permettant de setter
* @param object $request
* @param array $params
* @return object
* @throws NavitiaCreationException
*/
public static function setter($request, $params)
{
if (is_array($params)) {
foreach ($params as $property => $value) {
$property = Utils::deleteUnderscore($property);
$setter = 'set'.ucfirst($property);
if (method_exists($request, $setter)) {
$request->$setter($value);
} else {
throw new NavitiaCreationException(
sprintf(
'Neither property "%s" nor method "%s"'.
'nor method "%s" exist.',
$property,
'get'.ucfirst($property),
$setter
)
);
}
}
} else {
throw new NavitiaCreationException(
sprintf(
'The parameter (type "%s") will be an Array',
gettype($params)
)
);
}
return $request;
}
}