Completed
Branch feature/pre-split (ce4b6b)
by Anton
03:56
created

AbstractSchema::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Schemas\Relations;
8
9
use Spiral\ORM\Exceptions\OptionsException;
10
use Spiral\ORM\Helpers\RelationOptions;
11
use Spiral\ORM\ORMInterface;
12
use Spiral\ORM\Schemas\Definitions\RelationDefinition;
13
use Spiral\ORM\Schemas\RelationInterface;
14
15
/**
16
 * Basic class for SQL specific relations.
17
 */
18
abstract class AbstractSchema implements RelationInterface
19
{
20
    /**
21
     * Relation type to be stored in packed schema.
22
     */
23
    const RELATION_TYPE = null;
24
25
    /**
26
     * Options to be packed in schema (not all options are required in runtime).
27
     */
28
    const PACK_OPTIONS = [];
29
30
    /**
31
     * Most of relations provides ability to specify many different configuration options, such
32
     * as key names, pivot table schemas, foreign key request, ability to be nullabe and etc.
33
     *
34
     * To simple schema definition in real projects we can fill some of this values automatically
35
     * based on some "environment" values such as parent/outer record table, role name, primary key
36
     * and etc.
37
     *
38
     * Example:
39
     * Record::INNER_KEY => '{outer:role}_{outer:primaryKey}'
40
     *
41
     * Result:
42
     * Outer Record is User with primary key "id" => "user_id"
43
     *
44
     * @var array
45
     */
46
    const OPTIONS_TEMPLATE = [];
47
48
    /**
49
     * @var RelationDefinition
50
     */
51
    protected $definition;
52
53
    /**
54
     * Provides ability to define missing relation options based on template.
55
     *
56
     * @see self::OPTIONS_TEMPLATE
57
     * @var RelationOptions
58
     */
59
    protected $options;
60
61
    /**
62
     * @param RelationDefinition $definition
63
     */
64
    public function __construct(RelationDefinition $definition)
65
    {
66
        $this->definition = $definition;
67
        $this->options = new RelationOptions($definition, static::OPTIONS_TEMPLATE);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getDefinition(): RelationDefinition
74
    {
75
        return $this->definition;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function packRelation(): array
82
    {
83
        return [
84
            ORMInterface::R_TYPE    => static::RELATION_TYPE,
85
            ORMInterface::R_OPTIONS => $this->options->defineMultiple(static::PACK_OPTIONS)
86
        ];
87
    }
88
89
    /**
90
     * Define relation configuration option.
91
     *
92
     * @param string $option
93
     *
94
     * @return mixed
95
     *
96
     * @throws OptionsException
97
     */
98
    protected function option(string $option)
99
    {
100
        return $this->options->define($option);
101
    }
102
}