1<?php
2/**
3 * This file is part of phpDocumentor.
4 *
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
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 Webmozart\Assert\Assert;
16
17/**
18 * Reflection class for an {@}author tag in a Docblock.
19 */
20final class Author extends BaseTag implements Factory\StaticMethod
21{
22    /** @var string register that this is the author tag. */
23    protected $name = 'author';
24
25    /** @var string The name of the author */
26    private $authorName = '';
27
28    /** @var string The email of the author */
29    private $authorEmail = '';
30
31    /**
32     * Initializes this tag with the author name and e-mail.
33     *
34     * @param string $authorName
35     * @param string $authorEmail
36     */
37    public function __construct($authorName, $authorEmail)
38    {
39        Assert::string($authorName);
40        Assert::string($authorEmail);
41        if ($authorEmail && !filter_var($authorEmail, FILTER_VALIDATE_EMAIL)) {
42            throw new \InvalidArgumentException('The author tag does not have a valid e-mail address');
43        }
44
45        $this->authorName  = $authorName;
46        $this->authorEmail = $authorEmail;
47    }
48
49    /**
50     * Gets the author's name.
51     *
52     * @return string The author's name.
53     */
54    public function getAuthorName()
55    {
56        return $this->authorName;
57    }
58
59    /**
60     * Returns the author's email.
61     *
62     * @return string The author's email.
63     */
64    public function getEmail()
65    {
66        return $this->authorEmail;
67    }
68
69    /**
70     * Returns this tag in string form.
71     *
72     * @return string
73     */
74    public function __toString()
75    {
76        return $this->authorName . (strlen($this->authorEmail) ? ' <' . $this->authorEmail . '>' : '');
77    }
78
79    /**
80     * Attempts to create a new Author object based on †he tag body.
81     *
82     * @param string $body
83     *
84     * @return static
85     */
86    public static function create($body)
87    {
88        Assert::string($body);
89
90        $splitTagContent = preg_match('/^([^\<]*)(?:\<([^\>]*)\>)?$/u', $body, $matches);
91        if (!$splitTagContent) {
92            return null;
93        }
94
95        $authorName = trim($matches[1]);
96        $email = isset($matches[2]) ? trim($matches[2]) : '';
97
98        return new static($authorName, $email);
99    }
100}
101