Completed
Pull Request — master (#78)
by Jasper
05:00
created

AbstractManyRelation::getIncluded()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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|false|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 230
    public function associate(Collection $included)
19
    {
20 230
        $this->included = $included;
21
22 230
        return $this;
23
    }
24
25
    /**
26
     * @return \Swis\JsonApi\Client\Collection
27
     */
28 185
    public function getIncluded(): Collection
29
    {
30 185
        return $this->included ?: new Collection();
31
    }
32
33
    /**
34
     * Sort the included collection by the given key.
35
     * You can also pass your own callback to determine how to sort the collection values.
36
     *
37
     * @param callable $callback
38
     * @param int      $options
39
     * @param bool     $descending
40
     *
41
     * @return $this
42
     */
43 5
    public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
44
    {
45
        // Included may be empty when defining the relation (on the item),
46
        // but will be filled when using the relation to fetch the data.
47
        // Checking if we have included items and applying the order is
48
        // simpler then keeping track of the sorts and applying them later.
49 5
        if ($this->hasIncluded()) {
50 5
            $this->included = $this->getIncluded()->sortBy($callback, $options, $descending);
51
        }
52
53 5
        return $this;
54
    }
55
}
56