3816be23d60beb0b8a8c6ab33993e7efc39d7831
[mdref/mdref] / mdref / RefListing.php
1 <?php
2
3 namespace mdref;
4
5 /**
6 * A list of markdown reference files
7 */
8 class RefListing implements \Countable, \Iterator
9 {
10 /**
11 * @var \mdref\Path
12 */
13 protected $path;
14
15 /**
16 * @var array
17 */
18 protected $entries;
19
20 /**
21 * @param \mdref\Path $path
22 * @param array $files
23 */
24 function __construct(Path $path, array $files) {
25 $this->path = $path;
26 $this->entries = array_map(function($fn) {
27 return substr(trim($fn, DIRECTORY_SEPARATOR), 0, -3);
28 }, $files);
29 }
30
31 /**
32 * Implements \Countable
33 * @return int
34 */
35 function count() {
36 return count($this->entries);
37 }
38
39 /**
40 * Implements \Iterator
41 */
42 function rewind() {
43 reset($this->entries);
44 }
45
46 /**
47 * Implements \Iterator
48 * @return bool
49 */
50 function valid() {
51 return null !== key($this->entries);
52 }
53
54 /**
55 * Implements \Iterator
56 * @return string
57 */
58 function key() {
59 return $this->path->getSubPath(current($this->entries));
60 }
61
62 /**
63 * Implements \Iterator
64 */
65 function next() {
66 next($this->entries);
67 }
68
69 /**
70 * Implements \Iterator
71 * @return \mdref\RefEntry
72 */
73 function current() {
74 return new RefEntry($this->path, $this->key());//$this->format($this->key());
75 }
76
77 /**
78 * Get the parent reference entry
79 * @return null|\mdref\RefEntry
80 */
81 function getParent() {
82 switch ($parent = dirname($this->path->getPathName())) {
83 case ".":
84 case "":
85 return null;
86 default:
87 return new RefEntry($this->path, $parent);
88 }
89 }
90
91 /**
92 * Get the reference entry this reflist is based of
93 * @return \mdref\RefEntry
94 */
95 function getSelf() {
96 return new RefEntry($this->path);
97 }
98 }