Browse Source

Changed return values of filter-methods

Joachim M. Giæver 6 years ago
parent
commit
390ece29a3
3 changed files with 50 additions and 67 deletions
  1. 0 1
      index.php
  2. 3 2
      src/API/Request/Collection.php
  3. 47 64
      src/API/Request/Repos.php

+ 0 - 1
index.php

@@ -16,7 +16,6 @@ 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");

+ 3 - 2
src/API/Request/Collection.php

@@ -25,7 +25,7 @@ namespace Gogs\API\Request {
             parent::__construct($api_url, $api_token);
 
             if ($other != null)
-                $this->objs = $others->copy();
+                $this->objs = $others->all();
             else
                 $this->objs = new \Gogs\Lib\Collection();
         }
@@ -67,8 +67,9 @@ namespace Gogs\API\Request {
          * @see \Gogs\Lib\ArrayIterator
          */
         public function copy() {
-            return new Collection($this);
+            return new Collection($this->url, $this->token, $this);
         }
+        //abstract public function copy();
 
         /**
          * @see \Gogs\Lib\ArrayIterator

+ 47 - 64
src/API/Request/Repos.php

@@ -184,29 +184,39 @@ namespace Gogs\API\Request {
          * 
          * @param int $flag Defines sorting algorithm to use
          * @param bool $asc Ascending order
-         * @return \Gogs\Lib\Collection
+         * @return Repos
          */
         public function sort_by(int $flag = Collection::SORT_INDEX, bool $asc = false) {
+
+            $repos = new Repos($this->url, $this->token, $this->owner);
+
             switch ($flag) {
             case self::SORT_CREATED:
-                return ($sort = $this->sort(function(Repo $a, Repo $b) {
+                $sort = $this->sort(function(Repo $a, Repo $b) {
                     $adate = new \DateTime($a->created_at);
                     $bdate = new \DateTime($b->created_at);
                     return ($adate == $bdate ? 0 : ($adate > $bdate ? 1 : -1));
-                })) ? ($asc ? $sort->reverse() : $sort) : false;
+                });
             case self::SORT_UPDATED:
-                return ($sort = $this->sort(function(Repo $a, Repo $b) {
+                $sort = $this->sort(function(Repo $a, Repo $b) {
                     $adate = new \DateTime($a->updated_at);
                     $bdate = new \DateTime($b->updated_at);
                     return ($adate == $bdate ? 0 : ($adate > $bdate ? 1 : -1));
-                })) ? ($asc ? $sort->reverse() : $sort) : false;
+                });
             case self::SORT_OWNER:
-                return ($sort = $this->sort(function(Repo $a, Repo $b) {
+                $sort = $this->sort(function(Repo $a, Repo $b) {
                     return strcmp($a->owner->username, $b->owner->username);
-                })) ? ($asc ? $sort->reverse() : $sort) : false;
+                });
             default:
-                return ($sort = $this->sort("ksort")) ? ($asc ? $sort->reverse() : $sort) : false;
+                $sort = $this->sort("ksort");
             }
+
+            if ($asc)
+                $sort = $sort->reverse();
+
+            $repos->add($sort->all());
+
+            return $repos;
         }
 
         /**
@@ -221,94 +231,67 @@ namespace Gogs\API\Request {
         /** 
          * Get private repositories
          *
-         * @return \Gogs\Lib\Collection
+         * @return Repos
          */
         public function privates() {
-            return $this->filter(function(Repo $r) {
+            $repos = new Repos($this->url, $this->token, $this->owner);
+
+            $repos->add($this->filter(function(Repo $r) {
                 return $r->private;
-            });
+            })->all());
+
+            return $repos;
         }
 
         /** 
          * Get public repositories
          *
-         * @return \Gogs\Lib\Collection
+         * @return Repos
          */
         public function publics() {
-            return $this->filter(function(Repo $r) {
+            $repos = new Repos($this->url, $this->token, $this->owner);
+
+            $repos->add($this->filter(function(Repo $r) {
                 return !$r->private;
-            });
+            })->all());
+
+            return $repos;
         }
 
         /** 
          * Get personal repositories
          *
-         * @return \Gogs\Lib\Collection
+         * @return Repos
          */
         public function personals() {
+            $repos = new Repos($this->url, $this->token, $this->owner);
+
             if (empty($this->owner))
-                return new \Gogs\Lib\Collection();
+                return $repos;
 
-            return $this->filter(function(Repo $r) {
+            $repos->add($this->filter(function(Repo $r) {
                 return $this->owner->username == $r->owner->username;
-            });
+            })->all());
+
+            return $repos;
         }
 
         /** 
          * Get repositories contributed to
          *
-         * @return \Gogs\Lib\Collection
+         * @return Repos
          */
         public function contributions() {
+            $repos = new Repos($this->url, $this->token, $this->owner);
+
             if (empty($this->owner))
-                return new \Gogs\Lib\Collection();
+                return $repos;
 
-            return $this->filter(function(Repo $r) {
+            $repos->add($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;
-            });
-        }
+            })->all());
 
-        /** 
-         * 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;
-            });
+            return $repos;
         }
     }