Completed
Push — master ( 882cbc...a9ee6a )
by
unknown
15:23
created

src/jsonApi/AttributionBasedResourceFinder.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace hiapi\jsonApi;
5
6
use WoohooLabs\Yin\JsonApi\Schema\Resource\ResourceInterface;
7
8
final class AttributionBasedResourceFinder
9
{
10
    private ResourceDocumentFactoryInterface $resourceDocumentFactory;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
11
12
    public function __construct(ResourceDocumentFactoryInterface $resourceDocumentFactory)
13
    {
14
        $this->resourceDocumentFactory = $resourceDocumentFactory;
15
    }
16
17
    public function getResource(string $documentClassName): ResourceInterface
18
    {
19
        return $this->resourceDocumentFactory->getResourceByClassName(
20
            $this->findNearbyAttributionClass($documentClassName)
21
        );
22
    }
23
24
    private function findNearbyAttributionClass(string $documentClassName): string
25
    {
26
        $namespace = implode('\\', array_slice(explode($documentClassName, '\\'), 0, -1));
27
        $className = basename($documentClassName);
28
        $entityName = str_replace(['CollectionDocument', 'Document'], '', $className);
29
30
        $result = sprintf('%s\%s', $namespace, $entityName);
31
32
        return $result;
33
    }
34
35
}
36