Skip to content

Commit

Permalink
Add regex support
Browse files Browse the repository at this point in the history
Add Yaml conf file support
Increase speed execution
  • Loading branch information
niklongstone committed Jun 1, 2015
1 parent ba61e3b commit fef1511
Show file tree
Hide file tree
Showing 27 changed files with 613 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
@@ -1,3 +1,9 @@
0.7.0 / 2015-06-01
==================
* Add regular expression support
* Add Yaml configuration support
* Improve data generation speed

0.6.4 / 2015-05-26
==================
* Add different ODS data path support
Expand Down
25 changes: 16 additions & 9 deletions README.md
Expand Up @@ -14,14 +14,18 @@ Fakerino is a fake data generator framework fully extensible.

###Main features
Fakerino can:
* Fakes complex data (e.g. person: name, surname, hobby, country, ... ).
* Fakes single data (e.g. name, surname, integer, text, ...).
* Fakes data multiple times.
* Fakes a database table row/s automatically.
* Fakes a string or file template automatically (e.g. Hello Mr {{ surname }})
* Fakes a PHP Object (fills public properties and setters with fake data).
* Supports JSON, array and string output.

* Fake complex data (e.g. person: name, surname, hobby, country, ... ).
* Fake single data (e.g. name, surname, integer, text, ...).
* Fake data in different languages.
* Fake regular expression data (e.g. url => '/www\.\w+\.com/').
* Fake data multiple times.
* Fake a database table row/s automatically.
* Fake a string or file template automatically (e.g. Hello Mr {{ surname }})
* Fake a PHP Object (fills public properties and setters with fake data).
* Support JSON, array and string output.
* Support array, Yaml, XML, PHP, Txt and Ini configurations.
* Fake 500,000 data in 1 minute.

