Completed
Branch feature/pre-split (899df2)
by Anton
03:08
created

SourceTrait::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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