1 | <?php |
||
7 | class NormalistModels extends Source\AbstractSchemaSource |
||
8 | { |
||
9 | /** |
||
10 | * Current class version |
||
11 | */ |
||
12 | const VERSION = '1.0'; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $model_definition; |
||
18 | |||
19 | /** |
||
20 | * @param array $model_definition |
||
21 | */ |
||
22 | 17 | public function __construct(array $model_definition) |
|
26 | |||
27 | |||
28 | /** |
||
29 | * Get unique keys on table |
||
30 | * |
||
31 | * @param string $table table name |
||
32 | * @param boolean $include_primary include primary keys in the list |
||
33 | * @throws Exception\InvalidArgumentException |
||
34 | * @throws Exception\ErrorException |
||
35 | * @throws Exception\NoPrimaryKeyException |
||
36 | * @throws Exception\ExceptionInterface |
||
37 | * @throws Exception\TableNotFoundException |
||
38 | * @return array |
||
39 | */ |
||
40 | 1 | public function getUniqueKeys($table, $include_primary = false) |
|
41 | { |
||
42 | 1 | $this->checkTableArgument($table); |
|
43 | 1 | return $this->model_definition['tables'][$table]['unique_keys']; |
|
44 | } |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Return indexes information on a table |
||
49 | * |
||
50 | * @param string $table table name |
||
51 | * @throws Exception\InvalidArgumentException |
||
52 | * @throws Exception\ErrorException |
||
53 | * @throws Exception\ExceptionInterface |
||
54 | * @throws Exception\TableNotFoundException |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function getIndexesInformation($table) |
||
59 | { |
||
60 | $this->checkTableArgument($table); |
||
61 | return $this->model_definition['tables'][$table]['indexes']; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Return unique table primary key |
||
66 | * |
||
67 | * @throws Exception\InvalidArgumentException |
||
68 | * @throws Exception\ErrorException |
||
69 | * @throws Exception\NoPrimaryKeyException when no pk |
||
70 | * @throws Exception\MultiplePrimaryKeyException when multiple pk found |
||
71 | * @throws Exception\ExceptionInterface |
||
72 | * @throws Exception\TableNotFoundException |
||
73 | * |
||
74 | * @param string $table |
||
75 | * @return string|int primary key |
||
76 | */ |
||
77 | 4 | public function getPrimaryKey($table) |
|
78 | { |
||
79 | 4 | $this->checkTableArgument($table); |
|
80 | 3 | $pks = $this->getPrimaryKeys($table); |
|
81 | 2 | if (count($pks) > 1) { |
|
82 | 1 | $keys = implode(',', $pks); |
|
83 | 1 | throw new Exception\MultiplePrimaryKeyException(__METHOD__ . ". Multiple primary keys found on table '$table': $keys"); |
|
84 | } |
||
85 | 1 | return $pks[0]; |
|
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Return composite primary keys |
||
91 | * |
||
92 | * @throws Exception\InvalidArgumentException |
||
93 | * @throws Exception\ErrorException |
||
94 | * @throws Exception\NoPrimaryKeyException |
||
95 | * @throws Exception\ExceptionInterface |
||
96 | * @throws Exception\TableNotFoundException |
||
97 | * |
||
98 | * @param string $table |
||
99 | * @return array primary keys |
||
100 | */ |
||
101 | 6 | public function getPrimaryKeys($table) |
|
102 | { |
||
103 | 6 | $this->checkTableArgument($table); |
|
104 | 5 | $pks = $this->model_definition['tables'][$table]['primary_keys']; |
|
105 | 5 | if (count($pks) == 0) { |
|
106 | 2 | throw new Exception\NoPrimaryKeyException(__METHOD__ . ". No primary keys found on table '$table'."); |
|
107 | } |
||
108 | 3 | return $pks; |
|
109 | } |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Return column information |
||
114 | * |
||
115 | * @throws Exception\InvalidArgumentException |
||
116 | * @throws Exception\ErrorException |
||
117 | * @throws Exception\ExceptionInterface |
||
118 | * @throws Exception\TableNotFoundException |
||
119 | * |
||
120 | * @param string $table |
||
121 | * @return array associative array [column_name => infos] |
||
122 | */ |
||
123 | 1 | public function getColumnsInformation($table) |
|
124 | { |
||
125 | 1 | $this->checkTableArgument($table); |
|
126 | 1 | return $this->model_definition['tables'][$table]['columns']; |
|
127 | } |
||
128 | |||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 1 | public function getForeignKeys($table) |
|
134 | { |
||
135 | 1 | $this->checkTableArgument($table); |
|
136 | 1 | return $this->model_definition['tables'][$table]['foreign_keys']; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function getSchemaConfig() |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function getReferences($table) |
||
151 | { |
||
152 | $this->checkTableArgument($table); |
||
153 | return $this->model_definition['tables'][$table]['references']; |
||
154 | } |
||
155 | |||
156 | |||
157 | /** |
||
158 | * Return table informations |
||
159 | * |
||
160 | * @throws Exception\InvalidArgumentException |
||
161 | * @throws Exception\ErrorException |
||
162 | * @throws Exception\ExceptionInterface |
||
163 | * |
||
164 | * @return array associative array indexed by table_name |
||
165 | */ |
||
166 | 1 | public function getTablesInformation() |
|
170 | } |
||
171 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.