Link.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /**
  26. * Creates a new package link.
  27. *
  28. * @param string $source
  29. * @param string $target
  30. * @param LinkConstraintInterface $constraint Constraint applying to the target of this link
  31. * @param string $description Used to create a descriptive string representation
  32. */
  33. public function __construct($source, $target, LinkConstraintInterface $constraint = null, $description = 'relates to', $prettyConstraint = null)
  34. {
  35. $this->source = strtolower($source);
  36. $this->target = strtolower($target);
  37. $this->constraint = $constraint;
  38. $this->description = $description;
  39. $this->prettyConstraint = $prettyConstraint;
  40. }
  41. public function getSource()
  42. {
  43. return $this->source;
  44. }
  45. public function getTarget()
  46. {
  47. return $this->target;
  48. }
  49. public function getConstraint()
  50. {
  51. return $this->constraint;
  52. }
  53. public function getPrettyConstraint()
  54. {
  55. if (null === $this->prettyConstraint) {
  56. throw new \UnexpectedValueException(sprintf('Link %s has been misconfigured and had no prettyConstraint given.', $this));
  57. }
  58. return $this->prettyConstraint;
  59. }
  60. public function __toString()
  61. {
  62. return $this->source.' '.$this->description.' '.$this->target.' ('.$this->constraint.')';
  63. }
  64. public function getPrettyString(PackageInterface $sourcePackage)
  65. {
  66. return $sourcePackage->getPrettyString().' '.$this->description.' '.$this->target.' '.$this->constraint->getPrettyString().'';
  67. }
  68. }