1 | <?php |
||
18 | class RelationOptions |
||
19 | { |
||
20 | /** |
||
21 | * @var RelationDefinition |
||
22 | */ |
||
23 | private $definition; |
||
24 | |||
25 | /** |
||
26 | * Most of relations provides ability to specify many different configuration options, such |
||
27 | * as key names, pivot table schemas, foreign key request, ability to be nullabe and etc. |
||
28 | * |
||
29 | * To simple schema definition in real projects we can fill some of this values automatically |
||
30 | * based on some "environment" values such as parent/outer record table, role name, primary key |
||
31 | * and etc. |
||
32 | * |
||
33 | * Example: |
||
34 | * Record::INNER_KEY => '{outer:role}_{outer:primaryKey}' |
||
35 | * |
||
36 | * Result: |
||
37 | * Outer Record is User with primary key "id" => "user_id" |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | private $template; |
||
42 | |||
43 | /** |
||
44 | * Options calculated based on values provided by RelationDefinition and calculated via |
||
45 | * template. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | private $options; |
||
50 | |||
51 | /** |
||
52 | * @param RelationDefinition $definition |
||
53 | * @param array $template Relation options template. |
||
54 | */ |
||
55 | public function __construct(RelationDefinition $definition, array $template = []) |
||
62 | |||
63 | /** |
||
64 | * Get value for a specific option. Attention, option MUST exist in template in order to |
||
65 | * be retrievable. |
||
66 | * |
||
67 | * @param string $option |
||
68 | * |
||
69 | * @return mixed |
||
70 | * |
||
71 | * @throws OptionsException |
||
72 | */ |
||
73 | public function define(string $option) |
||
81 | |||
82 | /** |
||
83 | * All relation options. |
||
84 | * |
||
85 | * @param array $options Options to be defined. |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | public function defineMultiple(array $options): array |
||
93 | |||
94 | /** |
||
95 | * Calculate options based on given template |
||
96 | * |
||
97 | * @param array $userOptions Options provided by user. |
||
98 | * |
||
99 | * @return array Calculated options. |
||
100 | */ |
||
101 | protected function calculateOptions(array $userOptions): array |
||
124 | |||
125 | /** |
||
126 | * Create set of options to specify missing relation definition fields. |
||
127 | * |
||
128 | * @param array $userOptions User options. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | protected function proposedOptions(array $userOptions): array |
||
187 | } |