1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Metadata; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
18
|
|
|
use Silverback\ApiComponentBundle\Entity\Utility\FileInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This is to explicitly define the output class of any resource implementing FilterInterface |
22
|
|
|
* This is so that the Data Transformer will be called. |
23
|
|
|
* |
24
|
|
|
* @author Daniel West <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class FileInterfaceOutputClassMetadataFactory implements ResourceMetadataFactoryInterface |
27
|
|
|
{ |
28
|
|
|
private ResourceMetadataFactoryInterface $decorated; |
29
|
|
|
|
30
|
|
|
public function __construct(ResourceMetadataFactoryInterface $decorated) |
31
|
|
|
{ |
32
|
|
|
$this->decorated = $decorated; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function create(string $resourceClass): ResourceMetadata |
36
|
|
|
{ |
37
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
38
|
|
|
if (!is_subclass_of($resourceClass, FileInterface::class)) { |
39
|
|
|
return $resourceMetadata; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!$currentOutputClass = $resourceMetadata->getAttribute('output')) { |
|
|
|
|
43
|
|
|
$attributes = $resourceMetadata->getAttributes() ?: []; |
44
|
|
|
$resourceMetadata = $resourceMetadata->withAttributes(array_merge($attributes, [ |
45
|
|
|
'output' => [ |
46
|
|
|
'class' => $resourceClass, |
47
|
|
|
'name' => (new \ReflectionClass($resourceClass))->getShortName(), |
48
|
|
|
], |
49
|
|
|
])); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $resourceMetadata; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|