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 phpDocumentor\Reflection\DocBlock\Description; 16use phpDocumentor\Reflection\DocBlock\DescriptionFactory; 17use phpDocumentor\Reflection\DocBlock\StandardTagFactory; 18use phpDocumentor\Reflection\Types\Context as TypeContext; 19use Webmozart\Assert\Assert; 20 21/** 22 * Parses a tag definition for a DocBlock. 23 */ 24class Generic extends BaseTag implements Factory\StaticMethod 25{ 26 /** 27 * Parses a tag and populates the member variables. 28 * 29 * @param string $name Name of the tag. 30 * @param Description $description The contents of the given tag. 31 */ 32 public function __construct($name, Description $description = null) 33 { 34 $this->validateTagName($name); 35 36 $this->name = $name; 37 $this->description = $description; 38 } 39 40 /** 41 * Creates a new tag that represents any unknown tag type. 42 * 43 * @param string $body 44 * @param string $name 45 * @param DescriptionFactory $descriptionFactory 46 * @param TypeContext $context 47 * 48 * @return static 49 */ 50 public static function create( 51 $body, 52 $name = '', 53 DescriptionFactory $descriptionFactory = null, 54 TypeContext $context = null 55 ) { 56 Assert::string($body); 57 Assert::stringNotEmpty($name); 58 Assert::notNull($descriptionFactory); 59 60 $description = $descriptionFactory && $body ? $descriptionFactory->create($body, $context) : null; 61 62 return new static($name, $description); 63 } 64 65 /** 66 * Returns the tag as a serialized string 67 * 68 * @return string 69 */ 70 public function __toString() 71 { 72 return ($this->description ? $this->description->render() : ''); 73 } 74 75 /** 76 * Validates if the tag name matches the expected format, otherwise throws an exception. 77 * 78 * @param string $name 79 * 80 * @return void 81 */ 82 private function validateTagName($name) 83 { 84 if (! preg_match('/^' . StandardTagFactory::REGEX_TAGNAME . '$/u', $name)) { 85 throw new \InvalidArgumentException( 86 'The tag name "' . $name . '" is not wellformed. Tags may only consist of letters, underscores, ' 87 . 'hyphens and backslashes.' 88 ); 89 } 90 } 91} 92