Completed
Push — master ( 535b5e...300324 )
by Jasper
13s queued 13s
created

AbstractManyRelation::hasIncluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Swis\JsonApi\Client\Relations;
4
5
use Swis\JsonApi\Client\Collection;
6
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface;
7
8
/**
9
 * @property \Swis\JsonApi\Client\Collection|null $included
10
 */
11
abstract class AbstractManyRelation extends AbstractRelation implements ManyRelationInterface
12
{
13
    /**
14
     * @param \Swis\JsonApi\Client\Collection $included
15
     *
16
     * @return $this
17
     */
18 85
    public function associate(Collection $included)
19
    {
20 85
        $this->included = $included;
21
22 85
        return $this;
23
    }
24
25
    /**
26
     * @return bool
27
     */
28 10
    public function hasIncluded(): bool
29
    {
30 10
        return $this->getIncluded()->isNotEmpty();
31
    }
32
33
    /**
34
     * @return \Swis\JsonApi\Client\Collection
35
     */
36 80
    public function getIncluded(): Collection
37
    {
38 80
        return $this->included ?: new Collection();
39
    }
40
41
    /**
42
     * Sort the included collection by the given key.
43
     * You can also pass your own callback to determine how to sort the collection values.
44
     *
45
     * @param callable $callback
46
     * @param int      $options
47
     * @param bool     $descending
48
     *
49
     * @return $this
50
     */
51 5
    public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
52
    {
53
        // Included may be empty when defining the relation (on the item),
54
        // but will be filled when using the relation to fetch the data.
55
        // Checking if we have included items and applying the order is
56
        // simpler then keeping track of the sorts and applying them later.
57 5
        if ($this->hasIncluded()) {
58 5
            $this->included = $this->getIncluded()->sortBy($callback, $options, $descending);
59
        }
60
61 5
        return $this;
62
    }
63
}
64