1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Loaders\ColumnMetadataLoader |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Loaders; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Connection\ConnectionInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Loader for table column metadata. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/techdivision/import |
32
|
|
|
* @link http://www.techdivision.com |
33
|
|
|
*/ |
34
|
|
|
class ColumnMetadataLoader implements LoaderInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The column metadata. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $metadata = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Construct a new instance. |
46
|
|
|
* |
47
|
|
|
* @param \TechDivision\Import\Connection\ConnectionInterface $connection The DB connection instance used to load the table metadata |
48
|
|
|
*/ |
49
|
|
|
public function __construct(ConnectionInterface $connection) |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
// initialize the statement to load the table columns |
53
|
|
|
$stmtTables = $connection->query('SHOW TABLES'); |
54
|
|
|
|
55
|
|
|
// fetch the tables names of the actual database |
56
|
|
|
$tables = $stmtTables->fetchAll(\PDO::FETCH_ASSOC); |
57
|
|
|
|
58
|
|
|
// iterate over the rows with the tables |
59
|
|
|
foreach ($tables as $table) { |
60
|
|
|
// extract the tablename |
61
|
|
|
$tableName = reset($table); |
62
|
|
|
// load the table's columns |
63
|
|
|
$stmtColumns = $connection->query(sprintf('SHOW COLUMNS FROM %s', $tableName)); |
64
|
|
|
$columns = $stmtColumns->fetchAll(\PDO::FETCH_ASSOC); |
65
|
|
|
// prepare the metadata |
66
|
|
|
foreach ($columns as $column) { |
67
|
|
|
$this->metadata[$tableName][$column['Field']] = $column; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Loads and returns tables metadata. |
74
|
|
|
* |
75
|
|
|
* @param string $tableName The table name to return the metadata for |
76
|
|
|
* |
77
|
|
|
* @return \ArrayAccess The array with the metadata |
78
|
|
|
*/ |
79
|
|
|
public function load($tableName = null) |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
// query whether or not metadata for the table is available |
83
|
|
|
if (isset($this->metadata[$tableName])) { |
84
|
|
|
return $this->metadata[$tableName]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// return an empty array otherwise |
88
|
|
|
return array(); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.