This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestifier.php
120 lines (112 loc) · 3.68 KB
/
Restifier.php
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
118
119
120
<?php
namespace Wandu\Restifier;
use InvalidArgumentException;
use Wandu\Restifier\Contracts\Restifiable;
use Wandu\Restifier\Exception\NotFoundTransformerException;
class Restifier implements Restifiable
{
/** @var callable[] */
protected $transformers = [];
public function __construct(array $transformers = [])
{
foreach ($transformers as $name => $transformer) {
$this->addTransformer($name, $transformer);
}
}
/**
* @param string $name
* @param callable $transformer
*/
public function addTransformer(string $name, callable $transformer)
{
if (is_callable($transformer)) {
$this->transformers[$name] = $transformer;
return;
}
throw new InvalidArgumentException("Argument 2 passed to pushTransformer() must be callable");
}
/**
* {@inheritdoc}
*/
public function restify($resource, array $includes = [], callable $transformer = null)
{
if ($resource === null) return null;
if (!$transformer) {
$transformer = $this->findTransformer($resource);
}
$parsedIncludes = $this->parseIncludes($includes, $resource);
$entity = call_user_func($transformer, $resource, $this, $parsedIncludes);
foreach ($parsedIncludes as $key => $nextIncludes) {
if (is_object($transformer) && method_exists($transformer, $key)) {
$entity = array_merge(
$entity,
$transformer->{$key}($resource, $this, $nextIncludes)
);
}
}
return $entity;
}
/**
* {@inheritdoc}
*/
public function restifyMany($resource, array $includes = [], callable $transformer = null): array
{
$result = [];
foreach ($resource as $key => $value) {
$result[$key] = $this->restify($value, $includes, $transformer);
}
return $result;
}
protected function findTransformer($resource)
{
$className = null;
if (is_object($resource) && $className = get_class($resource)) {
if (array_key_exists($className, $this->transformers)) {
return $this->transformers[$className];
}
foreach ($this->transformers as $name => $transformer) {
if ($resource instanceof $name) {
return $transformer;
}
}
}
if ($className) {
throw new NotFoundTransformerException(
sprintf("cannot find the transformer named %s.", $className)
);
}
throw new NotFoundTransformerException("resource is not an object.");
}
/**
* @param array $includes
* @param mixed $resource
* @return array
*/
private function parseIncludes(array $includes = [], $resource)
{
$parsedIncludes = [];
foreach ($includes as $include => $condition) {
if (is_integer($include)) {
$include = $condition;
$condition = true;
}
while (is_callable($condition)) {
$condition = call_user_func($condition, $resource);
}
if (!$condition) continue;
if (strpos($include, '.') === false) {
$key = $include;
$param = null;
} else {
list($key, $param) = explode('.', $include, 2);
}
if (!isset($parsedIncludes[$key])) {
$parsedIncludes[$key] = [];
}
if ($param) {
$parsedIncludes[$key][] = $param;
}
}
return $parsedIncludes;
}
}