Completed
Branch feature/pre-split (8b986a)
by Anton
06:31
created

PartialTrait::partial()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Entities\Relations\Traits;
8
9
/**
10
 * Provides ability to skip loading data from database for realtion.
11
 */
12
trait PartialTrait
13
{
14
    /**
15
     * @var bool
16
     */
17
    private $autoload = true;
18
19
    /**
20
     * Partial selections will not be autoloaded.
21
     *
22
     * Example:
23
     *
24
     * $post = $this->findPost(); //no comments
25
     * $post->comments->partial(true);
26
     * $post->comments->add(new Comment());
27
     * assert($post->comments->count() == 1); //no other comments to be loaded
28
     *
29
     * $post->comments->add($comment);
30
     *
31
     * @param bool $partial
32
     *
33
     * @return self|$this
0 ignored issues
show
Comprehensibility Bug introduced by
The return type PartialTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
34
     */
35
    public function partial(bool $partial = true)
36
    {
37
        $this->autoload = !$partial;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @return bool
44
     */
45
    public function isPartial(): bool
46
    {
47
        return !$this->autoload;
48
    }
49
}