|
@@ -10,7 +10,7 @@ namespace Gogs\API\Request {
|
|
|
* and get the cURL functionality.
|
|
|
*
|
|
|
* @author Joachim M. Giaever (joachim[]giaever.org)
|
|
|
- * @abstact
|
|
|
+ * @version 0.1.4
|
|
|
*/
|
|
|
abstract class Base implements RequestInterface {
|
|
|
|
|
@@ -141,7 +141,7 @@ namespace Gogs\API\Request {
|
|
|
|
|
|
$ret = $this->method_post(...$args);
|
|
|
|
|
|
- $this->json_set_property($this->json_decode($ret));
|
|
|
+ $this->json_set_property((object)$this->json_decode($ret));
|
|
|
|
|
|
return true;
|
|
|
}
|
|
@@ -186,7 +186,7 @@ namespace Gogs\API\Request {
|
|
|
|
|
|
$this->json_error();
|
|
|
|
|
|
- return $obj;
|
|
|
+ return (object)$obj;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -215,6 +215,37 @@ namespace Gogs\API\Request {
|
|
|
throw new Exception\RequestErrorException(json_last_error_msg(), $err);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Set properties for the current object.
|
|
|
+ *
|
|
|
+ * Each child class must implement this to set its data. Will
|
|
|
+ * be called by methods such as `load` and from collection
|
|
|
+ * classes.
|
|
|
+ *
|
|
|
+ * Will return true/false for singel objects but an array on collections.
|
|
|
+ * The array will contain the newly inserted elements. This to prevent
|
|
|
+ * additional iterations.
|
|
|
+ *
|
|
|
+ * This method should also set loaded to true or false, depending
|
|
|
+ * on success or failure.
|
|
|
+ *
|
|
|
+ * @see Collection
|
|
|
+ * @param mixed $obj
|
|
|
+ * @return true|array Array with keys on collections
|
|
|
+ */
|
|
|
+ protected function json_set_property(\stdClass $obj) {
|
|
|
+
|
|
|
+ foreach ($obj as $key => $value) {
|
|
|
+ if ($this->property_exists($key))
|
|
|
+ $this->{$key} = $value;
|
|
|
+ else
|
|
|
+ echo "Unknown proerty " . $key . "\n";
|
|
|
+ }
|
|
|
+ $this->loaded = true;
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* Get basename of a class (remove namespace).
|
|
|
*
|
|
@@ -402,26 +433,6 @@ namespace Gogs\API\Request {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * Set properties for the current object.
|
|
|
- *
|
|
|
- * Each child class must implement this to set its data. Will
|
|
|
- * be called by methods such as `load` and from collection
|
|
|
- * classes.
|
|
|
- *
|
|
|
- * Will return true/false for singel objects but an array on collections.
|
|
|
- * The array will contain the newly inserted elements. This to prevent
|
|
|
- * additional iterations.
|
|
|
- *
|
|
|
- * This method should also set loaded to true or false, depending
|
|
|
- * on success or failure.
|
|
|
- *
|
|
|
- * @see Collection
|
|
|
- * @param mixed $obj
|
|
|
- * @return true|array
|
|
|
- */
|
|
|
- abstract protected function json_set_property($obj);
|
|
|
-
|
|
|
|
|
|
* Set the scope for the request methods accepted by the child.
|
|
|
*
|
|
@@ -438,6 +449,52 @@ namespace Gogs\API\Request {
|
|
|
*/
|
|
|
abstract protected function set_scope(string $method);
|
|
|
|
|
|
+
|
|
|
+ * Search for an matching object.
|
|
|
+ *
|
|
|
+ * Methods do OR-ing and not AND-ing by default.
|
|
|
+ *
|
|
|
+ * Params should be key (object property) and value that
|
|
|
+ * this parameter should match, e.g
|
|
|
+ *
|
|
|
+ * ```
|
|
|
+ * $repo->search(
|
|
|
+ * "name" => "this",
|
|
|
+ * "owner" => array(
|
|
|
+ * "username" => "that"
|
|
|
+ * )
|
|
|
+ * );
|
|
|
+ * ```
|
|
|
+ *
|
|
|
+ * will match `"this" IN $repo->name OR "that" IN $repo->owner->username` .
|
|
|
+ *
|
|
|
+ * @param array $params Parameters
|
|
|
+ * @param bool $strict Turn search into AND-ing, require match in each field.
|
|
|
+ * @throws Exception\SearchParamException when invalid property
|
|
|
+ * @return true
|
|
|
+ */
|
|
|
+ protected function search(array $params = array(), bool $strict = false) {
|
|
|
+
|
|
|
+ if (empty($params))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ foreach ($params as $key => $value) {
|
|
|
+ if (!$this->property_exists($key))
|
|
|
+ throw new Exception\SearchParamException("Invalid property exception");
|
|
|
+
|
|
|
+ if (is_array($value) && !$strict && $this->{$key}->search($value, $strict))
|
|
|
+ return true;
|
|
|
+ else if (is_array($value) && $strict && $this->{$key}->search($value, $strict))
|
|
|
+ return false;
|
|
|
+ else if (!$strict && stripos($this->{$key}, $value) !== false)
|
|
|
+ return true;
|
|
|
+ else if ($strict && stripos($this->{$key},$value) === false)
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return (!$strict ? false : true);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|