|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ablett\TwigFaker; |
|
4
|
|
|
|
|
5
|
|
|
use Faker\Factory; |
|
6
|
|
|
|
|
7
|
|
|
class TwigFakerExtension extends \Twig_Extension { |
|
8
|
|
|
|
|
9
|
|
|
public static $factoriesPath = 'factories/'; |
|
10
|
|
|
private $cache; |
|
11
|
|
|
private $fake_data; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Returns the name of the extension. |
|
15
|
|
|
* |
|
16
|
|
|
* @return string The extension name |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getName() |
|
19
|
|
|
{ |
|
20
|
|
|
return 'faker'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Callback for twig. |
|
25
|
|
|
* |
|
26
|
|
|
* @return array |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getFunctions() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
|
|
|
|
|
31
|
|
|
'fake' => new \Twig_Function_Method($this, 'fakerData') |
|
|
|
|
|
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Generate fake data. |
|
37
|
|
|
* |
|
38
|
|
|
* @param $type |
|
39
|
|
|
* @param int $count |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function fakerData($type, $count = 1, $cache_key = null) |
|
43
|
|
|
{ |
|
44
|
|
|
if (isset($cache_key)) $cache_key = "{$type}-$cache_key"; |
|
45
|
|
|
|
|
46
|
|
|
if (isset($this->cache[$cache_key])) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->cache[$cache_key]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->createNewFakeData($type, $count); |
|
52
|
|
|
$this->cacheNewFakeData($cache_key); |
|
53
|
|
|
|
|
54
|
|
|
return $this->fake_data; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Create the fake data. |
|
59
|
|
|
* |
|
60
|
|
|
* @param $type |
|
61
|
|
|
* @param integer $count |
|
62
|
|
|
* @throws InvalidFactoryException |
|
63
|
|
|
*/ |
|
64
|
|
|
private function createNewFakeData($type, $count) |
|
65
|
|
|
{ |
|
66
|
|
|
$faker = Factory::create(); |
|
67
|
|
|
$this->fake_data = []; |
|
68
|
|
|
|
|
69
|
|
|
for ($i = 0; $i < $count; $i++) |
|
70
|
|
|
{ |
|
71
|
|
|
if ( ! file_exists($this->fullFactoryPath($type))) |
|
72
|
|
|
{ |
|
73
|
|
|
throw new InvalidFactoryException('does not exist'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$fake_item = include $this->fullFactoryPath($type); |
|
77
|
|
|
|
|
78
|
|
|
array_push($this->fake_data, $fake_item); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Cache the new fake data if a key was sent. |
|
84
|
|
|
* |
|
85
|
|
|
* @param $cache_key |
|
86
|
|
|
*/ |
|
87
|
|
|
private function cacheNewFakeData($cache_key) |
|
88
|
|
|
{ |
|
89
|
|
|
if ($cache_key) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->cache[$cache_key] = $this->fake_data; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the full factory path. |
|
97
|
|
|
* |
|
98
|
|
|
* @param $type |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
private function fullFactoryPath($type) |
|
102
|
|
|
{ |
|
103
|
|
|
return static::$factoriesPath . $type . '.php'; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
class InvalidFactoryException extends \Exception {} |
|
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.