1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Osnova\Services; |
4
|
|
|
|
5
|
|
|
use Osnova\Api\ApiProvider; |
6
|
|
|
use Osnova\OsnovaResource; |
7
|
|
|
use Osnova\Support\Traits\BuildsEntities; |
8
|
|
|
use Osnova\Support\Traits\HasApiProvider; |
9
|
|
|
use Osnova\Support\Traits\HasOsnovaResource; |
10
|
|
|
|
11
|
|
|
abstract class ServiceEntity |
12
|
|
|
{ |
13
|
|
|
use HasApiProvider, HasOsnovaResource, BuildsEntities; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $data = []; |
17
|
|
|
|
18
|
|
|
/** @var null|ApiProvider */ |
19
|
|
|
protected $apiProvider; |
20
|
|
|
|
21
|
|
|
/** @var OsnovaResource */ |
22
|
|
|
protected $resource; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ServiceEntity constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $data |
28
|
|
|
* @param ApiProvider|null $apiProvider |
29
|
|
|
* @param OsnovaResource $resource = null |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $data = [], ApiProvider $apiProvider = null, OsnovaResource $resource = null) |
32
|
|
|
{ |
33
|
|
|
$this->data = $data; |
34
|
|
|
$this->apiProvider = $apiProvider; |
35
|
|
|
$this->resource = $resource; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get entity data or value at given path. |
40
|
|
|
* |
41
|
|
|
* @param string|null $path Path. |
42
|
|
|
* @param string|null $default Default value. |
43
|
|
|
* |
44
|
|
|
* @return array|string|mixed |
45
|
|
|
*/ |
46
|
|
|
public function getData(string $path = null, string $default = null) |
47
|
|
|
{ |
48
|
|
|
if (is_null($path)) { |
49
|
|
|
return $this->data; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$keys = explode('.', $path); |
53
|
|
|
|
54
|
|
|
// Quick getters |
55
|
|
|
|
56
|
|
|
if (count($keys) === 1) { |
57
|
|
|
return $this->data[$keys[0]] ?? $default; |
58
|
|
|
} elseif (count($keys) === 2) { |
59
|
|
|
return $this->data[$keys[0]][$keys[1]] ?? $default; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$data = $this->data; |
63
|
|
|
|
64
|
|
|
foreach ($keys as $key) { |
65
|
|
|
if (!array_key_exists($keys, $data)) { |
66
|
|
|
return $default; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$data = $data[$key]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Make new entity based on given data path. |
77
|
|
|
* |
78
|
|
|
* @param string $class |
79
|
|
|
* @param string $dataPath |
80
|
|
|
* |
81
|
|
|
* @return ServiceEntity|null |
82
|
|
|
*/ |
83
|
|
|
protected function makeEntity(string $class, string $dataPath) |
84
|
|
|
{ |
85
|
|
|
if (!is_array($data = $this->getData($dataPath)) || empty($data)) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->makeEntityFrom($class, $data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Make new entity based on given data path. |
94
|
|
|
* |
95
|
|
|
* @param string $class |
96
|
|
|
* @param string $dataPath |
97
|
|
|
* |
98
|
|
|
* @return ServiceEntity|null |
99
|
|
|
*/ |
100
|
|
|
protected function makeEntityFrom(string $class, array $data) |
101
|
|
|
{ |
102
|
|
|
return $this->getEntitiesBuilder($class) |
103
|
|
|
->from($data) |
104
|
|
|
->with($this->apiProvider, $this->resource) |
105
|
|
|
->item(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|