Link.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. use Composer\Package\PackageInterface;
  14. /**
  15. * Represents a link between two packages, represented by their names
  16. *
  17. * @author Nils Adermann <naderman@naderman.de>
  18. */
  19. class Link
  20. {
  21. protected $source;
  22. protected $target;
  23. protected $constraint;
  24. protected $description;
  25. protected $prettyConstraint;
  26. /**
  27. * Creates a new package link.
  28. *
  29. * @param string $source
  30. * @param string $target
  31. * @param LinkConstraintInterface $constraint Constraint applying to the target of this link
  32. * @param string $description Used to create a descriptive string representation
  33. * @param string $prettyConstraint
  34. */
  35. public function __construct($source, $target, LinkConstraintInterface $constraint = null, $description = 'relates to', $prettyConstraint = null)
  36. {
  37. $this->source = strtolower($source);
  38. $this->target = strtolower($target);
  39. $this->constraint = $constraint;
  40. $this->description = $description;
  41. $this->prettyConstraint = $prettyConstraint;
  42. }
  43. public function getSource()
  44. {
  45. return $this->source;
  46. }
  47. public function getTarget()
  48. {
  49. return $this->target;
  50. }
  51. public function getConstraint()
  52. {
  53. return $this->constraint;
  54. }
  55. public function getPrettyConstraint()
  56. {
  57. if (null === $this->prettyConstraint) {
  58. throw new \UnexpectedValueException(sprintf('Link %s has been misconfigured and had no prettyConstraint given.', $this));
  59. }
  60. return $this->prettyConstraint;
  61. }
  62. public function __toString()
  63. {
  64. return $this->source.' '.$this->description.' '.$this->target.' ('.$this->constraint.')';
  65. }
  66. public function getPrettyString(PackageInterface $sourcePackage)
  67. {
  68. return $sourcePackage->getPrettyString().' '.$this->description.' '.$this->target.' '.$this->constraint->getPrettyString().'';
  69. }
  70. }