BillogramApiObjectsSingletonObject

Represents a remote singleton object on Billogram.

Defined (1)

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

/Billogram/Api/Objects/SingletonObject.php  
  1. class SingletonObject 
  2. protected $objectClass = null; 
  3. protected $urlName = null; 
  4. protected $data = null; 
  5. protected $api; 
  6.  
  7. /** 
  8. * Constructor sets a url endpoint for the resource 
  9. * @param $api 
  10. * @param $urlName 
  11. */ 
  12. public function __construct($api, $urlName) 
  13. $this->api = $api; 
  14. $this->urlName = $urlName; 
  15.  
  16. /** 
  17. * String representation of the object 
  18. * @return string 
  19. */ 
  20. public function __toString() 
  21. return "<Billogram object '" . $this->url() . "'" . ($this->data === null ? " (lazy)" : "") . ">"; 
  22.  
  23. /** 
  24. * Returns the API url where you can receive this object. 
  25. * @return null|string 
  26. */ 
  27. public function url() 
  28. if ($this->urlName) 
  29. return $this->urlName; 
  30. if ($this->objectClass) 
  31. return $this->objectClass->url($this); 
  32. return null; 
  33.  
  34. /** 
  35. * Makes a GET request and refreshes the local data with up-to-date info. 
  36. * @return $this 
  37. */ 
  38. public function refresh() 
  39. $response = $this->api->get($this->url()); 
  40. $this->data = (array) $response->data; 
  41.  
  42. return $this; 
  43.  
  44. /** 
  45. * Updates the API object with $data. 
  46. * @param $data 
  47. * @return $this 
  48. */ 
  49. public function update($data) 
  50. $response = $this->api->put($this->url(), $data); 
  51. $this->data = (array) $response->data; 
  52.  
  53. return $this; 
  54.  
  55. /** 
  56. * Wrapper method to easier access the specific parameters 
  57. * @param $key 
  58. * @return null 
  59. * @throws \Billogram\Api\Exceptions\UnknownFieldError 
  60. */ 
  61. public function __get($key) 
  62. switch ($key) { 
  63. case 'data': 
  64. if (!$this->data) 
  65. $this->refresh(); 
  66.  
  67. return $this->data; 
  68.  
  69. default: 
  70. if (!$this->data) 
  71. $this->refresh(); 
  72. if (array_key_exists($key, $this->data)) 
  73. return $this->data[$key]; 
  74. throw new UnknownFieldError("Invalid parameter: " . $key);