For more information about installation, functions, support, contribution, or other,
please read the __[Fakerino wiki](https://github.com/niklongstone/Fakerino/wiki)__.

Expand All @@ -46,10 +50,13 @@ use Fakerino\Fakerino;
$fakerino = Fakerino::create();
echo $fakerino->fake('Surname')->toJson(); //["Donovan"]
echo $fakerino->fake('nameFemale'); //Alice
echo $fakerino->fake('/www\.\w+\.com/'); //www.nikdjap.com
echo $fakerino->fake('nameMale')->num(3); //Bob Jack Rick
echo $fakerino->fake(array('nameMale', 'Surname'))->num(3)->toJson(); //[["Simon","Rodgers"],["Dean","Smith"],["Anthony","Bauman"]]
```

//with configuration
With a [configuration](https://github.com/niklongstone/Fakerino/wiki/Use-a-configuration-file) you can __combine fake data__, or declare your __customs__.
```PHP
$fakerino = Fakerino::create('./conf.php');
print_r($fakerino->fake('fakeFamily')->toArray());
/*
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -15,7 +15,9 @@
"require": {
"php": ">=5.3.0",
"doctrine/dbal": "2.5.1",
"twig/twig": "~1.0"
"twig/twig": "~1.0",
"niklongstone/regex-reverse": "^0.3.0",
"symfony/yaml": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
Expand Down
@@ -0,0 +1,32 @@
<?php
/**
* This file is part of the Fakerino package.
*
* (c) Nicola Pietroluongo <niklongstone@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fakerino\Configuration\ConfigurationFile;

use Fakerino\Configuration\AbstractConfigurationFile;
use Symfony\Component\Yaml\Parser;

/**
* Class YamlConfigurationFile
*
* @author Nicola Pietroluongo <nik.longstone@gmail.com>
*/
class YamlConfigurationFile extends AbstractConfigurationFile
{
/**
* {@inheritdoc}
*/
public function toArray()
{
$yaml = new Parser();

return $yaml->parse(file_get_contents($this->getConfFilePath(), true));
}
}
46 changes: 27 additions & 19 deletions src/Fakerino/Core/FakeHandler/Handler.php
Expand Up @@ -9,6 +9,7 @@
*/

namespace Fakerino\Core\FakeHandler;

use Fakerino\Core\FakeElement;
use Fakerino\Core\OutPutFactory;

Expand All @@ -31,10 +32,17 @@ abstract class Handler implements HandlerInterface
private static $first = null;

/**
* Sets a successor handler,
* in case the class is not able to satisfy the request.
*
* @param HandlerInterface $handler
* @var array
*/
private static $outputContainer = array();

/**
* @var string
*/
private static $fakeElement;

/**
* {@inheritdoc}
*/
final public function setSuccessor(HandlerInterface $handler)
{
Expand All @@ -47,15 +55,18 @@ final public function setSuccessor(HandlerInterface $handler)
}

/**
* Handles the request or redirect the request
* to the successor.
*
* @param FakeElement $data
*
* @return string
* {@inheritdoc}
*/
final public function handle(FakeElement $data)
{
self::$fakeElement = $data->getName() . serialize($data->getOptions());
if (array_key_exists(self::$fakeElement, self::$outputContainer)) {

return $this->getOutput(
self::$outputContainer[self::$fakeElement][0],
self::$outputContainer[self::$fakeElement][1]
);
}
$processed = $this->process($data);
if ($processed === null) {
if ($this->successor !== null) {
Expand All @@ -67,22 +78,19 @@ final public function handle(FakeElement $data)
}

/**
* Generates the output.
*
* @param string $class
* @param string|array|null $options
*
* @return string|array
* {@inheritdoc}
*/
public function getOutput($class, $options = null)
{
if (!array_key_exists(self::$fakeElement, self::$outputContainer)) {
self::$outputContainer[self::$fakeElement] = array($class, $options);
}

return OutPutFactory::getOutput($class, $options);
}

/**
* Returns the first Handler in the chain.
*
* @return Handler
* {@inheritdoc}
*/
public static function getFirstChain()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Fakerino/Core/FakeHandler/HandlerInterface.php
Expand Up @@ -35,7 +35,7 @@ function setSuccessor(HandlerInterface $handler);
*
* @param FakeElement $data
*
* @return string
* @return mixed
*/
function handle(FakeElement $data);

Expand Down
62 changes: 62 additions & 0 deletions src/Fakerino/Core/FakeHandler/RegExFakerClass.php
@@ -0,0 +1,62 @@
<?php
/**
* This file is part of the Fakerino package.
*
* (c) Nicola Pietroluongo <nik.longstone@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fakerino\Core\FakeHandler;

use Fakerino\Core\RegEx\RegExGeneratorInterface;

/**
* Class RegExFakerClass,
* processes the request of a regular expression.
*
* @author Nicola Pietroluongo <nik.longstone@gmail.com>
*/
class RegExFakerClass extends Handler
{
private $regexGenerator;

/**
* Constructor.
*
* @param RegExGeneratorInterface $regexGenerator
*/
public function __construct(RegExGeneratorInterface $regexGenerator)
{
$this->regexGenerator = $regexGenerator;
}

/**
* {@inheritdoc}
*/
protected function process($data)
{
$elementName = $data->getName();
$options = $data->getOptions();
$expr = null;
if ($elementName[0] == '/') {
$expr = $elementName;
}
if ($options !== null && is_string($options)) {
if ($options[0] == '/') {
$expr = $options;
}
}
if ($expr !== null) {
$options = array(
'regexgenerator' => $this->regexGenerator,
'expression' => $expr
);

return $this->getOutput('Fakerino\\FakeData\\Core\\RegExFake', $options);
}

return;
}
}
30 changes: 30 additions & 0 deletions src/Fakerino/Core/RegEx/Exception/InvalidRegexException.php
@@ -0,0 +1,30 @@
<?php
/**
* This file is part of the Fakerino package.
*
* (c) Nicola Pietroluongo <nik.longstone@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fakerino\Core\RegEx\Exception;

/**
* Class InvalidRegexException,
* appears when the format of the regular expression is not valid.
*
* @author Nicola Pietroluongo <nik.longstone@gmail.com>
*/
class InvalidRegexException extends \RuntimeException
{
/**
* Construct
*
* @param string $expr
*/
public function __construct($expr)
{
parent::__construct(sprintf('The format of the regular expression "%s" is invalid', $expr));
}
}
29 changes: 29 additions & 0 deletions src/Fakerino/Core/RegEx/RegExGeneratorInterface.php
@@ -0,0 +1,29 @@
<?php
/**
* This file is part of the Fakerino package.
*
* (c) Nicola Pietroluongo <nik.longstone@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fakerino\Core\RegEx;

/**
* Interface RegExInterface,
* provides an interface for a regular expression generator.
*
* @package Fakerino\Core\RegEx
*/
interface RegExGeneratorInterface
{
/**
* Generates a string from the provided $regex
*
* @param string $regex
*
* @return string
*/
public function generate($regex);
}
41 changes: 41 additions & 0 deletions src/Fakerino/Core/RegEx/RegRevGenerator.php
@@ -0,0 +1,41 @@
<?php
/**
* This file is part of the Fakerino package.
*
* (c) Nicola Pietroluongo <nik.longstone@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fakerino\Core\RegEx;

use Fakerino\Core\RegEx\Exception\InvalidRegexException;
use RegRev\RegRev;

/**
* Interface RegExGenerator,
* provides an interface for the RegRev regular expression generator.
*
* @package Fakerino\Core\RegEx
*/
class RegRevGenerator implements RegExGeneratorInterface
{
/**
* @param string $regex
*
* @return string
* @throws InvalidRegexException
*/
public function generate($regex)
{
$expr = substr($regex, 1, -1);
$result = RegRev::generate($expr);
if ($result !== null) {

return $result;
} else {
throw new InvalidRegexException($expr);
}
}
}

0 comments on commit fef1511

Please sign in to comment.