Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to return an ID from an href. #95

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/recurly/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,31 @@ private static function __createNodeObject($node)
return $new_obj;
}
}

/**
* Return the ID of a resource, using only the href property.
*
* This is most useful for stub objects that haven't been retrieved yet,
* where only the ID is needed instead of the full object.
*
* @throws Recurly_Href_Undefined_Exception
* Thrown when an href was not set when constructing the object.
*
* @return string
* The ID of the Recurly resource, such as a UUID or plan code.
*/
public function idFromHref() {
if (!isset($this->_href)) {
throw new Recurly_Href_Undefined_Exception();
}

// This assumes that the end of _href is an ID.
$parts = parse_url($this->_href);
$path_parts = explode('/', $parts['path']);
return end($path_parts);
}
}

// In case node_class is not specified.
class Recurly_Object {}
class Recurly_Href_Undefined_Exception extends Exception {}