1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of phpDocumentor.
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * @link http://phpdoc.org
12 */
13
14namespace phpDocumentor\Reflection\DocBlock\Tags;
15
16use phpDocumentor\Reflection\DocBlock\Description;
17use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
18use phpDocumentor\Reflection\Types\Context as TypeContext;
19use phpDocumentor\Reflection\Utils;
20use Webmozart\Assert\Assert;
21
22/**
23 * Reflection class for a {@}link tag in a Docblock.
24 */
25final class Link extends BaseTag implements Factory\StaticMethod
26{
27    /** @var string */
28    protected $name = 'link';
29
30    /** @var string */
31    private $link;
32
33    /**
34     * Initializes a link to a URL.
35     */
36    public function __construct(string $link, ?Description $description = null)
37    {
38        $this->link        = $link;
39        $this->description = $description;
40    }
41
42    public static function create(
43        string $body,
44        ?DescriptionFactory $descriptionFactory = null,
45        ?TypeContext $context = null
46    ) : self {
47        Assert::notNull($descriptionFactory);
48
49        $parts = Utils::pregSplit('/\s+/Su', $body, 2);
50        $description = isset($parts[1]) ? $descriptionFactory->create($parts[1], $context) : null;
51
52        return new static($parts[0], $description);
53    }
54
55    /**
56     * Gets the link
57     */
58    public function getLink() : string
59    {
60        return $this->link;
61    }
62
63    /**
64     * Returns a string representation for this tag.
65     */
66    public function __toString() : string
67    {
68        if ($this->description) {
69            $description = $this->description->render();
70        } else {
71            $description = '';
72        }
73
74        $link = (string) $this->link;
75
76        return $link . ($description !== '' ? ($link !== '' ? ' ' : '') . $description : '');
77    }
78}
79