convert to autocracy
[mdref/mdref] / mdref / RefListing.php
1 <?php
2
3 namespace mdref;
4
5 class RefListing implements \Countable, \Iterator
6 {
7 /**
8 * @var \mdref\Path
9 */
10 protected $path;
11
12 /**
13 * @var array
14 */
15 protected $entries;
16
17 /**
18 * @param \mdref\Path $path
19 */
20 function __construct(Path $path, array $files) {
21 $this->path = $path;
22 $this->entries = array_map(function($fn) {
23 return substr(trim($fn, DIRECTORY_SEPARATOR), 0, -3);
24 }, $files);
25 }
26
27 /**
28 * Copy constructor
29 * @param mixed $filter callable array filter or fnmatch pattern
30 * @return \mdref\RefListing
31 */
32 function __invoke($filter) {
33 die(__METHOD__);
34 $that = clone $this;
35 $that->entries = array_filter($that->entries, is_callable($filter)
36 ? $filter
37 : function($fn) use ($filter) {
38 return fnmatch($filter, $fn);
39 }
40 );
41 return $that;
42 }
43
44 function __toString() {
45 return __METHOD__;
46 return $this->format(substr($this->path, strlen($this->path->getBaseDir())));
47 }
48
49 function count() {
50 return count($this->entries);
51 }
52
53 function rewind() {
54 reset($this->entries);
55 }
56
57 function valid() {
58 return null !== key($this->entries);
59 }
60
61 function key() {
62 return $this->path->getSubPath(current($this->entries));
63 }
64
65 function next() {
66 next($this->entries);
67 }
68
69 function current() {
70 return new RefEntry($this->path, $this->key());//$this->format($this->key());
71 }
72
73 function getParent() {
74 switch ($parent = dirname($this->path->getPathName())) {
75 case ".":
76 case "":
77 return null;
78 default:
79 return new RefEntry($this->path, $parent);
80 }
81 }
82
83 function getSelf() {
84 return new RefEntry($this->path);
85 }
86
87 function format($entry) {
88 return __METHOD__;
89 $ns = "";
90 if (strlen($entry = trim($entry, DIRECTORY_SEPARATOR))) {
91 $upper = ctype_upper($entry[0]);
92 $parts = explode(DIRECTORY_SEPARATOR, $entry);
93
94 for ($i = 0; $i < count($parts); ++$i) {
95 if (!strlen($parts[$i]) || $parts[$i] === ".") {
96 continue;
97 }
98 if (strlen($ns)) {
99 if ($upper && !ctype_upper($parts[$i][0])) {
100 $ns .= "::";
101 } else {
102 $ns .= "\\";
103 }
104 }
105 $ns .= $parts[$i];
106 $upper = ctype_upper($parts[$i][0]);
107 }
108 }
109 return $ns;
110 }
111 }