Skip to content

Commit c50a312

Browse files
committed
initial commit
0 parents  commit c50a312

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Dave Olsen, http://dmolsen.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Faker Plugin for Pattern Lab
2+
3+
The Faker Plugin adds support for the [Faker Library](https://github.com/fzaninotto/Faker) to Pattern Lab.
4+
5+
## Installation
6+
7+
Pattern Lab PHP uses [Composer](https://getcomposer.org/) to manage project dependencies with Pattern Lab Editions. To add the Faker Plugin to the dependencies list for your Edition you can type the following in the command line at the base of your project:
8+
9+
composer require pattern-lab/plugin-faker
10+
11+
See Packagist for [information on the latest release](https://packagist.org/packages/pattern-lab/plugin-faker).
12+
13+
## Usage
14+
15+
To use this in your project set the value for a data option to:
16+
17+
```
18+
"key": "Faker.[formatter]([options])",
19+
```
20+
21+
For example, to have a random first name use:
22+
23+
```
24+
"firstName": "Faker.firstName('female')",
25+
```
26+
27+
Using Mustache as an example you'd use:
28+
29+
```
30+
First Name: {{ firstName }}
31+
```
32+
33+
would output:
34+
35+
```
36+
First Name: Mary
37+
```

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "pattern-lab/plugin-faker",
3+
"description": "Faker for the PHP version of Pattern Lab",
4+
"keywords": ["faker", "pattern lab"],
5+
"homepage": "http://patternlab.io",
6+
"license": "MIT",
7+
"type": "patternlab-plugin",
8+
"authors": [
9+
{
10+
"name": "Dave Olsen",
11+
"email": "[email protected]",
12+
"homepage": "http://dmolsen.com",
13+
"role": "Lead Developer"
14+
}
15+
],
16+
"support": {
17+
"issues": "https://github.com/pattern-lab/mustachehelper-php-verbatim/issues",
18+
"wiki": "http://patternlab.io/docs/",
19+
"source": "https://github.com/pattern-lab/mustachehelper-php-verbatim/releases"
20+
},
21+
"autoload": {
22+
"psr-0": {
23+
"PatternLab\\Faker": "src/"
24+
}
25+
},
26+
"require": {
27+
"php": ">=5.4",
28+
"pattern-lab/core": "^2.0.0",
29+
"fzaninotto/faker": "^1.0.0"
30+
},
31+
"extra": {
32+
"patternlab": {
33+
"config": {
34+
"faker": {
35+
"locale": "en_US"
36+
}
37+
}
38+
}
39+
}
40+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/*!
4+
* Faker Listener Class
5+
*
6+
* Copyright (c) 2016 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
* Adds Faker support to Pattern Lab
10+
*
11+
*/
12+
13+
namespace PatternLab\Faker;
14+
15+
use \PatternLab\Config;
16+
use \PatternLab\PatternEngine\Twig\TwigUtil;
17+
18+
class PatternLabListener extends \PatternLab\Listener {
19+
20+
/**
21+
* Add the listeners for this plug-in
22+
*/
23+
public function __construct() {
24+
25+
// add listener
26+
$this->addListener("twigPatternLoader.customize","fakeContent");
27+
28+
// set-up locale
29+
$locale = Config::getOption("faker.locale");
30+
$locale = ($locale) ? $locale : "en_US";
31+
32+
// set-up Faker
33+
$this->faker = \Faker\Factory::create($locale);
34+
$this->faker->addProvider(new \Faker\Provider\Color($faker));
35+
$this->faker->addProvider(new \Faker\Provider\Payment($faker));
36+
$this->faker->addProvider(new \Faker\Provider\DateTime($faker));
37+
$this->faker->addProvider(new \Faker\Provider\Image($faker));
38+
39+
}
40+
41+
/**
42+
* Go through data and replace any values that match items from the link.array
43+
* @param {String} a string entry from the data to check for link.pattern
44+
*
45+
* @return {String} replaced version of link.pattern
46+
*/
47+
private function compareReplaceFaker($value) {
48+
if (is_string($value) && preg_match("/^Faker\.([A-z]+)(\((\'|\")?([A-z]+)?(\'|\")?\))?$/",$value,$matches)) {
49+
$formatter = $matches[1];
50+
$options = isset($matches[5]) ? $matches[5] : "";
51+
if ($options != "") {
52+
return $this->formatOptionsAndFake($formatter, $options);
53+
} else {
54+
return $this->faker->$formatter;
55+
}
56+
}
57+
return $value;
58+
}
59+
60+
/**
61+
* Read in the data and process faker data
62+
*/
63+
public function fakeContent() {
64+
65+
$foo = $this->recursiveWalk(Data::get());
66+
print_r($foo);
67+
68+
}
69+
70+
/**
71+
* Read in the data and process faker data
72+
*/
73+
public function formatOptionsAndFake($formatter, $options) {
74+
75+
if (($formatter == "date") || ($formatter == "time")) {
76+
return $this->faker->$formatter($options);
77+
} else {
78+
$options = explode(",", $options);
79+
if (count($options) === 6) {
80+
return $this->faker->$formatter($options[0],$options[1],$options[2],$options[3],$options[4],$options[5]);
81+
} else if (count($options) === 5) {
82+
return $this->faker->$formatter($options[0],$options[1],$options[2],$options[3],$options[4]);
83+
} else if (count($options) === 4) {
84+
return $this->faker->$formatter($options[0],$options[1],$options[2],$options[3]);
85+
} else if (count($options) === 3) {
86+
return $this->faker->$formatter($options[0],$options[1],$options[2]);
87+
} else if (count($options) === 2) {
88+
return $this->faker->$formatter($options[0],$options[1]);
89+
} else {
90+
return $this->faker->$formatter($options[0]);
91+
}
92+
}
93+
94+
}
95+
96+
/**
97+
* Work through a given array and decide if the walk should continue or if we should replace the var
98+
* @param {Array} the array to be checked
99+
*
100+
* @return {Array} the "fixed" array
101+
*/
102+
private function recursiveWalk($array) {
103+
foreach ($array as $k => $v) {
104+
if (is_array($v)) {
105+
$array[$k] = self::recursiveWalk($v);
106+
} else {
107+
$array[$k] = self::compareReplaceFaker($v);
108+
}
109+
}
110+
return $array;
111+
}
112+
113+
114+
115+
}

0 commit comments

Comments
 (0)