-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIndex.class.php
More file actions
executable file
·73 lines (65 loc) · 1.71 KB
/
ArrayIndex.class.php
File metadata and controls
executable file
·73 lines (65 loc) · 1.71 KB
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
71
72
73
<?php namespace text\json\patch;
class ArrayIndex extends Address {
private $pos;
/**
* Creates a array index address
*
* @param int $pos
* @param parent $parent
*/
public function __construct($pos, parent $parent) {
$this->pos= $pos;
is_object($parent->reference) && $parent->reference= (array)$parent->reference;
if (is_array($parent->reference) && array_key_exists($this->pos, $parent->reference)) {
parent::__construct($parent->reference[$this->pos], $parent);
} else {
parent::__construct(self::$null, $parent, false);
}
}
/** @return string */
public function token() { return '/'.$this->pos; }
/**
* Modify this address
*
* @param var $value
* @return text.json.patch.Applied
*/
public function modify($value) {
if ($this->exists) {
$this->reference= $value;
return Applied::$CLEANLY;
} else {
return new PathDoesNotExist($this->path());
}
}
/**
* Removes this address
*
* @return text.json.patch.Applied
*/
public function remove() {
if ($this->exists) {
array_splice($this->parent->reference, $this->pos, 1);
return Applied::$CLEANLY;
} else {
return new PathDoesNotExist($this->path());
}
}
/**
* Add to this address
*
* @param var $value
* @return text.json.patch.Applied
*/
public function add($value) {
if ($this->parent->exists) {
if ($this->pos < 0 || $this->pos > sizeof($this->parent->reference)) {
return new ArrayIndexOutOfBounds($this->pos);
}
array_splice($this->parent->reference, $this->pos, 0, [$value]);
return Applied::$CLEANLY;
} else {
return new PathDoesNotExist($this->path());
}
}
}