Skip to content

Commit

Permalink
Added json options to the templlate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 committed Feb 12, 2017
1 parent 929a077 commit 6bdddab
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 40 deletions.
69 changes: 56 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,39 @@ Add `mehr-als-nix/json-faker` dependency to the `require` section inside your co
use MehrAlsNix\JsonFaker\JsonFaker;

$jsonTemplate = <<<JSON
[
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__",
"simple-text": "__RAND__TEXT__"
}
]
{
"options": [
{
"__JSON_OPTIONS__": "JSON_PRETTY_PRINT|JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP"
}
],
"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__"
}
]
]
}
JSON;

echo (string) new JsonFaker($jsonTemplate, false);
Expand All @@ -38,10 +63,28 @@ Running this script generates a JSON string with random values like:
```json
[
{
"float-value": 5.234791,
"boolean-value": true,
"number-value": 867,
"simple-text": "id consequatur"
}
"float-value": 619505.89336841,
"boolean-value": false,
"number-value": 46243865,
"simple-text": "aspernatur saepe"
},
[
{
"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
}
]
]
```
43 changes: 27 additions & 16 deletions examples/templates/example.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
[
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__",
"simple-text": "__RAND__TEXT__"
},
[
{
"options": [
{
"__JSON_OPTIONS__": "JSON_PRETTY_PRINT|JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP"
}
],
"fixture": [
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__",
"simple-text": "test test test",
"simple-array": ["1", 2, "__RAND_FLOAT__"]
"simple-text": "__RAND__TEXT__"
},
{
"float-value": "__RAND_FLOAT__",
"boolean-value": "__RAND_BOOLEAN__",
"number-value": "__RAND_NUMBER__"
}
[
{
"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__"
}
]
]
]
}
35 changes: 24 additions & 11 deletions src/JsonFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class JsonFaker
{
private static $OPTIONS = [
['__JSON_OPTIONS__' => false]
];

/** @var string $jsonTemplate */
private $jsonTemplate;

Expand All @@ -26,20 +30,29 @@ public function __construct($jsonTemplate, $fromFile = true)

public function __toString()
{
try {
$json = $this->jsonTemplate;

if ($this->fromFile) {
$json = file_get_contents($this->jsonTemplate);
}
$json = $this->jsonTemplate;

$json = json_decode($json, true);
array_walk_recursive($json, [$this, 'getFakeValue']);
} catch (\Exception $e) {
var_dump($e->getMessage());
if ($this->fromFile) {
$json = file_get_contents($this->jsonTemplate);
}

return json_encode($json, JSON_PRETTY_PRINT);
$json = json_decode($json, true);
$fixture = $json['fixture'];

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

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

if (self::$OPTIONS['__JSON_OPTIONS__']) {
$options = explode('|', self::$OPTIONS['__JSON_OPTIONS__']);
$opt = 0;
foreach ($options as $option) {
$opt |= constant($option);
}
return json_encode($fixture, $opt);
} else {
return json_encode($fixture);
}
}

/**
Expand Down

0 comments on commit 6bdddab

Please sign in to comment.