wpml-st-string-dependencies-node.php
3.55 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
class WPML_ST_String_Dependencies_Node {
/** @var WPML_ST_String_Dependencies_Node|null $parent */
private $parent;
/** @var WPML_ST_String_Dependencies_Node[] $children */
private $children = array();
/** @var bool $iteration_completed */
private $iteration_completed = false;
/** @var int|null $id */
private $id;
/** @var string|null $type */
private $type;
/** @var bool|null $needs_refresh */
private $needs_refresh;
public function __construct( $id = null, $type = null ) {
$this->id = $id;
$this->type = $type;
}
public function get_id() {
return $this->id;
}
public function get_type() {
return $this->type;
}
public function set_needs_refresh( $needs_refresh ) {
$this->needs_refresh = $needs_refresh;
}
public function get_needs_refresh() {
return $this->needs_refresh;
}
public function set_parent( WPML_ST_String_Dependencies_Node $node ) {
$node->add_child( $this );
$this->parent = $node;
}
public function get_parent() {
return $this->parent;
}
public function add_child( WPML_ST_String_Dependencies_Node $node ) {
if ( ! isset( $this->children[ $node->get_hash() ] ) ) {
$this->children[ $node->get_hash() ] = $node;
$node->set_parent( $this );
}
}
public function remove_child( WPML_ST_String_Dependencies_Node $node ) {
unset( $this->children[ $node->get_hash() ] );
unset( $node );
}
public function detach() {
if ( $this->parent ) {
$this->parent->remove_child( $this );
}
}
/**
* Iteration DFS in post-order
*
* @return WPML_ST_String_Dependencies_Node
*/
public function get_next() {
if ( $this->children ) {
$first_child = reset( $this->children );
if ( $first_child ) {
/** @var WPML_ST_String_Dependencies_Node $first_child */
return $first_child->get_next();
}
}
if ( ! $this->parent && ! $this->iteration_completed ) {
$this->iteration_completed = true;
}
return $this;
}
/**
* Search DFS in pre-order
*
* @param int $id
* @param string $type
*
* @return false|WPML_ST_String_Dependencies_Node
*/
public function search( $id, $type ) {
if ( $this->id === $id && $this->type === $type ) {
return $this;
}
if ( $this->children ) {
foreach ( $this->children as $child ) {
$node = $child->search( $id, $type );
if ( $node ) {
return $node;
}
}
}
return false;
}
public function iteration_completed() {
return $this->iteration_completed;
}
/**
* @return string|stdClass
*/
public function to_json() {
$object = new stdClass();
foreach ( $this->get_item_properties() as $property ) {
if ( null !== $this->{$property} ) {
$object->{$property} = $this->{$property};
}
}
if ( $this->children ) {
foreach ( $this->children as $child ) {
$object->children[] = $child->to_json();
}
}
if ( ! $this->parent ) {
return json_encode( $object );
}
return $object;
}
/**
* @param string|self $object
*/
public function from_json( $object ) {
if ( is_string( $object ) ) {
$object = json_decode( $object );
}
foreach ( $this->get_item_properties() as $property ) {
if ( isset( $object->{$property} ) ) {
$this->{$property} = $object->{$property};
}
}
if ( isset( $object->children ) ) {
foreach ( $object->children as $child ) {
$child_node = new self( $child->id, $child->type );
$child_node->from_json( $child );
$child_node->set_parent( $this );
}
}
}
private function get_item_properties() {
return array( 'id', 'type', 'needs_refresh' );
}
private function get_hash() {
return spl_object_hash( $this );
}
}