No Description

AbstractCollection.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  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 array
  29. */
  30. private $items = array();
  31. /**
  32. * Get items
  33. *
  34. * @return array
  35. */
  36. public function getItems()
  37. {
  38. return $this->items;
  39. }
  40. /**
  41. * Get item by index
  42. *
  43. * @param int $index
  44. * @return mixed
  45. */
  46. public function getItem($index)
  47. {
  48. if (array_key_exists($index, $this->items)) {
  49. return $this->items[$index];
  50. } else {
  51. return null;
  52. }
  53. }
  54. /**
  55. * Set item.
  56. *
  57. * @param int $index
  58. * @param mixed $item
  59. * @return void
  60. */
  61. public function setItem($index, $item)
  62. {
  63. if (array_key_exists($index, $this->items)) {
  64. $this->items[$index] = $item;
  65. }
  66. }
  67. /**
  68. * Add new item
  69. *
  70. * @param mixed $item
  71. * @return int
  72. */
  73. public function addItem($item)
  74. {
  75. $index = $this->countItems() + 1;
  76. $this->items[$index] = $item;
  77. return $index;
  78. }
  79. /**
  80. * Get item count
  81. *
  82. * @return int
  83. */
  84. public function countItems()
  85. {
  86. return count($this->items);
  87. }
  88. }