@@ -7,18 +7,23 @@ class JsonValueReplace
7
7
{
8
8
private $ search ;
9
9
private $ replace ;
10
+ private $ pathFilterRegex ;
10
11
private $ path = '' ;
11
12
private $ pathItems = array ();
12
13
14
+ public $ affectedPaths = array ();
15
+
13
16
/**
14
17
* JsonReplace constructor.
15
18
* @param mixed $search
16
19
* @param mixed $replace
20
+ * @param null|string $pathFilter Regular expression to check path
17
21
*/
18
- public function __construct ($ search , $ replace )
22
+ public function __construct ($ search , $ replace, $ pathFilter = null )
19
23
{
20
24
$ this ->search = $ search ;
21
25
$ this ->replace = $ replace ;
26
+ $ this ->pathFilterRegex = $ pathFilter ;
22
27
}
23
28
24
29
/**
@@ -28,15 +33,28 @@ public function __construct($search, $replace)
28
33
*/
29
34
public function process ($ data )
30
35
{
36
+ $ check = true ;
37
+ if ($ this ->pathFilterRegex && !preg_match ($ this ->pathFilterRegex , $ this ->path )) {
38
+ $ check = false ;
39
+ }
40
+
31
41
if (!is_array ($ data ) && !is_object ($ data )) {
32
- return $ data === $ this ->search ? $ this ->replace : $ data ;
42
+ if ($ check && $ data === $ this ->search ) {
43
+ $ this ->affectedPaths [] = $ this ->path ;
44
+ return $ this ->replace ;
45
+ } else {
46
+ return $ data ;
47
+ }
33
48
}
34
49
35
50
$ originalKeys = $ data instanceof \stdClass ? get_object_vars ($ data ) : $ data ;
36
51
37
- $ diff = new JsonDiff ($ data , $ this ->search , JsonDiff::STOP_ON_DIFF );
38
- if ($ diff ->getDiffCnt () === 0 ) {
39
- return $ this ->replace ;
52
+ if ($ check ) {
53
+ $ diff = new JsonDiff ($ data , $ this ->search , JsonDiff::STOP_ON_DIFF );
54
+ if ($ diff ->getDiffCnt () === 0 ) {
55
+ $ this ->affectedPaths [] = $ this ->path ;
56
+ return $ this ->replace ;
57
+ }
40
58
}
41
59
42
60
$ result = array ();
0 commit comments