|
| 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