1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonDiff\Exception; |
6
|
|
|
use Swaggest\JsonDiff\JsonMergePatch; |
7
|
|
|
use Swaggest\JsonDiff\JsonPatch; |
8
|
|
|
use Yaoi\Command; |
9
|
|
|
use Yaoi\Command\Definition; |
10
|
|
|
|
11
|
|
|
class Apply extends Base |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
public $patchPath; |
15
|
|
|
/** @var string */ |
16
|
|
|
public $basePath; |
17
|
|
|
/** @var bool */ |
18
|
|
|
public $tolerateErrors; |
19
|
|
|
/** @var bool */ |
20
|
|
|
public $merge; |
21
|
1 |
|
|
22
|
|
|
/** |
23
|
1 |
|
* @param Definition $definition |
24
|
1 |
|
* @param \stdClass|static $options |
25
|
1 |
|
*/ |
26
|
1 |
|
static function setUpDefinition(Definition $definition, $options) |
27
|
|
|
{ |
28
|
1 |
|
$options->patchPath = Command\Option::create()->setType()->setIsUnnamed() |
|
|
|
|
29
|
|
|
->setDescription('Path to JSON patch file'); |
30
|
1 |
|
$options->basePath = Command\Option::create()->setType()->setIsUnnamed() |
|
|
|
|
31
|
1 |
|
->setDescription('Path to JSON base file'); |
32
|
1 |
|
|
33
|
1 |
|
parent::setUpDefinition($definition, $options); |
34
|
|
|
|
35
|
1 |
|
$definition->description = 'Apply patch to base json document, output to STDOUT'; |
36
|
|
|
$options->tolerateErrors = Command\Option::create() |
|
|
|
|
37
|
1 |
|
->setDescription('Continue on error'); |
38
|
1 |
|
$options->merge = Command\Option::create() |
|
|
|
|
39
|
|
|
->setDescription('Use merge patch (RFC 7386)'); |
40
|
|
|
} |
41
|
1 |
|
|
42
|
1 |
|
/** |
43
|
1 |
|
* @throws ExitCode |
44
|
|
|
*/ |
45
|
|
|
public function performAction() |
46
|
1 |
|
{ |
47
|
|
|
$patchJson = $this->readData($this->patchPath); |
48
|
|
|
$base = $this->readData($this->basePath); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
if ($this->merge) { |
52
|
1 |
|
JsonMergePatch::apply($base, $patchJson); |
53
|
1 |
|
$this->out = $base; |
54
|
|
|
} else { |
55
|
|
|
$patch = JsonPatch::import($patchJson); |
56
|
|
|
$errors = $patch->apply($base, !$this->tolerateErrors); |
57
|
|
|
foreach ($errors as $error) { |
58
|
|
|
$this->response->error($error->getMessage()); |
59
|
|
|
} |
60
|
|
|
$this->out = $base; |
61
|
|
|
} |
62
|
|
|
} catch (Exception $e) { |
63
|
|
|
$this->response->error($e->getMessage()); |
64
|
|
|
throw new ExitCode('', 1); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->postPerform(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..