1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\Core\Domain; |
15
|
|
|
|
16
|
|
|
use League\JsonGuard\Validator; |
17
|
|
|
use League\JsonReference\Dereferencer; |
18
|
|
|
use OAuth2Framework\Component\Core\Message\OAuth2Message; |
19
|
|
|
use League\JsonReference\LoaderManager; |
20
|
|
|
|
21
|
|
|
class DomainConverter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Dereferencer |
25
|
|
|
*/ |
26
|
|
|
private $dereferencer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $options; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* DomainConverter constructor. |
35
|
|
|
* |
36
|
|
|
* @param DomainUriLoader $domainUriLoader |
37
|
|
|
*/ |
38
|
|
|
public function __construct(DomainUriLoader $domainUriLoader) |
39
|
|
|
{ |
40
|
|
|
$this->options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; |
41
|
|
|
$loaderManager = new LoaderManager(['https' => $domainUriLoader]); |
42
|
|
|
$this->dereferencer = new Dereferencer(); |
43
|
|
|
$this->dereferencer->setLoaderManager($loaderManager); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $jsonData |
48
|
|
|
* |
49
|
|
|
* @return DomainObject |
50
|
|
|
* |
51
|
|
|
* @throws OAuth2Message |
52
|
|
|
*/ |
53
|
|
|
public function fromJson(string $jsonData): DomainObject |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$decoded = json_decode($jsonData, false, $this->options); |
57
|
|
|
if (!$decoded instanceof \stdClass) { |
58
|
|
|
throw new \RuntimeException('Unable to decode'); |
59
|
|
|
} |
60
|
|
|
if (!property_exists($decoded, '$schema')) { |
61
|
|
|
throw new \InvalidArgumentException('The object is not a valid Json object from the domain.'); |
62
|
|
|
} |
63
|
|
|
$schema = $this->dereferencer->dereference($decoded->{'$schema'}); |
64
|
|
|
|
65
|
|
|
$validator = new Validator($decoded, $schema); |
66
|
|
|
|
67
|
|
|
if ($validator->fails()) { |
68
|
|
|
throw new \InvalidArgumentException('The domain object cannot be verified with the selected schema.'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$class = $decoded->type; |
72
|
|
|
if (!class_exists($class)) { |
73
|
|
|
throw new \InvalidArgumentException(sprintf('Unsupported class "%s"', $class)); |
74
|
|
|
} |
75
|
|
|
$domainObject = $class::createFromJson($decoded); |
76
|
|
|
} catch (\Exception $e) { |
77
|
|
|
throw new OAuth2Message(500, OAuth2Message::ERROR_INTERNAL, 'The server encountered and internal error.', $e); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $domainObject; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param DomainObject $domainObject |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
* |
88
|
|
|
* @throws OAuth2Message |
89
|
|
|
*/ |
90
|
|
|
public function toJson(DomainObject $domainObject): string |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$jsonData = (object) $domainObject->jsonSerialize(); |
94
|
|
|
$schema = $this->dereferencer->dereference($jsonData->{'$schema'}); |
95
|
|
|
|
96
|
|
|
$validator = new Validator($jsonData, $schema); |
97
|
|
|
|
98
|
|
|
if ($validator->fails()) { |
99
|
|
|
throw new \InvalidArgumentException('The domain object cannot be verified with the selected schema.'); |
100
|
|
|
} |
101
|
|
|
} catch (\Exception $e) { |
102
|
|
|
throw new OAuth2Message(500, OAuth2Message::ERROR_INTERNAL, 'The server encountered and internal error.', $e); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return json_encode($jsonData, $this->options); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: