From 5f05996f8bbc3151ae773c3756ad3e9365629d34 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Mon, 30 Jun 2014 15:48:47 -0400 Subject: [PATCH] Add a method to return an ID from an href. --- lib/recurly/base.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/recurly/base.php b/lib/recurly/base.php index eee95888..d5e58eaa 100644 --- a/lib/recurly/base.php +++ b/lib/recurly/base.php @@ -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 {}