Browse Source

Added filter methods for repositories

Joachim M. Giæver 6 years ago
parent
commit
35cb9ce1ac
5 changed files with 114 additions and 1 deletions
  1. 1 1
      index.php
  2. 8 0
      src/API/Request/Collection.php
  3. 93 0
      src/API/Request/Repos.php
  4. 7 0
      src/Lib/ArrayIterator.php
  5. 5 0
      src/Lib/Collection.php

+ 1 - 1
index.php

@@ -16,6 +16,7 @@ define('API_URL', 'https://git.giaever.org/api/v1');
 
 // The token generated at Gogs
 define('API_TOKEN', '142efbfd6fbdf147f03d289f8b22a438eaa1b5d1');
+//define('API_TOKEN', 'e14b9eff0749b6f0c4cadf4bb72b83d44578ae28');
 
 // Edit this one to your authorized accounts password to create tokens.
 define('USR_PASS', "mypassword");
@@ -41,7 +42,6 @@ try {
     // Load every repo
     $repos = $me->repos()->load(); 
 
-
     // Loop through all of them in received order
     echo "\nNormal repo\n";
     foreach($repos->all() as $key => $repo)

+ 8 - 0
src/API/Request/Collection.php

@@ -124,6 +124,14 @@ namespace Gogs\API\Request {
             return $this->objs->copy()->sort($f);
         }
 
+        /**
+         * @see \Gogs\Lib\ArrayIterator
+         */
+
+        public function filter(callable $f) {
+            return $this->objs->copy()->filter($f);
+        }
+
         /**
          * @see \Gogs\Lib\ArrayIterator
          */

+ 93 - 0
src/API/Request/Repos.php

@@ -217,6 +217,99 @@ namespace Gogs\API\Request {
             $repo->json_set_property($obj);
             return $this->add($repo, $repo->full_name);
         }
+
+        /** 
+         * Get private repositories
+         *
+         * @return \Gogs\Lib\Collection
+         */
+        public function privates() {
+            return $this->filter(function(Repo $r) {
+                return $r->private;
+            });
+        }
+
+        /** 
+         * Get public repositories
+         *
+         * @return \Gogs\Lib\Collection
+         */
+        public function publics() {
+            return $this->filter(function(Repo $r) {
+                return !$r->private;
+            });
+        }
+
+        /** 
+         * Get personal repositories
+         *
+         * @return \Gogs\Lib\Collection
+         */
+        public function personals() {
+            if (empty($this->owner))
+                return new \Gogs\Lib\Collection();
+
+            return $this->filter(function(Repo $r) {
+                return $this->owner->username == $r->owner->username;
+            });
+        }
+
+        /** 
+         * Get repositories contributed to
+         *
+         * @return \Gogs\Lib\Collection
+         */
+        public function contributions() {
+            if (empty($this->owner))
+                return new \Gogs\Lib\Collection();
+
+            return $this->filter(function(Repo $r) {
+                return $this->owner->username != $r->owner->username;
+            });
+        }
+
+        /** 
+         * Get personal private repositories
+         *
+         * @return \Gogs\Lib\Collection
+         */
+        public function personals_privates() {
+            return $this->personals()->filter(function(Repo $r) {
+                return $r->private;
+            });
+        }
+
+        /** 
+         * Get personal public repositories
+         *
+         * @return \Gogs\Lib\Collection
+         */
+        public function personals_publics() {
+            return $this->personals()->filter(function(Repo $r) {
+                return !$r->private;
+            });
+        }
+
+        /** 
+         * Get private contributions
+         *
+         * @return \Gogs\Lib\Collection
+         */
+        public function contributions_privates() {
+            return $this->contributions()->filter(function(Repo $r) {
+                return $r->private;
+            });
+        }
+        /** 
+         * Get public contributions
+         *
+         * @return \Gogs\Lib\Collection
+         */
+        public function contributions_publics() {
+            return $this->contributions()->filter(function(Repo $r) {
+                return !$r->private;
+            });
+        }
     }
 
 }

+ 7 - 0
src/Lib/ArrayIterator.php

@@ -92,5 +92,12 @@ namespace Gogs\Lib {
          * @return Collection
          */
         public function sort(callable $f);
+
+        /**
+         * Filter collection
+         *
+         * @return Collection
+         */
+        public function filter(callable $f);
     }
 }

+ 5 - 0
src/Lib/Collection.php

@@ -94,6 +94,11 @@ namespace Gogs\Lib {
             return uasort($this->objs, $f) ? $this : false;
         }
 
+        public function filter(callable $f) {
+            $this->objs = array_filter($this->objs, $f);
+            return $this;
+        }
+
         /**
          * @see \Gogs\Lib\ArrayIterator
          */