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

AbstractManyRelation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 43
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A associate() 0 5 1
A getIncluded() 0 3 2
A sortBy() 0 11 2
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