RelationContext::getColumn()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Schemas\Definitions;
9
10
use Spiral\Database\Schemas\ColumnInterface;
11
use Spiral\Database\Schemas\Prototypes\AbstractColumn;
12
use Spiral\Database\Schemas\Prototypes\AbstractTable;
13
use Spiral\ORM\Exceptions\DefinitionException;
14
use Spiral\ORM\Exceptions\SchemaException;
15
use Spiral\ORM\Schemas\SchemaInterface;
16
17
/**
18
 * Defines set of properties relates to source or target model/record. Including class, location
19
 * in database, role name, primary key (if any).
20
 *
21
 * Attention, at this moment relations do not support multiple primary keys.
22
 */
23
final class RelationContext
24
{
25
    /**
26
     * Record class (source or target).
27
     *
28
     * @var string
29
     */
30
    private $class;
31
32
    /**
33
     * Role name.
34
     *
35
     * @var string
36
     */
37
    private $role;
38
39
    /**
40
     * @var string|null
41
     */
42
    private $database;
43
44
    /**
45
     * @var string
46
     */
47
    private $table;
48
49
    /**
50
     * @invisible
51
     *
52
     * @var AbstractTable
53
     */
54
    private $schema;
55
56
    /**
57
     * @param SchemaInterface $schema
58
     * @param AbstractTable   $table
59
     *
60
     * @return RelationContext
61
     *
62
     * @throws SchemaException
63
     */
64
    public static function createContent(SchemaInterface $schema, AbstractTable $table): self
65
    {
66
        $context = new self();
67
        $context->class = $schema->getClass();
68
        $context->database = $schema->getDatabase();
69
        $context->table = $schema->getTable();
70
        $context->role = $schema->getRole();
71
72
        $context->schema = $table;
73
74
        return $context;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getClass(): string
81
    {
82
        return $this->class;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getRole(): string
89
    {
90
        return $this->role;
91
    }
92
93
    /**
94
     * @return null|string
95
     */
96
    public function getDatabase()
97
    {
98
        return $this->database;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getTable(): string
105
    {
106
        return $this->table;
107
    }
108
109
    /**
110
     * @return ColumnInterface
111
     */
112
    public function getPrimary(): ColumnInterface
113
    {
114
        //We are always expecting to have primary key
115
        return $this->getColumn($this->schema->getPrimaryKeys()[0]);
116
    }
117
118
    /**
119
     * @param string $name
120
     *
121
     * @return AbstractColumn
122
     */
123
    public function getColumn(string $name): AbstractColumn
124
    {
125
        if (!$this->schema->hasColumn($name)) {
126
            throw new DefinitionException("Undefined column {$name} in {$this->schema->getName()}");
127
        }
128
129
        return clone $this->schema->column($name);
130
    }
131
}