ListInterface.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. /**
  4. * Interface for a list of typed data.
  5. *
  6. * A list of typed data contains only items of the same type, is ordered and may
  7. * contain duplicates. Note that the data type of a list is always 'list'.
  8. *
  9. * When implementing this interface which extends Traversable, make sure to list
  10. * IteratorAggregate or Iterator before this interface in the implements clause.
  11. *
  12. * @see \Drupal\Core\TypedData\ListDefinitionInterface
  13. *
  14. * @ingroup typed_data
  15. */
  16. interface ListInterface extends TraversableTypedDataInterface, \ArrayAccess, \Countable {
  17. /**
  18. * Gets the data definition.
  19. *
  20. * @return \Drupal\Core\TypedData\ListDataDefinitionInterface
  21. * The data definition object describing the list.
  22. */
  23. public function getDataDefinition();
  24. /**
  25. * Determines whether the list contains any non-empty items.
  26. *
  27. * @return bool
  28. * TRUE if the list is empty, FALSE otherwise.
  29. */
  30. public function isEmpty();
  31. /**
  32. * Gets the definition of a contained item.
  33. *
  34. * @return \Drupal\Core\TypedData\DataDefinitionInterface
  35. * The data definition of contained items.
  36. */
  37. public function getItemDefinition();
  38. /**
  39. * Returns the item at the specified position in this list.
  40. *
  41. * @param int $index
  42. * Index of the item to return.
  43. *
  44. * @return \Drupal\Core\TypedData\TypedDataInterface|null
  45. * The item at the specified position in this list, or NULL if no item
  46. * exists at that position.
  47. *
  48. * @throws \Drupal\Core\TypedData\Exception\MissingDataException
  49. * If the complex data structure is unset and no item can be created.
  50. */
  51. public function get($index);
  52. /**
  53. * Sets the value of the item at a given position in the list.
  54. *
  55. * @param int $index
  56. * The position of the item in the list. Since a List only contains
  57. * sequential, 0-based indexes, $index has to be:
  58. * - Either the position of an existing item in the list. This updates the
  59. * item value.
  60. * - Or the next available position in the sequence of the current list
  61. * indexes. This appends a new item with the provided value at the end of
  62. * the list.
  63. * @param mixed $value
  64. * The value of the item to be stored at the specified position.
  65. *
  66. * @return $this
  67. *
  68. * @throws \InvalidArgumentException
  69. * If the $index is invalid (non-numeric, or pointing to an invalid
  70. * position in the list).
  71. * @throws \Drupal\Core\TypedData\Exception\MissingDataException
  72. * If the complex data structure is unset and no item can be set.
  73. */
  74. public function set($index, $value);
  75. /**
  76. * Returns the first item in this list.
  77. *
  78. * @return \Drupal\Core\TypedData\TypedDataInterface
  79. * The first item in this list.
  80. *
  81. * @throws \Drupal\Core\TypedData\Exception\MissingDataException
  82. * If the complex data structure is unset and no item can be created.
  83. */
  84. public function first();
  85. /**
  86. * Appends a new item to the list.
  87. *
  88. * @param mixed $value
  89. * The value of the new item.
  90. *
  91. * @return \Drupal\Core\TypedData\TypedDataInterface
  92. * The item that was appended.
  93. */
  94. public function appendItem($value = NULL);
  95. /**
  96. * Removes the item at the specified position.
  97. *
  98. * @param int $index
  99. * Index of the item to remove.
  100. *
  101. * @return $this
  102. */
  103. public function removeItem($index);
  104. /**
  105. * Filters the items in the list using a custom callback.
  106. *
  107. * @param callable $callback
  108. * The callback to use for filtering. Like with array_filter(), the
  109. * callback is called for each item in the list. Only items for which the
  110. * callback returns TRUE are preserved.
  111. *
  112. * @return $this
  113. */
  114. public function filter($callback);
  115. }