1 | <?php |
||
15 | abstract class AbstractRelation implements RelationInterface |
||
16 | { |
||
17 | /** |
||
18 | * Indicates that relation commands must be executed prior to parent command. |
||
19 | */ |
||
20 | const LEADING_RELATION = false; |
||
21 | |||
22 | /** |
||
23 | * Indication that relation have been loaded. |
||
24 | * |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $loaded; |
||
28 | |||
29 | /** |
||
30 | * Parent record. Only read operations! |
||
31 | * |
||
32 | * @invisible |
||
33 | * @var RecordInterface |
||
34 | */ |
||
35 | protected $parent; |
||
36 | |||
37 | /** |
||
38 | * Class name relation points to. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $class; |
||
43 | |||
44 | /** |
||
45 | * Relation schema, defined when ORM being compiled. Check relation config to find out what |
||
46 | * schema producer been used for this relation accessor. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $schema; |
||
51 | |||
52 | /** |
||
53 | * Related data. |
||
54 | * |
||
55 | * @invisible |
||
56 | * @var array|null |
||
57 | */ |
||
58 | protected $data = null; |
||
59 | |||
60 | /** |
||
61 | * Provides ability for lazy-loading model initialization and inner selects. |
||
62 | * |
||
63 | * @invisible |
||
64 | * @var ORMInterface |
||
65 | */ |
||
66 | protected $orm; |
||
67 | |||
68 | /** |
||
69 | * @param string $class Owner model class name. |
||
70 | * @param array $schema |
||
71 | * @param ORMInterface $orm |
||
72 | */ |
||
73 | public function __construct(string $class, array $schema, ORMInterface $orm) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function isLeading(): bool |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function withContext( |
||
92 | RecordInterface $parent, |
||
93 | bool $loaded = false, |
||
94 | array $data = null |
||
95 | ): RelationInterface { |
||
96 | $relation = clone $this; |
||
97 | $relation->parent = $parent; |
||
98 | $relation->loaded = $loaded; |
||
99 | $relation->data = is_null($data) ? [] : $data; |
||
100 | |||
101 | return $relation; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function getClass(): string |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function isLoaded(): bool |
||
119 | |||
120 | /** |
||
121 | * Get value from record based on schema key. |
||
122 | * |
||
123 | * @param RecordInterface $record |
||
124 | * @param string $key |
||
125 | * |
||
126 | * @return mixed |
||
127 | */ |
||
128 | protected function value(RecordInterface $record, string $key) |
||
132 | |||
133 | /** |
||
134 | * Schema value. |
||
135 | * |
||
136 | * @param int $key |
||
137 | * |
||
138 | * @return mixed |
||
139 | */ |
||
140 | protected function key(int $key) |
||
144 | |||
145 | /** |
||
146 | * Get primary key column |
||
147 | * |
||
148 | * @param RecordInterface $record |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | protected function primaryColumnOf(RecordInterface $record): string |
||
156 | |||
157 | /** |
||
158 | * @param $value |
||
159 | */ |
||
160 | protected function assertValid($value) |
||
176 | |||
177 | /** |
||
178 | * If record not synced or can't be synced. Only work for PK based relations. |
||
179 | * |
||
180 | * @param RecordInterface $inner |
||
181 | * @param RecordInterface $outer |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | protected function isSynced(RecordInterface $inner, RecordInterface $outer): bool |
||
199 | } |