-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGenJson.php
66 lines (55 loc) · 2.16 KB
/
GenJson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Swaggest\JsonCli;
use Swaggest\JsonCli\GenPhp\BuilderOptions;
use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchemaMaker\InstanceFaker;
use Swaggest\JsonSchemaMaker\Options;
use Yaoi\Command;
class GenJson extends Base
{
public $maxNesting = 10;
public $defaultAdditionalProperties;
public $randSeed;
use BuilderOptions;
/**
* @param Command\Definition $definition
* @param \stdClass|static $options
*/
public static function setUpDefinition(Command\Definition $definition, $options)
{
$definition->description = 'Generate JSON sample from JSON schema';
Base::setupGenOptions($definition, $options);
$options->maxNesting = Command\Option::create()->setType()
->setDescription('Max nesting level, default 10');
$options->defaultAdditionalProperties = Command\Option::create()
->setDescription('Treat non-existent `additionalProperties` as `additionalProperties: true`');
$options->randSeed = Command\Option::create()->setType()
->setDescription('Integer random seed for deterministic output');
Base::setUpDefinition($definition, $options);
Base::setupLoadFileOptions($options);
}
public function performAction()
{
if ($this->randSeed !== null) {
mt_srand((int)$this->randSeed);
}
try {
$skipRoot = false;
$baseName = null;
$schema = $this->loadSchema($skipRoot, $baseName);
if (!$schema instanceof Schema) {
$this->response->error('failed to assert Schema type, ' . get_class($schema) . ' received');
throw new ExitCode('', 1);
}
$options = new Options;
$options->maxNesting = $this->maxNesting;
$options->defaultAdditionalProperties = $this->defaultAdditionalProperties;
$instanceFaker = new InstanceFaker($schema, $options);
$this->out = $instanceFaker->makeValue();
$this->postPerform();
} catch (\Exception $e) {
$this->response->error($e->getMessage());
throw new ExitCode('', 1);
}
}
}