|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lyal\Checkr\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait Getable |
|
6
|
|
|
{ |
|
7
|
|
|
private $getPath; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract functions to imppose requirements for the exhibiting class. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract public function getAttribute($key); |
|
13
|
|
|
|
|
14
|
|
|
abstract public function getResourceName($object = null); |
|
15
|
|
|
|
|
16
|
|
|
abstract public function getAttributes($sanitized = true); |
|
17
|
|
|
|
|
18
|
|
|
abstract public function processPath($path = null, array $values = null); |
|
19
|
|
|
|
|
20
|
|
|
abstract public function getClient(); |
|
21
|
|
|
|
|
22
|
|
|
abstract public function setValues($values); |
|
23
|
|
|
|
|
24
|
|
|
abstract public function setAttributes(array $values); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Make a get request against the path. |
|
28
|
|
|
* |
|
29
|
|
|
* Note: options for Guzzle are set/handled on the Client |
|
30
|
|
|
* |
|
31
|
|
|
* @param string|null $path |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function getRequest($path = null, $parameters = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$parameters = $parameters ?? $this->getAttributes(); |
|
38
|
|
|
|
|
39
|
|
|
if (method_exists($this, 'getEmbeddedResources')) { |
|
40
|
|
|
$parameters['include'] = $this->getEmbeddedResources(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ($parameters) { |
|
44
|
|
|
$path .= '?'.http_build_query($parameters); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->getClient()->request('get', $path); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Load a resource via a get call. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array|null $parameters |
|
54
|
|
|
* |
|
55
|
|
|
* @return $this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function load(array $parameters = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$parameters = $parameters ?? ['id' => $this->getAttribute('id')]; |
|
60
|
|
|
$path = $this->processPath($this->getLoadPath() ?? $this->getResourceName().'/'.$this->getAttribute('id')); |
|
61
|
|
|
$this->setValues($this->getRequest($path, $parameters)); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getLoadPath() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->getPath; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setLoadPath($path) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->getPath = $path; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|