Completed
Push — master ( dd5a61...384355 )
by Dmitry
27:40
created

src/Command.php (1 issue)

Check that arguments can be used as reference when one is expected

Bug Minor

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
/**
3
 * HiPanel API client made with HiART.
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-hiart
6
 * @package   hipanel-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\hiart;
12
13
use hiqdev\hiart\AbstractResponse;
14
use yii\helpers\Json;
15
16
/**
17
 * Command for HiPanel API.
18
 */
19
class Command extends \hiqdev\hiart\Command
20
{
21
    public function search($options = [])
22
    {
23
        /** @var AbstractResponse $response */
24
        $response = parent::search($options);
25
26
        if ($this->request->getQuery()->getOption('batch')) {
27
            return $response;
28
        }
29
30
        if ($response->getData() === []) {
31
            return $this->fakeResponseWithData($response, '');
32
        }
33
34
        return $this->fakeResponseWithData($response, Json::encode(reset($response->getData())));
0 ignored issues
show
$response->getData() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
35
    }
36
37
    private function fakeResponseWithData(AbstractResponse $response, $data)
38
    {
39
        $newResponse = clone $response;
40
        $newResponseReflection = new \ReflectionObject($newResponse);
41
42
        $isDecodedProperty = $newResponseReflection->getProperty('isDecoded');
43
        $isDecodedProperty->setAccessible(true);
44
        $isDecodedProperty->setValue($newResponse, false);
45
        $isDecodedProperty->setAccessible(false);
46
47
        $rawDataProperty = $newResponseReflection->getProperty('rawData');
48
        $rawDataProperty->setAccessible(true);
49
        $rawDataProperty->setValue($newResponse, $data);
50
        $rawDataProperty->setAccessible(false);
51
52
        return $newResponse;
53
    }
54
}
55