Link.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Package\LinkConstraint\LinkConstraintInterface;
  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 LinkConstraintInterface|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 LinkConstraintInterface|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, LinkConstraintInterface $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 getSource()
  61. {
  62. return $this->source;
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function getTarget()
  68. {
  69. return $this->target;
  70. }
  71. /**
  72. * @return LinkConstraintInterface|null
  73. */
  74. public function getConstraint()
  75. {
  76. return $this->constraint;
  77. }
  78. /**
  79. * @throws \UnexpectedValueException If no pretty constraint was provided
  80. * @return string
  81. */
  82. public function getPrettyConstraint()
  83. {
  84. if (null === $this->prettyConstraint) {
  85. throw new \UnexpectedValueException(sprintf('Link %s has been misconfigured and had no prettyConstraint given.', $this));
  86. }
  87. return $this->prettyConstraint;
  88. }
  89. /**
  90. * @return string
  91. */
  92. public function __toString()
  93. {
  94. return $this->source.' '.$this->description.' '.$this->target.' ('.$this->constraint.')';
  95. }
  96. /**
  97. * @param PackageInterface $sourcePackage
  98. * @return string
  99. */
  100. public function getPrettyString(PackageInterface $sourcePackage)
  101. {
  102. return $sourcePackage->getPrettyString().' '.$this->description.' '.$this->target.' '.$this->constraint->getPrettyString().'';
  103. }
  104. }