1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP SDK library for the Superdesk Content API. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Superdesk\ContentApiSdk\Api\Pagerfanta; |
16
|
|
|
|
17
|
|
|
use Superdesk\ContentApiSdk\Api\Request\RequestInterface; |
18
|
|
|
use Superdesk\ContentApiSdk\Client\ApiClientInterface; |
19
|
|
|
use Superdesk\ContentApiSdk\ContentApiSdk; |
20
|
|
|
use Superdesk\ContentApiSdk\Data\Package; |
21
|
|
|
use Superdesk\ContentApiSdk\Exception\InvalidDataException; |
22
|
|
|
use Exception; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Adapter for package. |
26
|
|
|
*/ |
27
|
|
|
class PackageAdapter extends ResourceAdapter |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* SDK Instance. |
31
|
|
|
* |
32
|
|
|
* @var ContentApiSdk |
33
|
|
|
*/ |
34
|
|
|
protected $apiInstance; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Resolve package associations. |
38
|
|
|
* |
39
|
|
|
* @var boolean |
40
|
|
|
*/ |
41
|
|
|
protected $resolveAssociations; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Instantiate object. |
45
|
|
|
* |
46
|
|
|
* @param ApiClientInterface $client HTTP client |
47
|
|
|
* @param RequestInterface $request API Request is_object(var) |
48
|
|
|
* @param ContentApiSdk $apiInstance SDK Instance |
49
|
|
|
* @param boolean $resolveAssociations Resolve package associations |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
ApiClientInterface $client, |
53
|
|
|
RequestInterface $request, |
54
|
|
|
ContentApiSdk $apiInstance, |
55
|
|
|
$resolveAssociations |
56
|
|
|
) { |
57
|
|
|
parent::__construct($client, $request); |
58
|
|
|
|
59
|
|
|
$this->apiInstance = $apiInstance; |
60
|
|
|
$this->resolveAssociations = $resolveAssociations; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function getSlice($offset, $length) |
67
|
|
|
{ |
68
|
|
|
$packages = array(); |
69
|
|
|
$resources = parent::getSlice($offset, $length); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
foreach ($resources as $itemData) { |
|
|
|
|
73
|
|
|
$packages[] = new Package($itemData); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($this->resolveAssociations) { |
77
|
|
|
foreach ($packages as $id => $package) { |
78
|
|
|
$associations = $this->apiInstance->getAssociationsFromPackage($package); |
79
|
|
|
$packages[$id] = $this->apiInstance->injectAssociations($package, $associations); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} catch (Exception $e) { |
83
|
|
|
throw new InvalidDataException('Could not convert resources to packages.', $e->getCode(), $e); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $packages; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.