1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the samshal/rando package. |
5
|
|
|
* |
6
|
|
|
* (c) Samuel Adeshina <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Samshal\Rando; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Generates several random data such as names, country, gender, numbers, |
15
|
|
|
* SSN and so on using the mt_rand implementation of mersenne twitter. |
16
|
|
|
* It can be used to supply randomly generated data during tests and placeholding. |
17
|
|
|
* |
18
|
|
|
* @since 1.0 |
19
|
|
|
* |
20
|
|
|
* @author Samuel Adeshina <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Rando |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
* @access protected |
27
|
|
|
*/ |
28
|
|
|
protected static $packagesDirectory = __DIR__ . '/packages'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
* @access protected |
33
|
|
|
*/ |
34
|
|
|
protected static $commonNamespace = __NAMESPACE__.'\\Packages'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
* @access protected |
39
|
|
|
*/ |
40
|
|
|
protected static $commonInterface = 'Samshal\\Rando\\Packages\\PackageableInterface'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Magic method for calling packages, treats supplied package methods |
44
|
|
|
* as methods and retrieves their __toString values |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function __call($method, $parameters) |
50
|
|
|
{ |
51
|
|
|
return self::processInstruction($method, $parameters); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Magic method for calling static packages. A variation of the __call magic |
56
|
|
|
* method |
57
|
|
|
* |
58
|
|
|
* @access public |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public static function __callStatic($method, $parameters) |
62
|
|
|
{ |
63
|
|
|
return self::processInstruction($method, $parameters); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $method string |
68
|
|
|
* @param $parameters array |
69
|
|
|
* |
70
|
|
|
* Performs the instantiation of a class and returns |
71
|
|
|
* the generated random string value |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
private static function processInstruction($method, $parameters) |
77
|
|
|
{ |
78
|
|
|
$parameters = isset($parameters[0]) ? $parameters[0] : []; |
79
|
|
|
$class = self::instantiatePackage($method); |
80
|
|
|
return self::getRandoString($class, $parameters); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $packageName string |
85
|
|
|
* |
86
|
|
|
* Searches the directories containing package namespaces |
87
|
|
|
* for the $packageName parameter and then returns an instance |
88
|
|
|
* of the class when it finds it or throws an exception if it cant be found. |
89
|
|
|
* |
90
|
|
|
* @access private |
91
|
|
|
* @return PackagesInterface instance |
92
|
|
|
* @throws Samshal\Rando\Exceptions\RequestedClassNotFoundExcption |
93
|
|
|
*/ |
94
|
|
|
private static function instantiatePackage($packageName) |
95
|
|
|
{ |
96
|
|
|
$namespaceDirectories = glob(self::$packagesDirectory.'/*', GLOB_ONLYDIR); |
97
|
|
|
$packageName = ucfirst(strtolower($packageName)); |
98
|
|
|
foreach ($namespaceDirectories as $namespaceDirectory) { |
99
|
|
|
$baseName = basename($namespaceDirectory); |
100
|
|
|
$namespace = self::$commonNamespace.'\\'.ucfirst(strtolower($baseName)); |
101
|
|
|
$class = $namespace.'\\'.$packageName; |
102
|
|
|
if (class_exists($class)) { |
103
|
|
|
return new $class; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
throw new Exceptions\RequestedClassNotFoundException(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param class PackagesInterface |
111
|
|
|
* @param parameter array |
112
|
|
|
* |
113
|
|
|
* Returns a randomly generated string |
114
|
|
|
* |
115
|
|
|
* @access private |
116
|
|
|
* @return string |
117
|
|
|
* @throws Samshal\Rando\Exceptions\InterfaceNotImplementedException |
118
|
|
|
*/ |
119
|
|
|
private static function getRandoString($class, $parameters) |
120
|
|
|
{ |
121
|
|
|
$implementedInterfaces = class_implements($class); |
122
|
|
|
if (!in_array(self::$commonInterface, $implementedInterfaces)) { |
123
|
|
|
throw new Exceptions\InterfaceNotImplementedException(); |
124
|
|
|
} else { |
125
|
|
|
$class->initializeParameters($parameters); |
126
|
|
|
return $class->stringify(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|