1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenMindParser\Models; |
4
|
|
|
|
5
|
|
|
use \DOMDocument; |
6
|
|
|
|
7
|
|
|
/*Object that represent the document itself. It is the entry point of the objects tree with all nodes.*/ |
8
|
|
|
class Document |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var String $path : The file path in the filesystem |
12
|
|
|
*/ |
13
|
|
|
private $path; |
14
|
|
|
/** |
15
|
|
|
* @var String $name : The file name (basename of the path). |
16
|
|
|
*/ |
17
|
|
|
private $name; |
18
|
|
|
/** |
19
|
|
|
* @var int $size : The file size. |
20
|
|
|
*/ |
21
|
|
|
private $size; |
22
|
|
|
/** |
23
|
|
|
* @var DOMDocument $domDocument : The DOMDocument instance of the loaded file. |
24
|
|
|
*/ |
25
|
|
|
private $domDocument; |
26
|
|
|
/** |
27
|
|
|
* @var Node $rootNode : The Node instance of the first Node of the file. |
28
|
|
|
*/ |
29
|
|
|
private $rootNode; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Perform usual checks over the given path and if everything is fine, fill class attributes and build tree nodes objects. |
33
|
|
|
* |
34
|
|
|
* @param String $path : Path's file to open. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(DOMDocument $domDocument = null, Node $rootNode = null, $path = null, $name = null, $size = null) { |
37
|
|
|
$this->path = $path; |
38
|
|
|
$this->name = $name; |
39
|
|
|
$this->size = $size; |
40
|
|
|
$this->domDocument = $domDocument; |
41
|
|
|
$this->rootNode = $rootNode; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return the file path. |
46
|
|
|
* |
47
|
|
|
* @return String : The file path. |
48
|
|
|
*/ |
49
|
|
|
public function getPath() { |
50
|
|
|
return $this->path; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set the file path. |
55
|
|
|
* |
56
|
|
|
* @param String $path : The file path. |
57
|
|
|
*/ |
58
|
|
|
public function setPath($path) { |
59
|
|
|
$this->path = $path; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the file name. |
64
|
|
|
* |
65
|
|
|
* @return String : The file name. |
66
|
|
|
*/ |
67
|
|
|
public function getName() { |
68
|
|
|
return $this->name; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the file name. |
73
|
|
|
* |
74
|
|
|
* @param String $name : The file name. |
75
|
|
|
*/ |
76
|
|
|
public function setName($name) { |
77
|
|
|
$this->name = $name; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the file size. |
82
|
|
|
* |
83
|
|
|
* @return int : The file size. |
84
|
|
|
*/ |
85
|
|
|
public function getSize() { |
86
|
|
|
return $this->size; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the file size. |
91
|
|
|
* |
92
|
|
|
* @param int $size : The file size. |
93
|
|
|
*/ |
94
|
|
|
public function setSize($size) { |
95
|
|
|
$this->size = $size; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return the DOMDocument instance. |
100
|
|
|
* |
101
|
|
|
* @return DOMDocument : The DOMDocument instance. |
102
|
|
|
*/ |
103
|
|
|
public function getDomDocument() { |
104
|
|
|
return $this->domDocument; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the DOMDocument instance. |
109
|
|
|
* |
110
|
|
|
* @param DOMDocument $domDocument : The DOMDocument instance. |
111
|
|
|
*/ |
112
|
|
|
public function setDomDocument($domDocument) { |
113
|
|
|
$this->domDocument = $domDocument; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return the root Node instance. |
118
|
|
|
* |
119
|
|
|
* @return Node : The node instance. |
120
|
|
|
*/ |
121
|
|
|
public function getRootNode() { |
122
|
|
|
return $this->rootNode; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set the root Node instance. |
127
|
|
|
* |
128
|
|
|
* @param Node $rootNode : The node instance. |
129
|
|
|
*/ |
130
|
|
|
public function setRootNode($rootNode) { |
131
|
|
|
$this->rootNode = $rootNode; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* To String method. |
136
|
|
|
* |
137
|
|
|
* @return String : The name of the file. |
138
|
|
|
*/ |
139
|
|
|
public function __toString() { |
140
|
|
|
return $this->getName(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Transform the objects tree in an array tree. |
145
|
|
|
* |
146
|
|
|
* @return Array $array : The array tree. |
147
|
|
|
*/ |
148
|
|
|
public function toArray() { |
149
|
|
|
$array = []; |
150
|
|
|
$sorter = function($value, $key) use(&$array) { |
151
|
|
|
if($value instanceof Node) { |
152
|
|
|
$value = $value->toArray(); |
153
|
|
|
} elseif($value instanceof DOMDocument) { |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
$array[$key] = $value; |
157
|
|
|
}; |
158
|
|
|
|
159
|
|
|
foreach(get_object_vars($this) as $key => $value) { |
160
|
|
|
$sorter($value, $key); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $array; |
164
|
|
|
} |
165
|
|
|
} |