1<?php
2/**
3 * phpDocumentor
4 *
5 * PHP Version 5.3
6 *
7 * @author    Vasil Rangelov <boen.robot@gmail.com>
8 * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
9 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10 * @link      http://phpdoc.org
11 */
12
13namespace phpDocumentor\Reflection\DocBlock\Tags;
14
15use phpDocumentor\Reflection\DocBlock\Description;
16use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
17use phpDocumentor\Reflection\Types\Context as TypeContext;
18use Webmozart\Assert\Assert;
19
20/**
21 * Reflection class for a {@}version tag in a Docblock.
22 */
23final class Version extends BaseTag implements Factory\StaticMethod
24{
25    protected $name = 'version';
26
27    /**
28     * PCRE regular expression matching a version vector.
29     * Assumes the "x" modifier.
30     */
31    const REGEX_VECTOR = '(?:
32        # Normal release vectors.
33        \d\S*
34        |
35        # VCS version vectors. Per PHPCS, they are expected to
36        # follow the form of the VCS name, followed by ":", followed
37        # by the version vector itself.
38        # By convention, popular VCSes like CVS, SVN and GIT use "$"
39        # around the actual version vector.
40        [^\s\:]+\:\s*\$[^\$]+\$
41    )';
42
43    /** @var string The version vector. */
44    private $version = '';
45
46    public function __construct($version = null, Description $description = null)
47    {
48        Assert::nullOrStringNotEmpty($version);
49
50        $this->version = $version;
51        $this->description = $description;
52    }
53
54    /**
55     * @return static
56     */
57    public static function create($body, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
58    {
59        Assert::nullOrString($body);
60        if (empty($body)) {
61            return new static();
62        }
63
64        $matches = [];
65        if (!preg_match('/^(' . self::REGEX_VECTOR . ')\s*(.+)?$/sux', $body, $matches)) {
66            return null;
67        }
68
69        return new static(
70            $matches[1],
71            $descriptionFactory->create(isset($matches[2]) ? $matches[2] : '', $context)
72        );
73    }
74
75    /**
76     * Gets the version section of the tag.
77     *
78     * @return string
79     */
80    public function getVersion()
81    {
82        return $this->version;
83    }
84
85    /**
86     * Returns a string representation for this tag.
87     *
88     * @return string
89     */
90    public function __toString()
91    {
92        return $this->version . ($this->description ? ' ' . $this->description->render() : '');
93    }
94}
95