|
1
|
|
|
<?php namespace Limoncello\Flute\Schema; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Contracts\Data\ModelSchemeInfoInterface; |
|
20
|
|
|
use Limoncello\Flute\Contracts\Schema\JsonSchemesInterface; |
|
21
|
|
|
use Limoncello\Flute\Contracts\Schema\SchemaInterface; |
|
22
|
|
|
use Limoncello\Flute\Exceptions\InvalidSchemeFactoryException; |
|
23
|
|
|
use Neomerx\JsonApi\Contracts\Schema\SchemaFactoryInterface; |
|
24
|
|
|
use Neomerx\JsonApi\Schema\Container; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @package Limoncello\Flute |
|
28
|
|
|
*/ |
|
29
|
|
|
class JsonSchemes extends Container implements JsonSchemesInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var ModelSchemeInfoInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $modelSchemes; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param SchemaFactoryInterface $factory |
|
38
|
|
|
* @param array $schemas |
|
39
|
|
|
* @param ModelSchemeInfoInterface $modelSchemes |
|
40
|
|
|
*/ |
|
41
|
40 |
|
public function __construct(SchemaFactoryInterface $factory, array $schemas, ModelSchemeInfoInterface $modelSchemes) |
|
42
|
|
|
{ |
|
43
|
40 |
|
parent::__construct($factory, $schemas); |
|
44
|
40 |
|
$this->modelSchemes = $modelSchemes; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
|
|
*/ |
|
50
|
8 |
|
public function getRelationshipSchema(string $schemaClass, string $relationshipName): SchemaInterface |
|
51
|
|
|
{ |
|
52
|
8 |
|
assert( |
|
53
|
8 |
|
class_exists($schemaClass) === true && |
|
54
|
8 |
|
in_array(SchemaInterface::class, class_implements($schemaClass)) === true |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
/** @var SchemaInterface $schemaClass */ |
|
58
|
|
|
|
|
59
|
8 |
|
$modelRelName = $schemaClass::getMappings()[SchemaInterface::SCHEMA_RELATIONSHIPS][$relationshipName]; |
|
60
|
8 |
|
$targetSchema = $this->getModelRelationshipSchema($schemaClass::MODEL, $modelRelName); |
|
61
|
|
|
|
|
62
|
8 |
|
return $targetSchema; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
8 |
|
public function getModelRelationshipSchema(string $modelClass, string $relationshipName): SchemaInterface |
|
69
|
|
|
{ |
|
70
|
8 |
|
$reverseModelClass = $this->getModelSchemes()->getReverseModelClass($modelClass, $relationshipName); |
|
71
|
|
|
|
|
72
|
|
|
/** @var SchemaInterface $targetSchema */ |
|
73
|
8 |
|
$targetSchema = $this->getSchemaByType($reverseModelClass); |
|
74
|
|
|
|
|
75
|
8 |
|
return $targetSchema; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return ModelSchemeInfoInterface |
|
80
|
|
|
*/ |
|
81
|
17 |
|
protected function getModelSchemes(): ModelSchemeInfoInterface |
|
82
|
|
|
{ |
|
83
|
17 |
|
return $this->modelSchemes; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
|
87
|
|
|
* @param callable $callable |
|
88
|
|
|
* |
|
89
|
|
|
* @return SchemaInterface |
|
90
|
|
|
* |
|
91
|
|
|
* @codeCoverageIgnore |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function createSchemaFromCallable(callable $callable) |
|
94
|
|
|
{ |
|
95
|
|
|
assert($callable); |
|
96
|
|
|
|
|
97
|
|
|
// callable as Scheme factory is not supported. |
|
98
|
|
|
throw new InvalidSchemeFactoryException(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
|
102
|
|
|
* @param string $className |
|
103
|
|
|
* |
|
104
|
|
|
* @return SchemaInterface |
|
105
|
|
|
*/ |
|
106
|
17 |
|
protected function createSchemaFromClassName($className) |
|
107
|
|
|
{ |
|
108
|
17 |
|
$schema = new $className($this->getFactory(), $this->getModelSchemes()); |
|
109
|
|
|
|
|
110
|
17 |
|
return $schema; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|