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