/vendor/phpdocumentor/type-resolver/src/Types/Compound.php

https://bitbucket.org/alan_cordova/api-sb-map · PHP · 93 lines · 37 code · 10 blank · 46 comment · 2 complexity · 85c30d1a61a722170a6ada8b52e5dad2 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of phpDocumentor.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
  9. * @license http://www.opensource.org/licenses/mit-license.php MIT
  10. * @link http://phpdoc.org
  11. */
  12. namespace phpDocumentor\Reflection\Types;
  13. use ArrayIterator;
  14. use IteratorAggregate;
  15. use phpDocumentor\Reflection\Type;
  16. /**
  17. * Value Object representing a Compound Type.
  18. *
  19. * A Compound Type is not so much a special keyword or object reference but is a series of Types that are separated
  20. * using an OR operator (`|`). This combination of types signifies that whatever is associated with this compound type
  21. * may contain a value with any of the given types.
  22. */
  23. final class Compound implements Type, IteratorAggregate
  24. {
  25. /** @var Type[] */
  26. private $types;
  27. /**
  28. * Initializes a compound type (i.e. `string|int`) and tests if the provided types all implement the Type interface.
  29. *
  30. * @param Type[] $types
  31. * @throws \InvalidArgumentException when types are not all instance of Type
  32. */
  33. public function __construct(array $types)
  34. {
  35. foreach ($types as $type) {
  36. if (!$type instanceof Type) {
  37. throw new \InvalidArgumentException('A compound type can only have other types as elements');
  38. }
  39. }
  40. $this->types = $types;
  41. }
  42. /**
  43. * Returns the type at the given index.
  44. *
  45. * @param integer $index
  46. *
  47. * @return Type|null
  48. */
  49. public function get($index)
  50. {
  51. if (!$this->has($index)) {
  52. return null;
  53. }
  54. return $this->types[$index];
  55. }
  56. /**
  57. * Tests if this compound type has a type with the given index.
  58. *
  59. * @param integer $index
  60. *
  61. * @return bool
  62. */
  63. public function has($index)
  64. {
  65. return isset($this->types[$index]);
  66. }
  67. /**
  68. * Returns a rendered output of the Type as it would be used in a DocBlock.
  69. *
  70. * @return string
  71. */
  72. public function __toString()
  73. {
  74. return implode('|', $this->types);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getIterator()
  80. {
  81. return new ArrayIterator($this->types);
  82. }
  83. }