some min-width for code blocks
[mdref/mdref] / mdref / Tree.php
1 <?php
2
3 namespace mdref;
4
5 class Tree implements \RecursiveIterator {
6 /**
7 * The repository
8 * @var \mdref\Repo
9 */
10 private $repo;
11
12 /**
13 * List of first level entries
14 * @var array
15 */
16 private $list;
17
18 /**
19 * The list iterator
20 * @var array
21 */
22 private $iter;
23
24 /**
25 * @param string $path
26 * @param \mdref\Repo $repo
27 */
28 public function __construct($path, Repo $repo) {
29 if (!($list = glob("$path/*.md"))) {
30 $list = glob("$path/*/*.md");
31 }
32 $this->list = array_filter($list, $this->generateFilter($list));
33 sort($this->list, SORT_STRING);
34 $this->repo = $repo;
35 }
36
37 /**
38 * @param array $list
39 * @return callable
40 */
41 private function generateFilter(array $list) {
42 return function($v) use($list) {
43 if ($v{0} === ".") {
44 return false;
45 }
46 if (false !== array_search("$v.md", $list, true)) {
47 return false;
48 }
49
50 $pi = pathinfo($v);
51 if (isset($pi["extension"]) && "md" !== $pi["extension"]) {
52 return false;
53 }
54
55 return true;
56 };
57 }
58
59 /**
60 * Implements \Iterator
61 * @return \mdref\Entry
62 */
63 public function current() {
64 return $this->repo->getEntry($this->repo->hasFile(current($this->iter)));
65 }
66
67 /**
68 * Implements \Iterator
69 */
70 public function next() {
71 next($this->iter);
72 }
73
74 /**
75 * Implements \Iterator
76 * @return int
77 */
78 public function key() {
79 return key($this->iter);
80 }
81
82 /**
83 * Implements \Iterator
84 */
85 public function rewind() {
86 $this->iter = $this->list;
87 reset($this->iter);
88 }
89
90 /**
91 * Implements \Iterator
92 * @return bool
93 */
94 public function valid() {
95 return null !== key($this->iter);
96 }
97
98 /**
99 * Implements \RecursiveIterator
100 * @return bool
101 */
102 public function hasChildren() {
103 return $this->current()->hasIterator();
104 }
105
106 /**
107 * Implements \RecursiveIterator
108 * @return \mdref\Tree
109 */
110 public function getChildren() {
111 return $this->current()->getIterator();
112 }
113 }