Skip to content

Commit 93dcfb0

Browse files
committed
Issue #4: replacing document root with add() fixed
1 parent d289679 commit 93dcfb0

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Fixed
9+
- Issue #3: replacing document root with `add()` fixed.
10+
711
## [0.6.2] - 2019-11-18
812
### Fixed
913
- Issue #3: adding element to empty array fixed.

src/Processor/Processor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ public function add(QueryInterface $query, NodeValueInterface $rootNode, NodeVal
139139

140140
private function createAddMutation(QueryResultInterface $queryResult, NodeValueInterface $value): ?MutationInterface
141141
{
142-
if (!$queryResult->hasParent() || !$queryResult->hasLastReference()) {
142+
if (!$queryResult->hasParent()) {
143+
return $queryResult->hasSelection()
144+
? new ReplaceMutation($value, $queryResult->getSelection()->getPath())
145+
: null;
146+
}
147+
if (!$queryResult->hasLastReference()) {
143148
return null;
144149
}
145150
$parent = $queryResult->getParent();

tests/AcceptanceTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public function providerAdd(): array
108108
return [
109109
'Add element to empty array' => ['[]', '/0', '1', '[1]'],
110110
'Add property to empty object' => ['{}', '/a', '"b"', '{"a":"b"}'],
111+
'Replace root array with object' => ['[1]', '', '{"a":"b"}', '{"a":"b"}'],
112+
'Replace root scalar' => ['1', '', '"a"', '"a"'],
113+
'Insert array element' => ['[1,2]', '/1', '3', '[1,3,2]'],
114+
'Append array element by index' => ['[1,2]', '/2', '3', '[1,2,3]'],
115+
'Auto-append array element' => ['[1,2]', '/-', '3', '[1,2,3]'],
111116
];
112117
}
113118
}

0 commit comments

Comments
 (0)