@@ -81,6 +81,20 @@ abstract class AbstractElement
81
81
*/
82
82
protected $ elements = array ();
83
83
84
+ /**
85
+ * Index of element in the elements collection (start with 1)
86
+ *
87
+ * @var integer
88
+ */
89
+ protected $ elementIndex = 1 ;
90
+
91
+ /**
92
+ * Unique Id for element
93
+ *
94
+ * @var integer
95
+ */
96
+ protected $ elementId ;
97
+
84
98
/**
85
99
* Relation Id
86
100
*
@@ -108,7 +122,7 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null)
108
122
$ text = String::toUTF8 ($ text );
109
123
$ textObject = new Text ($ text , $ fontStyle , $ paragraphStyle );
110
124
$ textObject ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
111
- $ this ->elements [] = $ textObject ;
125
+ $ this ->addElement ( $ textObject) ;
112
126
113
127
return $ textObject ;
114
128
}
@@ -125,7 +139,7 @@ public function addTextRun($paragraphStyle = null)
125
139
126
140
$ textRun = new TextRun ($ paragraphStyle );
127
141
$ textRun ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
128
- $ this ->elements [] = $ textRun ;
142
+ $ this ->addElement ( $ textRun) ;
129
143
130
144
return $ textRun ;
131
145
}
@@ -148,7 +162,7 @@ public function addLink($linkSrc, $linkName = null, $fontStyle = null, $paragrap
148
162
$ link ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
149
163
$ rId = Media::addElement ($ elementDocPart , 'link ' , $ linkSrc );
150
164
$ link ->setRelationId ($ rId );
151
- $ this ->elements [] = $ link ;
165
+ $ this ->addElement ( $ link) ;
152
166
153
167
return $ link ;
154
168
}
@@ -179,7 +193,7 @@ public function addTitle($text, $depth = 1)
179
193
$ bookmarkId = $ data [1 ];
180
194
$ title ->setAnchor ($ anchor );
181
195
$ title ->setBookmarkId ($ bookmarkId );
182
- $ this ->elements [] = $ title ;
196
+ $ this ->addElement ( $ title) ;
183
197
184
198
return $ title ;
185
199
}
@@ -198,7 +212,7 @@ public function addPreserveText($text, $fontStyle = null, $paragraphStyle = null
198
212
199
213
$ preserveText = new PreserveText (String::toUTF8 ($ text ), $ fontStyle , $ paragraphStyle );
200
214
$ preserveText ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
201
- $ this ->elements [] = $ preserveText ;
215
+ $ this ->addElement ( $ preserveText) ;
202
216
203
217
return $ preserveText ;
204
218
}
@@ -217,7 +231,7 @@ public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = nu
217
231
for ($ i = 1 ; $ i <= $ count ; $ i ++) {
218
232
$ textBreak = new TextBreak ($ fontStyle , $ paragraphStyle );
219
233
$ textBreak ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
220
- $ this ->elements [] = $ textBreak ;
234
+ $ this ->addElement ( $ textBreak) ;
221
235
}
222
236
}
223
237
@@ -237,7 +251,7 @@ public function addListItem($text, $depth = 0, $fontStyle = null, $styleList = n
237
251
238
252
$ listItem = new ListItem (String::toUTF8 ($ text ), $ depth , $ fontStyle , $ styleList , $ paragraphStyle );
239
253
$ listItem ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
240
- $ this ->elements [] = $ listItem ;
254
+ $ this ->addElement ( $ listItem) ;
241
255
242
256
return $ listItem ;
243
257
}
@@ -253,7 +267,7 @@ public function addTable($style = null)
253
267
$ this ->checkValidity ('table ' );
254
268
255
269
$ table = new Table ($ this ->getDocPart (), $ this ->getDocPartId (), $ style );
256
- $ this ->elements [] = $ table ;
270
+ $ this ->addElement ( $ table) ;
257
271
258
272
return $ table ;
259
273
}
@@ -275,7 +289,8 @@ public function addImage($src, $style = null, $isWatermark = false)
275
289
$ image ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
276
290
$ rId = Media::addElement ($ elementDocPart , 'image ' , $ src , $ image );
277
291
$ image ->setRelationId ($ rId );
278
- $ this ->elements [] = $ image ;
292
+ $ this ->addElement ($ image );
293
+
279
294
return $ image ;
280
295
}
281
296
@@ -307,7 +322,8 @@ public function addObject($src, $style = null)
307
322
$ object ->setRelationId ($ rId );
308
323
$ rIdimg = Media::addElement ($ elementDocPart , 'image ' , $ icon , new Image ($ icon ));
309
324
$ object ->setImageRelationId ($ rIdimg );
310
- $ this ->elements [] = $ object ;
325
+ $ this ->addElement ($ object );
326
+
311
327
return $ object ;
312
328
} else {
313
329
throw new InvalidObjectException ();
@@ -329,7 +345,7 @@ public function addFootnote($paragraphStyle = null)
329
345
330
346
$ footnote ->setDocPart ('footnote ' , $ this ->getDocPartId ());
331
347
$ footnote ->setRelationId ($ rId );
332
- $ this ->elements [] = $ footnote ;
348
+ $ this ->addElement ( $ footnote) ;
333
349
334
350
return $ footnote ;
335
351
}
@@ -349,7 +365,7 @@ public function addEndnote($paragraphStyle = null)
349
365
350
366
$ endnote ->setDocPart ('endnote ' , $ this ->getDocPartId ());
351
367
$ endnote ->setRelationId ($ rId );
352
- $ this ->elements [] = $ endnote ;
368
+ $ this ->addElement ( $ endnote) ;
353
369
354
370
return $ endnote ;
355
371
}
@@ -369,7 +385,7 @@ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = n
369
385
370
386
$ checkBox = new CheckBox (String::toUTF8 ($ name ), String::toUTF8 ($ text ), $ fontStyle , $ paragraphStyle );
371
387
$ checkBox ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
372
- $ this ->elements [] = $ checkBox ;
388
+ $ this ->addElement ( $ checkBox) ;
373
389
374
390
return $ checkBox ;
375
391
}
@@ -416,6 +432,16 @@ public function getDocPartId()
416
432
return $ this ->docPartId ;
417
433
}
418
434
435
+ /**
436
+ * Set element index and unique id, and add element into elements collection
437
+ */
438
+ protected function addElement (AbstractElement $ element )
439
+ {
440
+ $ element ->setElementIndex ($ this ->countElements () + 1 );
441
+ $ element ->setElementId ();
442
+ $ this ->elements [] = $ element ;
443
+ }
444
+
419
445
/**
420
446
* Get all elements
421
447
*
@@ -426,6 +452,54 @@ public function getElements()
426
452
return $ this ->elements ;
427
453
}
428
454
455
+ /**
456
+ * Count elements
457
+ *
458
+ * @return integer
459
+ */
460
+ public function countElements ()
461
+ {
462
+ return count ($ this ->elements );
463
+ }
464
+
465
+ /**
466
+ * Get element index
467
+ *
468
+ * @return int
469
+ */
470
+ public function getElementIndex ()
471
+ {
472
+ return $ this ->elementIndex ;
473
+ }
474
+
475
+ /**
476
+ * Set element index
477
+ *
478
+ * @param int $value
479
+ */
480
+ public function setElementIndex ($ value )
481
+ {
482
+ $ this ->elementIndex = $ value ;
483
+ }
484
+
485
+ /**
486
+ * Get element unique ID
487
+ *
488
+ * @return string
489
+ */
490
+ public function getElementId ()
491
+ {
492
+ return $ this ->elementId ;
493
+ }
494
+
495
+ /**
496
+ * Set element unique ID from 6 first digit of md5
497
+ */
498
+ public function setElementId ()
499
+ {
500
+ $ this ->elementId = substr (md5 (rand ()), 0 , 6 );
501
+ }
502
+
429
503
/**
430
504
* Get relation Id
431
505
*
0 commit comments