Skip to content

Commit

Permalink
Added node clone function.
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 committed Feb 12, 2017
1 parent 6bdddab commit d03f7c2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 66 deletions.
68 changes: 25 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,26 @@ Add `mehr-als-nix/json-faker` dependency to the `require` section inside your co
## Examples

```php
<?php

use MehrAlsNix\JsonFaker\JsonFaker;

$jsonTemplate = <<<JSON
{
"options": [
{
"__JSON_OPTIONS__": "JSON_PRETTY_PRINT|JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP"
"__JSON_OPTIONS__": "JSON_PRETTY_PRINT|JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP|JSON_UNESCAPED_SLASHES",
"__NODE_CLONE__": 3
}
],
"fixture": [
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__",
"simple-text": "__RAND__TEXT__"
},
[
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__",
"simple-text": "test test test",
"simple-array": [
"1",
2,
"__RAND_FLOAT__"
]
},
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__"
}
]
"simple-text": "__RAND_TEXT__",
"user-agent": "__RAND_USERAGENT__"
}
]
}
JSON;
Expand All @@ -63,28 +48,25 @@ Running this script generates a JSON string with random values like:
```json
[
{
"float-value": 619505.89336841,
"boolean-value": false,
"number-value": 46243865,
"simple-text": "aspernatur saepe"
"float-value": 157176.955378,
"boolean-value": true,
"number-value": 855701,
"simple-text": "pariatur ad",
"user-agent": "Opera/8.99 (Windows NT 6.2; sl-SI) Presto/2.9.218 Version/12.00"
},
{
"float-value": 1,
"boolean-value": true,
"number-value": 1207,
"simple-text": "ex sit",
"user-agent": "Mozilla/5.0 (iPad; CPU OS 7_1_1 like Mac OS X; sl-SI) AppleWebKit/535.11.1 (KHTML, like Gecko) Version/4.0.5 Mobile/8B118 Safari/6535.11.1"
},
[
{
"float-value": 866878.15492,
"boolean-value": false,
"number-value": 0,
"simple-text": "dolores voluptates assumenda",
"simple-array": [
5,
6,
1072888.1899307
]
},
{
"float-value": 3733219.1933299,
"boolean-value": true,
"number-value": 705
}
]
{
"float-value": 933,
"boolean-value": true,
"number-value": 59201435,
"simple-text": "voluptatem qui",
"user-agent": "Mozilla/5.0 (iPad; CPU OS 8_1_2 like Mac OS X; en-US) AppleWebKit/532.46.4 (KHTML, like Gecko) Version/4.0.5 Mobile/8B116 Safari/6532.46.4"
}
]
```
26 changes: 5 additions & 21 deletions examples/templates/example.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
{
"options": [
{
"__JSON_OPTIONS__": "JSON_PRETTY_PRINT|JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP"
"__JSON_OPTIONS__": "JSON_PRETTY_PRINT|JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP|JSON_UNESCAPED_SLASHES",
"__NODE_CLONE__": 3
}
],
"fixture": [
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__",
"simple-text": "__RAND__TEXT__"
},
[
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__",
"simple-text": "test test test",
"simple-array": [
"1",
2,
"__RAND_FLOAT__"
]
},
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__"
}
]
"simple-text": "__RAND_TEXT__",
"user-agent": "__RAND_USERAGENT__"
}
]
}
11 changes: 9 additions & 2 deletions src/JsonFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
class JsonFaker
{
private static $OPTIONS = [
['__JSON_OPTIONS__' => false]
'__JSON_OPTIONS__' => false,
'__NODE_CLONE__' => false
];

/** @var string $jsonTemplate */
Expand Down Expand Up @@ -39,7 +40,11 @@ public function __toString()
$json = json_decode($json, true);
$fixture = $json['fixture'];

self::$OPTIONS = array_merge(self::$OPTIONS[0], $json['options'][0]);
self::$OPTIONS = array_merge(self::$OPTIONS, $json['options'][0]);

if (self::$OPTIONS['__NODE_CLONE__']) {
$fixture = array_fill(0, (int) self::$OPTIONS['__NODE_CLONE__'], $fixture[0]);
}

array_walk_recursive($fixture, [$this, 'getFakeValue']);

Expand Down Expand Up @@ -70,6 +75,8 @@ private function getFakeValue(&$original)
$original = $this->faker->randomFloat();
} elseif ($original === "__RAND_BOOLEAN__") {
$original = $this->faker->randomElement([true, false]);
} elseif ($original === '__RAND_USERAGENT__') {
$original = $this->faker->userAgent;
} elseif (is_numeric($original)) {
if (strpos($original, '.') !== false) {
$original = $this->faker->randomFloat();
Expand Down

0 comments on commit d03f7c2

Please sign in to comment.