AbstractEntity::setValues()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 12
c 3
b 1
f 0
dl 0
loc 24
rs 9.5555
cc 5
nc 6
nop 1
1
<?php
2
3
namespace Lyal\Checkr\Entities;
4
5
use Illuminate\Support\Str;
6
use Lyal\Checkr\Client;
7
use Lyal\Checkr\Exceptions\UnknownResourceException;
8
use Lyal\Checkr\Traits\Getable;
9
use Lyal\Checkr\Traits\HasAttributes;
10
11
abstract class AbstractEntity
12
{
13
    use HasAttributes;
14
    use Getable;
15
16
    /**
17
     * Define the field allowed for each resource
18
     * Overridden in individual children.
19
     *
20
     * @var array
21
     */
22
    protected $fields = [];
23
24
    /**
25
     * @var \Lyal\Checkr\Client
26
     */
27
    private $client;
28
29
    /**
30
     * AbstractEntity constructor.
31
     *
32
     * @param string|array|null $values
33
     * @param Client|null       $client
34
     */
35
    public function __construct($values = null, $client = null)
36
    {
37
        $this->setValues($values);
38
        $this->setClient($client);
39
    }
40
41
    /**
42
     * Attach the previously queried resource as classname_id
43
     * Allows for magic querying.
44
     *
45
     * @param AbstractEntity $object
46
     *
47
     * @return void
48
     */
49
    public function setPreviousObject(self $object)
50
    {
51
        $objectId = strtolower((new \ReflectionClass($object))->getShortName()).'_id';
52
        if (null !== $object->getAttribute('id') && $this->checkField($objectId)) {
53
            $this->setAttribute($objectId, $object->getAttribute('id'));
54
        }
55
    }
56
57
    /**
58
     * @param \StdClass|array|string $values
59
     *
60
     * return void
61
     */
62
    public function setValues($values)
63
    {
64
        /*
65
         * If we get a string, we assume that it's an ID here
66
         * to allow for loading objects easily
67
         */
68
        if (is_string($values)) {
69
            $this->setAttribute('id', $values);
70
71
            return;
72
        }
73
74
        foreach ((array) $values as $key => $value) {
75
            if (isset($value->object)) {
76
                $className = checkrEntityClassName($value->object);
77
                $value = new $className($value, $this->getClient());
78
                $value->setPreviousObject($this);
79
            }
80
81
            if (is_array($value)) {
82
                $list = collect($value);
83
                $value = $list;
84
            }
85
            $this->{$key} = $value;
86
        }
87
    }
88
89
    /**
90
     * @throws UnknownResourceException
91
     *
92
     * @return mixed
93
     */
94
    public function __call($name, $args)
95
    {
96
        return $this->getClient()->api($name, $args, $this);
97
    }
98
99
    /**
100
     * Get the client object; Client also handles routing between resources.
101
     *
102
     * @return \Lyal\Checkr\Client
103
     */
104
    public function getClient()
105
    {
106
        return $this->client;
107
    }
108
109
    /**
110
     * Set the client object; Client also handles routing between resources.
111
     *
112
     * @param Client|null $client
113
     *
114
     * @return void;
115
     */
116
    public function setClient(Client $client = null)
117
    {
118
        $this->client = $client;
119
    }
120
121
    /**
122
     * Get the resource name of an object.
123
     *
124
     * AdverseAction becomes adverse_actions
125
     *
126
     * @param AbstractEntity $object
127
     *
128
     * @return string
129
     */
130
    public function getResourceName($object = null)
131
    {
132
        $object = $object ?? $this;
133
134
        return Str::snake(Str::plural((new \ReflectionClass($object))->getShortName()));
135
    }
136
137
    /**
138
     * @param string|null $path
139
     * @param array|null  $values
140
     *
141
     * @return string
142
     */
143
    public function processPath($path = null, array $values = null)
144
    {
145
        $path = $path ?? $this->getResourceName();
146
147
        return str_replace_tokens($path, $values ?? $this->getAttributes(false));
148
    }
149
150
    /**
151
     * Set the fields for an object.
152
     *
153
     * @param array $fields
154
     */
155
    public function setFields($fields = [])
156
    {
157
        $this->fields = $fields;
158
    }
159
160
    /**
161
     * Return the fields for a resource.
162
     *
163
     * @return array
164
     */
165
    public function getFields()
166
    {
167
        return $this->fields;
168
    }
169
}
170