From: Mikael Nordfeldth Date: Sat, 25 Jun 2016 09:52:17 +0000 (+0200) Subject: getByUri shorthand function for Managed_DataObject (with uri) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=42a62da7649dc2910bff56c838526d0a7f454e50;p=quix0rs-gnu-social.git getByUri shorthand function for Managed_DataObject (with uri) --- diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php index 89263ee778..8c9a55f2b6 100644 --- a/classes/Managed_DataObject.php +++ b/classes/Managed_DataObject.php @@ -383,6 +383,9 @@ abstract class Managed_DataObject extends Memcached_DataObject static function getByID($id) { + if (!property_exists(get_called_class(), 'id')) { + throw new ServerException('Trying to get undefined property of dataobject class.'); + } if (empty($id)) { throw new EmptyPkeyValueException(get_called_class(), 'id'); } @@ -391,6 +394,24 @@ abstract class Managed_DataObject extends Memcached_DataObject return static::getByPK(array('id' => $id)); } + static function getByUri($uri) + { + if (!property_exists(get_called_class(), 'uri')) { + throw new ServerException('Trying to get undefined property of dataobject class.'); + } + if (empty($uri)) { + throw new EmptyPkeyValueException(get_called_class(), 'uri'); + } + + $class = get_called_class(); + $obj = new $class(); + $obj->uri = $uri; + if (!$obj->find(true)) { + throw new NoResultException($obj); + } + return $obj; + } + /** * Returns an ID, checked that it is set and reasonably valid *