Completed
Push — master ( 3a869f...beb3fd )
by John
12s
created

toAPIResponseData()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
namespace LunixREST\Server\Router\Endpoint\ResourceEndpoint;
3
4
use LunixREST\Server\APIResponse\APIResponseData;
5
use LunixREST\Server\Router\Endpoint\ResourceEndpoint\Exceptions\UnableToCreateAPIResponseDataException;
6
7
/**
8
 * abstract class ResourceAPIResponseDataFactory
9
 * @package LunixREST\Server\Router\Endpoint\ResourceEndpoint
10
 */
11
abstract class ResourceAPIResponseDataFactory {
12
13
    /**
14
     * @param null|Resource $resource
15
     * @return APIResponseData
16
     * @throws UnableToCreateAPIResponseDataException
17
     */
18
    abstract public function toAPIResponseData(?Resource $resource): APIResponseData;
19
20
    /**
21
     * Uses above method for each resource, extracting the data into an array, creating a new APIResponseData from that array.
22
     * @param null|Resource[] $resources
23
     * @return APIResponseData
24
     * @throws UnableToCreateAPIResponseDataException
25
     */
26 10
    public function multipleToAPIResponseData(?array $resources): APIResponseData
27
    {
28 10
        if(is_null($resources)) {
29 2
            return $this->toAPIResponseData(null);
30
        }
31
32 8
        return new APIResponseData(array_map(function(Resource $resource) {
33 7
            return $this->toAPIResponseData($resource)->getData();
1 ignored issue
show
Documentation introduced by
$resource is of type object<LunixREST\Server\...ourceEndpoint\Resource>, but the function expects a null|resource.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34 8
        }, $resources));
35
    }
36
}
37