legacy
[mdref/mdref] / mdref / Entry.php
1 <?php
2
3 namespace mdref;
4
5 /**
6 * A single reference entry
7 */
8 class Entry implements \IteratorAggregate {
9 /**
10 * Compound name
11 * @var string
12 */
13 private $name;
14
15 /**
16 * Split name
17 * @var array
18 */
19 private $list;
20
21 /**
22 * The containing repository
23 * @var \mdref\Repo
24 */
25 private $repo;
26
27 /**
28 * The file path, if the refentry exists
29 * @var type
30 */
31 private $path;
32
33 /**
34 * The file instance of this entry
35 * @var \mdref\File
36 */
37 private $file;
38
39 /**
40 * @param string $name the compound name of the ref entry, e.g. "pq/Connection/exec"
41 * @param \mdref\Repo $repo the containing repository
42 */
43 public function __construct($name, Repo $repo) {
44 $this->repo = $repo;
45 $this->name = $name;
46 $this->list = explode("/", $name);
47 $this->path = $repo->hasEntry($name);
48 }
49
50 /**
51 * Get the compound name, e.g. "pq/Connection/exec"
52 * @return string
53 */
54 public function getName() {
55 return $this->name;
56 }
57
58 /**
59 * Get the containing repository
60 * @return \mdref\Repo
61 */
62 public function getRepo() {
63 return $this->repo;
64 }
65
66 /**
67 * Get the file path, if any
68 * @return string
69 */
70 public function getPath() {
71 return $this->path;
72 }
73
74 /**
75 * Get the file instance of this entry
76 * @return \mdref\File
77 */
78 public function getFile() {
79 if (!$this->file) {
80 $this->file = new File($this->path);
81 }
82 return $this->file;
83 }
84
85 /**
86 * Get edit URL
87 * @return string
88 */
89 public function getEditUrl() {
90 return $this->repo->getEditUrl($this->name);
91 }
92
93 /**
94 * Read the title of the ref entry file
95 * @return string
96 */
97 public function getTitle() {
98 if ($this->isFile()) {
99 return $this->getFile()->readTitle();
100 }
101 if ($this->isRoot()) {
102 return $this->repo->getRootEntry()->getTitle();
103 }
104 return $this->name;
105 }
106
107 /**
108 * Read the description of the ref entry file
109 * @return string
110 */
111 public function getDescription() {
112 if ($this->isFile()) {
113 return $this->getFile()->readDescription();
114 }
115 if ($this->isRoot()) {
116 return $this->repo->getRootEntry()->getDescription();
117 }
118 return $this;
119 }
120
121 /**
122 * Read the intriductory section of the refentry file
123 * @return string
124 */
125 public function getIntro() {
126 if ($this->isFile()) {
127 return $this->getFile()->readIntro();
128 }
129 if ($this->isRoot()) {
130 return $this->repo->getRootEntry()->getIntro();
131 }
132 return "";
133 }
134
135 /**
136 * Check if the refentry exists
137 * @return bool
138 */
139 public function isFile() {
140 return strlen($this->path) > 0;
141 }
142
143 /**
144 * Check if this is the first entry of the reference tree
145 * @return bool
146 */
147 public function isRoot() {
148 return count($this->list) === 1;
149 }
150
151 /**
152 * Get the parent ref entry
153 * @return \mdref\Entry
154 */
155 public function getParent() {
156 switch ($dirn = dirname($this->name)) {
157 case ".":
158 case "/":
159 break;
160 default:
161 return $this->repo->getEntry($dirn);
162 }
163 }
164
165 /**
166 * Get the list of parents up-down
167 * @return array
168 */
169 public function getParents() {
170 $parents = array();
171 for ($parent = $this->getParent(); $parent; $parent = $parent->getParent()) {
172 array_unshift($parents, $parent);
173 }
174 return $parents;
175 }
176
177 /**
178 * Guess whether this ref entry is about a function or method
179 * @return bool
180 */
181 public function isFunction() {
182 $base = end($this->list);
183 return $base{0} === "_" || ctype_lower($base{0});
184 }
185
186 /**
187 * Guess whether this ref entry is about a namespace, interface or class
188 * @return bool
189 */
190 public function isNsClass() {
191 $base = end($this->list);
192 return ctype_upper($base{0});
193 }
194
195 /**
196 * Display name
197 * @return string
198 */
199 public function __toString() {
200 $parts = explode("/", trim($this->getName(), "/"));
201 $myself = array_pop($parts);
202 if (!$parts) {
203 return $myself;
204 }
205 $parent = end($parts);
206
207 switch ($myself{0}) {
208 case ":":
209 return "★" . substr($myself, 1);
210
211 default:
212 if (!ctype_lower($myself{0}) || ctype_lower($parent{0})) {
213 return $myself;
214 }
215 case "_":
216 return $parent . "::" . $myself;
217 }
218 }
219
220 /**
221 * Get the base name of this ref entry
222 * @return string
223 */
224 public function getBasename() {
225 return dirname($this->path) . "/" . basename($this->path, ".md");
226 }
227
228 /**
229 * Guess whether there are any child nodes
230 * @param string $glob
231 * @return boolean
232 */
233 function hasIterator($glob = null, $loose = false) {
234 if (strlen($glob)) {
235 return glob($this->getBasename() . "/$glob") ||
236 ($loose && glob($this->getBasename() . "/*/$glob"));
237 } elseif ($this->isRoot()) {
238 return true;
239 } else {
240 return is_dir($this->getBasename());
241 }
242 }
243
244 /**
245 * Guess whether there are namespace/interface/class child nodes
246 * @return bool
247 */
248 function hasNsClasses() {
249 return $this->hasIterator("/[A-Z]*.md", true);
250 }
251
252 /**
253 * Guess whether there are function/method child nodes
254 * @return bool
255 */
256 function hasFunctions() {
257 return $this->hasIterator("/[a-z_]*.md");
258 }
259
260 /**
261 * Implements \IteratorAggregate
262 * @return \mdref\Tree child nodes
263 */
264 function getIterator() {
265 return new Tree($this->getBasename(), $this->repo);
266 }
267 }