move formatting from JS to PHP
[mdref/mdref] / mdref / Repo.php
index 8f4b830295a138f87d50222bc2bb27b7e6ddf59f..f52a35e5ac06c7d939e4e3ad6d718ee61a2eefdf 100644 (file)
@@ -3,10 +3,22 @@
 namespace mdref;
 
 
+use InvalidArgumentException;
+use IteratorAggregate;
+use function basename;
+use function current;
+use function file_get_contents;
+use function glob;
+use function is_file;
+use function realpath;
+use function rtrim;
+use function sprintf;
+use function trim;
+
 /**
  * A reference repo
  */
-class Repo implements \IteratorAggregate {
+class Repo implements IteratorAggregate {
        /**
         * The name of the repository
         * @var string
@@ -30,13 +42,13 @@ class Repo implements \IteratorAggregate {
         * @param string $path
         * @throws \InvalidArgumentException
         */
-       public function __construct($path) {
-               if (!($mdref = current(glob("$path/*.mdref")))) {
-                       throw new \InvalidArgumentException(
-                               sprintf("Not a reference, could not find '*.mdref': '%s'",
-                                       $path));
+       public function __construct(string $path) {
+               if (!($glob = glob("$path/*.mdref"))) {
+                       throw new InvalidArgumentException(
+                               sprintf("Not a reference, could not find '*.mdref': '%s'", $path));
                }
 
+               $mdref = current($glob);
                $this->path = realpath($path);
                $this->name = basename($mdref, ".mdref");
                $this->edit = trim(file_get_contents($mdref));
@@ -46,7 +58,7 @@ class Repo implements \IteratorAggregate {
         * Get the repository's name
         * @return string
         */
-       public function getName() {
+       public function getName() : string {
                return $this->name;
        }
 
@@ -55,7 +67,7 @@ class Repo implements \IteratorAggregate {
         * @param string $file
         * @return string
         */
-       public function getPath($file = "") {
+       public function getPath(string $file = "") : string {
                return $this->path . "/$file";
        }
 
@@ -64,16 +76,18 @@ class Repo implements \IteratorAggregate {
         * @param string $entry
         * @return string
         */
-       public function getEditUrl($entry) {
+       public function getEditUrl(string $entry) : string {
                return sprintf($this->edit, $entry);
        }
 
        /**
         * Get the file path of an entry in this repo
+        *
         * @param string $entry
+        * @param string|null $canonical
         * @return string file path
         */
-       public function hasEntry($entry, &$canonical = null) {
+       public function hasEntry(string $entry, ?string &$canonical = null) : ?string {
                $trim = rtrim($entry, "/");
                $file = $this->getPath("$trim.md");
                if (is_file($file)) {
@@ -87,6 +101,7 @@ class Repo implements \IteratorAggregate {
                        $canonical = $this->getName() . "/" . $entry;
                        return $file;
                }
+               return null;
        }
 
        /**
@@ -94,7 +109,7 @@ class Repo implements \IteratorAggregate {
         * @param string $file
         * @return string entry
         */
-       public function hasFile($file) {
+       public function hasFile(string $file) : ?string {
                if (($file = realpath($file))) {
                        $path = $this->getPath();
                        $plen = strlen($path);
@@ -109,6 +124,17 @@ class Repo implements \IteratorAggregate {
                                return  $dirname . "/". $basename;
                        }
                }
+               return null;
+       }
+
+       /**
+        * Check whether the repo has a stub file to serve
+        * @param string|null $path receives the path if there's a stub
+        * @return bool
+        */
+       public function hasStub(string &$path = null) : bool {
+               $path = $this->getPath($this->getName() . ".stub.php");
+               return is_file($path) && is_readable($path);
        }
 
        /**
@@ -117,7 +143,7 @@ class Repo implements \IteratorAggregate {
         * @return \mdref\Entry
         * @throws \OutOfBoundsException
         */
-       public function getEntry($entry) {
+       public function getEntry(string $entry) : Entry {
                return new Entry($entry, $this);
        }
 
@@ -125,7 +151,7 @@ class Repo implements \IteratorAggregate {
         * Get the root Entry instance
         * @return \mdref\Entry
         */
-       public function getRootEntry() {
+       public function getRootEntry() : Entry {
                return new Entry($this->name, $this);
        }
 
@@ -133,7 +159,7 @@ class Repo implements \IteratorAggregate {
         * Implements \IteratorAggregate
         * @return \mdref\Tree
         */
-       public function getIterator() {
+       public function getIterator() : Tree {
                return new Tree($this->path, $this);
        }
 }