generator: use a proper destination
[mdref/mdref] / mdref / Reference.php
1 <?php
2
3 namespace mdref;
4
5 use ArrayIterator;
6 use Iterator;
7 use IteratorAggregate;
8 use function is_numeric;
9 use function preg_replace;
10
11 /**
12 * The complete available reference
13 */
14 class Reference implements IteratorAggregate {
15 /**
16 * List of mdref repositories
17 * @var Repo[]
18 */
19 private $repos = array();
20
21 /**
22 * @var Formatter
23 */
24 private $fmt;
25
26 /**
27 * @param array $refs list of mdref repository paths
28 */
29 public function __construct(array $refs, Formatter $fmt = null) {
30 foreach ($refs as $path) {
31 $repo = new Repo($path);
32 $this->repos[$repo->getName()] = $repo;
33 }
34 $this->fmt = $fmt ?: Formatter::factory();
35 }
36
37 /**
38 * Lookup the repo containing a ref entry
39 * @param string $entry requested reference entry, e.g. "pq/Connection/exec"
40 * @param string $canonical
41 * @return \mdref\Repo|NULL
42 */
43 public function getRepoForEntry(string $entry, string &$canonical = null) : ?Repo {
44 foreach ($this->repos as $repo) {
45 /** @var $repo Repo */
46 if ($repo->hasEntry($entry, $canonical)) {
47 return $repo;
48 }
49 }
50 return null;
51 }
52
53 /**
54 * Implements IteratorAggregate
55 * @return ArrayIterator repository list
56 */
57 public function getIterator() : Iterator {
58 return new ArrayIterator($this->repos);
59 }
60
61 /**
62 * @param string $anchor
63 * @return string
64 */
65 public function formatAnchor(string $anchor) : string {
66 if (is_numeric($anchor)) {
67 return "L$anchor";
68 }
69 return preg_replace("/[^[:alnum:]\.:_]/", ".", $anchor);
70 }
71
72 /**
73 * @param string $string
74 * @return string
75 * @throws \Exception, Exception
76 */
77 public function formatString(string $string) : string {
78 return $this->fmt->formatString($string);
79 }
80
81 /**
82 * @param string $file
83 * @return string
84 * @throws \Exception, Exception
85 */
86 public function formatFile(string $file) : string {
87 return $this->fmt->formatFile($file);
88 }
89 }