BillogramApiModelsSimpleClass

Represents a collection of remote objects on the Billogram service.

Defined (1)

The class is defined in the following location(s).

/Billogram/Api/Models/SimpleClass.php  
  1. class SimpleClass 
  2. public $objectClass = 'Billogram\Api\Objects\SimpleObject'; 
  3. protected $api; 
  4. protected $urlName; 
  5. protected $objectIdField; 
  6.  
  7. /** 
  8. * Constructor sets the base url and significant id field for the resource. 
  9. */ 
  10. public function __construct($api, $urlName, $objectIdField) 
  11. $this->api = $api; 
  12. $this->urlName = $urlName; 
  13. $this->objectIdField = $objectIdField; 
  14.  
  15. /** 
  16. * Create a query for objects of this type. 
  17. * @return \Billogram\Api\Query 
  18. */ 
  19. public function query() 
  20. return new Query($this->api, $this); 
  21.  
  22. /** 
  23. * Finds an object by id $objectId and returns an object. 
  24. * @param $objectId 
  25. * @return \Billogram\Api\Objects\SimpleObject 
  26. */ 
  27. public function get($objectId) 
  28. $response = $this->api->get($this->url($objectId)); 
  29. $className = $this->objectClass; 
  30.  
  31. return new $className($this->api, $this, $response->data); 
  32.  
  33. /** 
  34. * Makes a POST request to the API and creates a new object. 
  35. * @param $data 
  36. * @return \Billogram\Api\Objects\SimpleObject 
  37. */ 
  38. public function create($data) 
  39. $response = $this->api->post($this->url(), $data); 
  40. $className = $this->objectClass; 
  41.  
  42. return new $className($this->api, $this, $response->data); 
  43.  
  44. /** 
  45. * Formats and returns a URL to an object or object id. 
  46. * @param null $object 
  47. * @return string 
  48. */ 
  49. public function url($object = null) 
  50. if (is_object($object)) { 
  51. $objectIdField = $this->objectIdField; 
  52.  
  53. return $this->urlName . '/' . $object->$objectIdField; 
  54. } elseif ($object) { 
  55. return $this->urlName . '/' . $object; 
  56.  
  57. return $this->urlName;