TypeDoc.php 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?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;
11
use yii\helpers\StringHelper;
12

13
/**
14
 * Base class for API documentation information for classes, interfaces and traits.
15
 *
16 17 18 19 20 21 22
 * @property MethodDoc[] $nativeMethods This property is read-only.
 * @property PropertyDoc[] $nativeProperties This property is read-only.
 * @property MethodDoc[] $protectedMethods This property is read-only.
 * @property PropertyDoc[] $protectedProperties This property is read-only.
 * @property MethodDoc[] $publicMethods This property is read-only.
 * @property PropertyDoc[] $publicProperties This property is read-only.
 *
23 24 25
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
26 27 28
class TypeDoc extends BaseDoc
{
	public $authors = [];
29 30 31
	/**
	 * @var MethodDoc[]
	 */
32
	public $methods = [];
33 34 35
	/**
	 * @var PropertyDoc[]
	 */
36 37
	public $properties = [];

38
	public $namespace;
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

	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;
	}

64 65 66 67 68 69 70 71 72 73 74
	/**
	 * @return MethodDoc[]
	 */
	public function getNativeMethods()
	{
		return $this->getFilteredMethods(null, $this->name);
	}

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

80 81 82
	/**
	 * @return MethodDoc[]
	 */
83 84 85 86 87
	public function getProtectedMethods()
	{
		return $this->getFilteredMethods('protected');
	}

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

108 109 110 111 112 113 114 115 116 117 118
	/**
	 * @return PropertyDoc[]
	 */
	public function getNativeProperties()
	{
		return $this->getFilteredProperties(null, $this->name);
	}

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

124 125 126
	/**
	 * @return PropertyDoc[]
	 */
127 128 129 130 131
	public function getProtectedProperties()
	{
		return $this->getFilteredProperties('protected');
	}

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

155 156
	/**
	 * @param \phpDocumentor\Reflection\InterfaceReflector $reflector
157
	 * @param Context $context
158 159
	 * @param array $config
	 */
160
	public function __construct($reflector = null, $context = null, $config = [])
161
	{
162
		parent::__construct($reflector, $context, $config);
163

164
		$this->namespace = StringHelper::dirname($this->name);
165

166 167 168 169 170 171 172 173 174 175 176 177 178
		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') {
179
				$property = new PropertyDoc($propertyReflector, $context, ['sourceFile' => $this->sourceFile]);
180 181 182 183 184 185 186
				$property->definedBy = $this->name;
				$this->properties[$property->name] = $property;
			}
		}

		foreach($reflector->getMethods() as $methodReflector) {
			if ($methodReflector->getVisibility() != 'private') {
187
				$method = new MethodDoc($methodReflector, $context, ['sourceFile' => $this->sourceFile]);
188 189 190 191 192 193
				$method->definedBy = $this->name;
				$this->methods[$method->name] = $method;
			}
		}
	}
}