Completed
Push — master ( ba10fa...52fde2 )
by Anton
04:45
created

SourceTrait::source()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\ORM\Traits;
10
11
use Interop\Container\ContainerInterface;
12
use Spiral\Core\Component;
13
use Spiral\Core\Exceptions\ScopeException;
14
use Spiral\ORM\Entities\RecordSelector;
15
use Spiral\ORM\Entities\RecordSource;
16
use Spiral\ORM\ORMInterface;
17
18
/**
19
 * Static record functionality including create and find methods.
20
 */
21
trait SourceTrait
22
{
23
    /**
24
     * Get record selector for a given query.
25
     *
26
     * Example:
27
     * User::find(['status' => 'active'];
28
     *
29
     * @param array $query Selection WHERE statement.
30
     *
31
     * @return RecordSelector
32
     *
33
     * @throws ScopeException
34
     */
35
    public static function find(array $query = []): RecordSelector
36
    {
37
        return static::source()->find($query);
38
    }
39
40
    /**
41
     * Fetch one record based on provided query or return null. Make sure to specify sort by in
42
     * order to stabilize selection
43
     *
44
     * Example:
45
     * User::findOne(['name' => 'Wolfy-J'], ['id' => 'DESC'], ['profile']);
46
     *
47
     *
48
     * @param array $where  Selection WHERE statement.
49
     * @param array $sortBy Sort by.
50
     * @param array $load   Relations to pre-load.
51
     *
52
     * @return \Spiral\ORM\RecordEntity|null     *
53
     * @throws ScopeException
54
     */
55
    public static function findOne($where = [], array $sortBy = [], array $load = [])
56
    {
57
        return static::source()->findOne($where, $sortBy, $load);
58
    }
59
60
    /**
61
     * Find record using it's primary key and load children.
62
     *
63
     * Example:
64
     * User::findByPK(1, ['profile']);
65
     *
66
     * @param mixed $primaryKey Primary key.
67
     * @param array $load       Relations to pre-load.
68
     *
69
     * @return \Spiral\ORM\RecordEntity|null
70
     *
71
     * @throws ScopeException
72
     */
73
    public static function findByPK($primaryKey, array $load = [])
74
    {
75
        return static::source()->findByPK($primaryKey, $load);
76
    }
77
78
    /**
79
     * Instance of RecordSource associated with specific model.
80
     *
81
     * @see   Component::staticContainer()
82
     **
83
     * @return RecordSource
84
     *
85
     * @throws ScopeException
86
     */
87
    public static function source(): RecordSource
88
    {
89
        /**
90
         * Container to be received via global scope.
91
         *
92
         * @var ContainerInterface $container
93
         */
94
        //Via global scope
95
        $container = self::staticContainer();
96
97
        if (empty($container)) {
98
            //Via global scope
99
            throw new ScopeException(sprintf(
100
                "Unable to get '%s' source, no container scope is available",
101
                static::class
102
            ));
103
        }
104
105
        return $container->get(ORMInterface::class)->source(static::class);
106
    }
107
108
    /**
109
     * Trait can ONLY be added to components.
110
     *
111
     * @see Component
112
     *
113
     * @param ContainerInterface|null $container
114
     *
115
     * @return ContainerInterface|null
116
     */
117
    abstract protected static function staticContainer(ContainerInterface $container = null);
118
}