-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobject.php
executable file
·163 lines (153 loc) · 3.75 KB
/
object.php
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
<?php
require_once('properties.php');
class ObjectBase {
//attributes
public $name='';
public $type='';
public $x=0;
public $y=0;
public $width=0;
public $height=0;
public $gid=NULL;
public $visible=1;
public $ellipse=false;
public $polygon=false;
public $polyline=false;
public $rotation=0;//deg clockwise
public $points=array();
//methods
public function load_from_element(SimpleXMLElement $xml, $ref='') {
$this->name=(string)$xml['name'];
$this->type=(string)$xml['type'];
$this->x=(int)$xml['x'];
$this->y=(int)$xml['y'];
$this->width=(int)$xml['width'];
$this->height=(int)$xml['height'];
if((bool)$xml['gid']!==false) {
$this->gid=(int)$xml['gid'];
}
$this->visible=(int)$xml['visible'];
$this->rotation=(int)$xml['rotation'];
if((bool)$xml->ellipse!==false) {
$this->ellipse=true;
}
if((bool)$xml->polygon!==false) {
$this->polygon=true;
}
if((bool)$xml->polyline!==false) {
$this->polyline=true;
}
if($this->ellipse + $this->polygon + $this->polyline > 1) trigger_error('ERROR : ellipse & polygon & polyline.', E_USER_ERROR);
if($this->polygon||$this->polyline) {
$p='';
if((bool)$xml->polygon['points']!==false) {
$p=strtok($xml->polygon['points'],', ');
}
if((bool)$xml->polyline['points']!==false) {
$p=strtok($xml->polyline['points'],', ');
}
assert($p!='');
do {
$this->points[]=$p;
$p=strtok(', ');
}
while($p!==false);
assert(count($this->points)%2==0);
}
if((bool)$xml->properties!==false) {
$this->loadProperties_from_element($xml->properties, $ref);
}
}
public function getWidthL() {
$w=0;
for($i=0;$i<count($this->points);$i+=2) {
if($this->points[$i]<$w) {
$w=$this->points[$i];
}
}
return abs($w);
}
public function getWidthR() {
$w=0;
for($i=0;$i<count($this->points);$i+=2) {
if($this->points[$i]>$w) {
$w=$this->points[$i];
}
}
return $w;
}
public function getHeightT() {
$h=0;
for($i=1;$i<count($this->points);$i+=2) {
if($this->points[$i]<$h) {
$h=$this->points[$i];
}
}
return abs($h);
}
public function getHeightB() {
$h=0;
for($i=1;$i<count($this->points);$i+=2) {
if($this->points[$i]>$h) {
$h=$this->points[$i];
}
}
return $h;
}
public function isValid() {
if(!is_string($this->name)) {
throw new Exception('Incorrect object name value.');
return false;
}
if(!is_string($this->type)) {
throw new Exception('Incorrect object type value.');
return false;
}
if(!is_int($this->x)) {
throw new Exception('Incorrect object x value.');
return false;
}
if(!is_int($this->y)) {
throw new Exception('Incorrect object y value.');
return false;
}
if(!is_int($this->width)) {
throw new Exception('Incorrect object width value.');
return false;
}
if(!is_int($this->height)) {
throw new Exception('Incorrect object height value.');
return false;
}
if(!is_null($this->gid) && !is_int($this->gid)) {
throw new Exception('Incorrect object gid value.');
return false;
}
if(!is_int($this->rotation)) {
throw new Exception('Incorrect object rotation value.');
return false;
}
if(!is_int($this->visible) || ($this->visible!=0 && $this->visible!=1)) {
throw new Exception('Incorrect object visible value.');
return false;
}
if(!is_bool($this->ellipse)) {
throw new Exception('Incorrect object ellipse node.');
return false;
}
if(!is_bool($this->polygon)) {
throw new Exception('Incorrect object polygon node.');
return false;
}
if(!is_bool($this->polyline)) {
throw new Exception('Incorrect object polyline node.');
return false;
}
if(!is_array($this->points) || count($this->points)%2!=0) {
throw new Exception('Incorrect object points list.');
return false;
}
return true;
}
};
?>