-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathResource.php
More file actions
117 lines (102 loc) · 2.25 KB
/
Resource.php
File metadata and controls
117 lines (102 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Interfax
*
* (C) InterFAX, 2016
*
* @package interfax/interfax
* @author Interfax <dev@interfax.net>
* @author Mike Smith <mike.smith@camc-ltd.co.uk>
* @copyright Copyright (c) 2016, InterFAX
* @license MIT
*/
namespace Interfax;
use Interfax\Exception\RequestException;
/**
* Class Resource
*
* Base Resource Class to be used for resource classes that are represented by specific endpoints on the API.
*
* @package Interfax
*/
abstract class Resource
{
/**
* @var GenericFactory
*/
protected $factory;
/**
* @var Client
*/
protected $client;
/**
* Base URI used for carrying out actions on the resource.
*
* @var string
*/
protected $resource_uri;
/**
* Stores the internal properties of the resource.
*
* @var array
*/
protected $record = [];
/**
* Should be overridden in inheriting class
*
* @var
*/
protected static $resource_uri_stem;
/**
* Resource constructor.
*
* @param Client $client
* @param $id
* @param array $definition
* @param GenericFactory|null $factory
*/
public function __construct(Client $client, $id, $definition = [], GenericFactory $factory = null)
{
$this->client = $client;
$this->resource_uri = static::$resource_uri_stem . $id;
$this->record['id'] = $id;
foreach ($definition as $k => $v) {
$this->record[$k] = $v;
}
if ($factory === null) {
$factory = new GenericFactory();
}
$this->factory = $factory;
}
/**
* @param $name
* @return mixed|null
*/
public function __get($name)
{
if ($name === 'location') {
return $this->resource_uri;
}
if (array_key_exists($name, $this->record)) {
return $this->record[$name];
}
return null;
}
/**
* @return array
*/
public function attributes()
{
return $this->record;
}
/**
* @return self
* @throws RequestException
*/
public function refresh()
{
$response = $this->client->get($this->resource_uri);
$this->record = $response;
return $this;
}
}