Passed
Pull Request — master (#206)
by Alex
05:39
created

SerialiseUtilitiesTrait::checkSingleElementInput()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 16/02/20
6
 * Time: 1:19 PM
7
 */
8
9
namespace AlgoWeb\PODataLaravel\Serialisers;
10
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Support\Collection;
13
use POData\Common\InvalidOperationException;
14
use POData\Providers\Query\QueryResult;
15
16
trait SerialiseUtilitiesTrait
17
{
18
    /**
19
     * @param QueryResult $entryObjects
20
     * @throws InvalidOperationException
21
     */
22
    protected function checkElementsInput(QueryResult &$entryObjects)
23
    {
24
        $res = $entryObjects->results;
25
        if (!(is_array($res) || $res instanceof Collection)) {
26
            throw new InvalidOperationException('!is_array($entryObjects->results)');
27
        }
28
        if (is_array($res) && 0 == count($res)) {
29
            $entryObjects->hasMore = false;
30
        }
31
        if ($res instanceof Collection && 0 == $res->count()) {
32
            $entryObjects->hasMore = false;
33
        }
34
    }
35
36
    /**
37
     * @param QueryResult $entryObject
38
     * @throws InvalidOperationException
39
     */
40
    protected function checkSingleElementInput(QueryResult $entryObject)
41
    {
42
        if (!$entryObject->results instanceof Model) {
43
            $res = $entryObject->results;
44
            $msg = is_array($res) ? 'Entry object must be single Model' : get_class($res);
45
            throw new InvalidOperationException($msg);
46
        }
47
    }
48
}
49