Link.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package;
  12. use Composer\Semver\Constraint\ConstraintInterface;
  13. /**
  14. * Represents a link between two packages, represented by their names
  15. *
  16. * @author Nils Adermann <naderman@naderman.de>
  17. */
  18. class Link
  19. {
  20. /**
  21. * @var string
  22. */
  23. protected $source;
  24. /**
  25. * @var string
  26. */
  27. protected $target;
  28. /**
  29. * @var ConstraintInterface|null
  30. */
  31. protected $constraint;
  32. /**
  33. * @var string
  34. */
  35. protected $description;
  36. /**
  37. * @var string|null
  38. */
  39. protected $prettyConstraint;
  40. /**
  41. * Creates a new package link.
  42. *
  43. * @param string $source
  44. * @param string $target
  45. * @param ConstraintInterface|null $constraint Constraint applying to the target of this link
  46. * @param string $description Used to create a descriptive string representation
  47. * @param string|null $prettyConstraint
  48. */
  49. public function __construct($source, $target, ConstraintInterface $constraint = null, $description = 'relates to', $prettyConstraint = null)
  50. {
  51. $this->source = strtolower($source);
  52. $this->target = strtolower($target);
  53. $this->constraint = $constraint;
  54. $this->description = $description;
  55. $this->prettyConstraint = $prettyConstraint;
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function getDescription()
  61. {
  62. return $this->description;
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function getSource()
  68. {
  69. return $this->source;
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function getTarget()
  75. {
  76. return $this->target;
  77. }
  78. /**
  79. * @return ConstraintInterface|null
  80. */
  81. public function getConstraint()
  82. {
  83. return $this->constraint;
  84. }
  85. /**
  86. * @throws \UnexpectedValueException If no pretty constraint was provided
  87. * @return string
  88. */
  89. public function getPrettyConstraint()
  90. {
  91. if (null === $this->prettyConstraint) {
  92. throw new \UnexpectedValueException(sprintf('Link %s has been misconfigured and had no prettyConstraint given.', $this));
  93. }
  94. return $this->prettyConstraint;
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function __toString()
  100. {
  101. return $this->source.' '.$this->description.' '.$this->target.' ('.$this->constraint.')';
  102. }
  103. /**
  104. * @param PackageInterface $sourcePackage
  105. * @return string
  106. */
  107. public function getPrettyString(PackageInterface $sourcePackage)
  108. {
  109. return $sourcePackage->getPrettyString().' '.$this->description.' '.$this->target.($this->constraint ? ' '.$this->constraint->getPrettyString() : '');
  110. }
  111. }