AbstractCollection.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Collection;
  18. /**
  19. * Collection abstract class
  20. *
  21. * @since 0.10.0
  22. */
  23. abstract class AbstractCollection
  24. {
  25. /**
  26. * Items
  27. *
  28. * @var \PhpOffice\PhpWord\Element\AbstractContainer[]
  29. */
  30. private $items = array();
  31. /**
  32. * Get items
  33. *
  34. * @return \PhpOffice\PhpWord\Element\AbstractContainer[]
  35. */
  36. public function getItems()
  37. {
  38. return $this->items;
  39. }
  40. /**
  41. * Get item by index
  42. *
  43. * @param int $index
  44. * @return \PhpOffice\PhpWord\Element\AbstractContainer
  45. */
  46. public function getItem($index)
  47. {
  48. if (array_key_exists($index, $this->items)) {
  49. return $this->items[$index];
  50. }
  51. return null;
  52. }
  53. /**
  54. * Set item.
  55. *
  56. * @param int $index
  57. * @param \PhpOffice\PhpWord\Element\AbstractContainer $item
  58. */
  59. public function setItem($index, $item)
  60. {
  61. if (array_key_exists($index, $this->items)) {
  62. $this->items[$index] = $item;
  63. }
  64. }
  65. /**
  66. * Add new item
  67. *
  68. * @param \PhpOffice\PhpWord\Element\AbstractContainer $item
  69. * @return int
  70. */
  71. public function addItem($item)
  72. {
  73. $index = $this->countItems() + 1;
  74. $this->items[$index] = $item;
  75. return $index;
  76. }
  77. /**
  78. * Get item count
  79. *
  80. * @return int
  81. */
  82. public function countItems()
  83. {
  84. return count($this->items);
  85. }
  86. }