TypeDoc.php 3.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\apidoc\models;

use phpDocumentor\Reflection\DocBlock\Tag\AuthorTag;
use yii\base\Exception;
12
use yii\helpers\StringHelper;
13

14
/**
15
 * Base class for API documentation information for classes, interfaces and traits.
16 17 18 19
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
20 21 22
class TypeDoc extends BaseDoc
{
	public $authors = [];
23 24 25
	/**
	 * @var MethodDoc[]
	 */
26
	public $methods = [];
27 28 29
	/**
	 * @var PropertyDoc[]
	 */
30 31
	public $properties = [];

32
	public $namespace;
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

	public function findSubject($subjectName)
	{
		if ($subjectName[0] != '$') {
			foreach($this->methods as $name => $method) {
				if (rtrim($subjectName, '()') == $name) {
					return $method;
				}
			}
		}
		if (substr($subjectName, -2, 2) == '()') {
			return null;
		}
		if ($this->properties === null) {
			return null;
		}
		foreach($this->properties as $name => $property) {
			if (ltrim($subjectName, '$') == ltrim($name, '$')) {
				return $property;
			}
		}
		return null;
	}

58 59 60 61 62 63 64 65 66 67 68
	/**
	 * @return MethodDoc[]
	 */
	public function getNativeMethods()
	{
		return $this->getFilteredMethods(null, $this->name);
	}

	/**
	 * @return MethodDoc[]
	 */
69 70 71 72 73
	public function getPublicMethods()
	{
		return $this->getFilteredMethods('public');
	}

74 75 76
	/**
	 * @return MethodDoc[]
	 */
77 78 79 80 81
	public function getProtectedMethods()
	{
		return $this->getFilteredMethods('protected');
	}

82 83 84 85 86 87
	/**
	 * @param null $visibility
	 * @param null $definedBy
	 * @return MethodDoc[]
	 */
	private function getFilteredMethods($visibility = null, $definedBy = null)
88 89
	{
		$methods = [];
90
		foreach($this->methods as $name => $method) {
91 92 93 94 95
			if ($visibility !== null && $method->visibility != $visibility) {
				continue;
			}
			if ($definedBy !== null && $method->definedBy != $definedBy) {
				continue;
96
			}
97
			$methods[$name] = $method;
98 99 100 101
		}
		return $methods;
	}

102 103 104 105 106 107 108 109 110 111 112
	/**
	 * @return PropertyDoc[]
	 */
	public function getNativeProperties()
	{
		return $this->getFilteredProperties(null, $this->name);
	}

	/**
	 * @return PropertyDoc[]
	 */
113 114 115 116 117
	public function getPublicProperties()
	{
		return $this->getFilteredProperties('public');
	}

118 119 120
	/**
	 * @return PropertyDoc[]
	 */
121 122 123 124 125
	public function getProtectedProperties()
	{
		return $this->getFilteredProperties('protected');
	}

126 127 128 129 130 131
	/**
	 * @param null $visibility
	 * @param null $definedBy
	 * @return PropertyDoc[]
	 */
	private function getFilteredProperties($visibility = null, $definedBy = null)
132 133 134 135 136
	{
		if ($this->properties === null) {
			return [];
		}
		$properties = [];
137
		foreach($this->properties as $name => $property) {
138 139 140 141 142
			if ($visibility !== null && $property->visibility != $visibility) {
				continue;
			}
			if ($definedBy !== null && $property->definedBy != $definedBy) {
				continue;
143
			}
144
			$properties[$name] = $property;
145 146 147 148
		}
		return $properties;
	}

149 150 151 152
	/**
	 * @param \phpDocumentor\Reflection\InterfaceReflector $reflector
	 * @param array $config
	 */
153
	public function __construct($reflector = null, $config = [])
154 155 156
	{
		parent::__construct($reflector, $config);

157 158
		$this->namespace = StringHelper::basename($this->name);

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		if ($reflector === null) {
			return;
		}

		foreach($this->tags as $i => $tag) {
			if ($tag instanceof AuthorTag) {
				$this->authors[$tag->getAuthorName()] = $tag->getAuthorEmail();
				unset($this->tags[$i]);
			}
		}

		foreach($reflector->getProperties() as $propertyReflector) {
			if ($propertyReflector->getVisibility() != 'private') {
				$property = new PropertyDoc($propertyReflector);
				$property->definedBy = $this->name;
				$this->properties[$property->name] = $property;
			}
		}

		foreach($reflector->getMethods() as $methodReflector) {
			if ($methodReflector->getVisibility() != 'private') {
				$method = new MethodDoc($methodReflector);
				$method->definedBy = $this->name;
				$this->methods[$method->name] = $method;
			}
		}
	}
}