-
Notifications
You must be signed in to change notification settings - Fork 11
/
AbstractFactory.php
139 lines (126 loc) · 2.79 KB
/
AbstractFactory.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
<?php
/*
* AbstractFactory
*/
namespace Navitia\Component;
use Navitia\Component\Exception\NavitiaCreationException;
/**
* Description of AbstractFactory
*
* @author rndiaye
*/
class AbstractFactory implements FactoryInterface
{
private $suffix;
private $prefix = null;
private $defaultClass = null;
/**
* {@inheritDoc}
*/
public function create($type)
{
$name = $this->builClassName($type);
if (class_exists($name)) {
return new $name;
} else {
if (!is_null($this->getDefaultClass())) {
$default = $this->getNamespace().'\\'.$this->getDefaultClass();
if (class_exists($default)) {
return new $default;
}
}
throw new NavitiaCreationException(
sprintf(
'Class "%s" not found',
$name
)
);
}
}
/**
* Getter du suffix
*
* @return string
*/
public function getSuffix()
{
return $this->suffix;
}
/**
* Setter du suffix
*
* @param string $suffix
* @return \Navitia\Component\AbstractFactory
*/
public function setSuffix($suffix)
{
$this->suffix = ucfirst($suffix);
return $this;
}
/**
* Getter du prefix
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* Setter du prefix
*
* @param string $prefix
* @return \Navitia\Component\AbstractFactory
*/
public function setPrefix($prefix)
{
$this->prefix = ucfirst($prefix);
return $this;
}
/**
* Getter d'une class par defaut
*
* @return string
*/
public function getDefaultClass()
{
return $this->defaultClass;
}
/**
* Setter d'une class par défaut
*
* @param string $defaultClass
* @return \Navitia\Component\AbstractFactory
*/
public function setDefaultClass($defaultClass)
{
$this->defaultClass = $defaultClass;
return $this;
}
/**
* Fonction permettant de créer le nom de la class
*
* @params string
* @return string
*/
private function builClassName($type)
{
$name = $this->getNamespace().'\\';
if (!is_null($this->getprefix())) {
$name .= $this->getprefix();
}
$name .= ucfirst($type).$this->getSuffix();
return $name;
}
/**
* Recupération du namespace
*
* @return string
*/
public function getNamespace()
{
$currentClass = get_class($this);
$reflector = new \ReflectionClass($currentClass);
return $reflector->getNamespaceName();
}
}