-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathApply.php
70 lines (60 loc) · 2 KB
/
Apply.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
67
68
69
70
<?php
namespace Swaggest\JsonCli;
use Swaggest\JsonDiff\Exception;
use Swaggest\JsonDiff\JsonMergePatch;
use Swaggest\JsonDiff\JsonPatch;
use Yaoi\Command;
use Yaoi\Command\Definition;
class Apply extends Base
{
/** @var string */
public $patchPath;
/** @var string */
public $basePath;
/** @var bool */
public $tolerateErrors;
/** @var bool */
public $merge;
/**
* @param Definition $definition
* @param \stdClass|static $options
*/
static function setUpDefinition(Definition $definition, $options)
{
$options->patchPath = Command\Option::create()->setType()->setIsUnnamed()
->setDescription('Path to JSON patch file');
$options->basePath = Command\Option::create()->setType()->setIsUnnamed()
->setDescription('Path to JSON base file');
parent::setUpDefinition($definition, $options);
$definition->description = 'Apply patch to base json document, output to STDOUT';
$options->tolerateErrors = Command\Option::create()
->setDescription('Continue on error');
$options->merge = Command\Option::create()
->setDescription('Use merge patch (RFC 7386)');
}
/**
* @throws ExitCode
*/
public function performAction()
{
$patchJson = $this->readData($this->patchPath);
$base = $this->readData($this->basePath);
try {
if ($this->merge) {
JsonMergePatch::apply($base, $patchJson);
$this->out = $base;
} else {
$patch = JsonPatch::import($patchJson);
$errors = $patch->apply($base, !$this->tolerateErrors);
foreach ($errors as $error) {
$this->response->error($error->getMessage());
}
$this->out = $base;
}
} catch (Exception $e) {
$this->response->error($e->getMessage());
throw new ExitCode('', 1);
}
$this->postPerform();
}
}