Skip to content
Browse files

Dynamic subresources for subscriptions

  • Loading branch information...
1 parent a83bde2 commit b1a22297f06c70d6d50d1b95a67a14f739fad252 @DHS DHS committed Apr 22, 2012
Showing with 74 additions and 22 deletions.
  1. +74 −22 lib/GoCardless/Subscription.php
View
96 lib/GoCardless/Subscription.php
@@ -37,6 +37,68 @@ function __construct($client, array $attrs = null) {
}
}
+ // Check for subresources
+ if (isset($this->sub_resource_uris)) {
+
+ // Loop through each subresource loading it as appropriate object
+ foreach ($this->sub_resource_uris as $key => $value) {
+
+ // Generate subresource endpoint by snipping out the
+ // right part of the url
+ $endpoint = preg_replace('/api\/v[0-9]+\//', '',
+ parse_url($value, PHP_URL_PATH));
+
+ // Add params to endpoint
+ if (parse_url($value, PHP_URL_QUERY)) {
+ $endpoint .= '?' . parse_url($value, PHP_URL_QUERY);
+ }
+
+ // Generate the class name
+ $class = 'GoCardless_' .
+ GoCardless_Utils::camelize(GoCardless_Utils::singularize($key));
+
+ // Create an array for the subresource
+ $this->$key = array();
+
+ // Query the API
+ foreach ($this->client->request('get', $endpoint) as $value) {
+
+ // Load each element into the appropriate class
+ $this->{$key}[] = new $class($this->client, $value);
+
+ }
+
+ }
+
+ // Unset the sub_resource_uris feild as we now have now loaded the
+ // subresources themselves
+ unset($this->sub_resource_uris);
+
+ }
+
+ }
+
+ /**
+ * This magic method is used to call subresources
+ *
+ * @param string $method The name of the method being called
+ *
+ * @return array The subresource index
+ */
+ public function __call($method, $params = array()) {
+
+ // Check the subresource exists
+ if (isset($this->$method)) {
+
+ // Return the subresource
+ return $this->fetch_sub_resource($method, $params);
+
+ }
+
+ // Subresource doesn't exist so error out
+ $class = get_class($this);
+ trigger_error("Call to undefined method $class::$method()", E_USER_ERROR);
+
}
/**
@@ -72,39 +134,29 @@ public static function find_with_client($client, $id) {
}
/**
- * Cancel a subscription in the API
+ * Fetch an object's subresource from the API
*
- * @return object The result of the cancel query
+ * @param string $type The subresource to fetch
+ * @param array $params The params for the subresource query
+ *
+ * @return object The subresource object
*/
- public function cancel() {
+ public function fetch_sub_resource($type, $params = array()) {
- $endpoint = self::$endpoint . '/' . $this->id . '/cancel';
-
- return new self($this->client, $this->client->request('put', $endpoint));
+ return $this->$type;
}
/**
- * Fetch bills created under a subscription from the API
- *
- * @param array $params Params to append to the query ie. for filtering
+ * Cancel a subscription in the API
*
- * @return array Array of bill objects
+ * @return object The result of the cancel query
*/
- public function bills($params = array()) {
-
- $params['source_id'] = $this->id;
-
- $endpoint = GoCardless_Merchant::$endpoint . '/' . $this->merchant_id .
- '/bills';
-
- $objects = array();
+ public function cancel() {
- foreach ($this->client->request('get', $endpoint, $params) as $value) {
- $objects[] = new GoCardless_Bill($this->client, $value);
- }
+ $endpoint = self::$endpoint . '/' . $this->id . '/cancel';
- return $objects;
+ return new self($this->client, $this->client->request('put', $endpoint));
}

0 comments on commit b1a2229

Please sign in to comment.
Something went wrong with that request. Please try again.