Google\Spreadsheet\SpreadsheetService::getResourceById PHP Method

getResourceById() public method

All classes which have a "getId()" method can be used. e.g. - SpreadsheetFeed - Spreadsheet - WorksheetFeed - Worksheet - ListFeed - CellFeed
public getResourceById ( string $resource, string $id ) : Object
$resource string the full path of the class
$id string the id (full url) of the resource
return Object
    public function getResourceById($resource, $id)
    {
        try {
            return new $resource(new \SimpleXMLElement(ServiceRequestFactory::getInstance()->get($id)));
        } catch (BadRequestException $e) {
            throw new ResourceNotFoundException($e->getMessage());
        }
    }

Usage Example

 /**
  * @expectedException Google\Spreadsheet\Exception\ResourceNotFoundException
  */
 public function testGetResourceByIdException()
 {
     $resourceId = "http://resource";
     $mockRequest = $this->getMockBuilder(DefaultServiceRequest::class)->setMethods(["get"])->disableOriginalConstructor()->getMock();
     $mockRequest->expects($this->once())->method("get")->with($this->equalTo($resourceId))->will($this->throwException(new BadRequestException()));
     ServiceRequestFactory::setInstance($mockRequest);
     $spreadsheetService = new SpreadsheetService();
     $spreadsheet = $spreadsheetService->getResourceById(Spreadsheet::class, $resourceId);
 }