minor visual improvements; add back edit link
[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 if ("." !== ($dirn = dirname($this->name))) {
157 return $this->repo->getEntry($dirn);
158 }
159 }
160
161 /**
162 * Get the list of parents up-down
163 * @return array
164 */
165 public function getParents() {
166 $parents = array();
167 for ($parent = $this->getParent(); $parent; $parent = $parent->getParent()) {
168 array_unshift($parents, $parent);
169 }
170 return $parents;
171 }
172
173 /**
174 * Guess whether this ref entry is about a function or method
175 * @return bool
176 */
177 public function isFunction() {
178 $base = end($this->list);
179 return $base{0} === "_" || ctype_lower($base{0});
180 }
181
182 /**
183 * Guess whether this ref entry is about a namespace, interface or class
184 * @return bool
185 */
186 public function isNsClass() {
187 $base = end($this->list);
188 return ctype_upper($base{0});
189 }
190
191 /**
192 * Display name
193 * @return string
194 */
195 public function __toString() {
196 $parts = explode("/", trim($this->getName(), "/"));
197 $myself = array_pop($parts);
198 if (!$parts) {
199 return $myself;
200 }
201 $parent = end($parts);
202
203 switch ($myself{0}) {
204 case ":":
205 return "★" . substr($myself, 1);
206
207 default:
208 if (!ctype_lower($myself{0}) || ctype_lower($parent{0})) {
209 return $myself;
210 }
211 case "_":
212 return $parent . "::" . $myself;
213 }
214 }
215
216 /**
217 * Get the base name of this ref entry
218 * @return string
219 */
220 public function getBasename() {
221 return dirname($this->path) . "/" . basename($this->path, ".md");
222 }
223
224 /**
225 * Guess whether there are any child nodes
226 * @param string $glob
227 * @return boolean
228 */
229 function hasIterator($glob = null, $loose = false) {
230 if (strlen($glob)) {
231 return glob($this->getBasename() . "/$glob") ||
232 ($loose && glob($this->getBasename() . "/*/$glob"));
233 } elseif ($this->isRoot()) {
234 return true;
235 } else {
236 return is_dir($this->getBasename());
237 }
238 }
239
240 /**
241 * Guess whether there are namespace/interface/class child nodes
242 * @return bool
243 */
244 function hasNsClasses() {
245 return $this->hasIterator("/[A-Z]*.md", true);
246 }
247
248 /**
249 * Guess whether there are function/method child nodes
250 * @return bool
251 */
252 function hasFunctions() {
253 return $this->hasIterator("/[a-z_]*.md");
254 }
255
256 /**
257 * Implements \IteratorAggregate
258 * @return \mdref\Tree child nodes
259 */
260 function getIterator() {
261 return new Tree($this->getBasename(), $this->repo);
262 }
263 }