-
Notifications
You must be signed in to change notification settings - Fork 17
/
Factory.php
54 lines (44 loc) · 1.4 KB
/
Factory.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
<?php
namespace ValdeirPsr\PagSeguro\Validation;
class Factory
{
const RULES_NAMESPACE = '\\ValdeirPsr\\PagSeguro\\Validation\\Rules\\';
private static $defaultInstance;
/**
* Captura a intancia atual do objeto
*/
public static function getDefaultInstance()
{
if (self::$defaultInstance === null) {
self::$defaultInstance = new self();
}
return self::$defaultInstance;
}
/**
* Instancia, caso exista, a regra informada
*
* @param string $name regra
* @param string $args
*
* @throws \UnexpectedValueException Caso a regra não exista
* @throws \BadMethodCallException Caso a classe da regra não seja instanciável
*
* @return IValidation
*/
public function rule($name, $args)
{
$className = self::RULES_NAMESPACE . ucfirst($name);
if (!class_exists($className)) {
throw new \UnexpectedValueException("Rule {$className} not found", 2000);
}
$reflection = new \ReflectionClass($className);
if (!$reflection->isInstantiable()) {
throw new \BadMethodCallException("Class $className is not instantiable", 2001);
}
if ($reflection->getConstructor() !== null) {
return $reflection->newInstanceArgs($args);
} else {
return $reflection->newInstanceWithoutConstructor();
}
}
}