| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Licensed to CRATE Technology GmbH("Crate") under one or more contributor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * license agreements.  See the NOTICE file distributed with this work for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * additional information regarding copyright ownership.  Crate licenses | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * this file to you under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * you may not use this file except in compliance with the License.  You may | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * License for the specific language governing permissions and limitations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * However, if you have executed another commercial license agreement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * with Crate these terms will supersede the license and you may use the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * software solely pursuant to the terms of the relevant commercial agreement. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | namespace Crate\DBAL\Driver\PDOCrate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Crate\DBAL\Platforms\Crate057Platform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Crate\DBAL\Platforms\CratePlatform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Doctrine\DBAL\VersionAwarePlatformDriver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class Driver implements \Doctrine\DBAL\Driver, VersionAwarePlatformDriver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     const VERSION = '0.2.1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     const NAME = 'crate'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     const SCHEMA_MIN_VERSION = '0.57.0'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return PDOConnection The database connection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 6 |  |     public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 6 |  |         return new PDOConnection($this->constructPdoDsn($params), $username, $password, $driverOptions); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Constructs the Crate PDO DSN. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return string The DSN. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 6 |  |     private function constructPdoDsn(array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  |         $dsn = self::NAME . ':'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |         if (isset($params['host']) && $params['host'] != '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 6 |  |             $dsn .= $params['host']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $dsn .= 'localhost'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |         $dsn .= ':' . (isset($params['port']) ? $params['port'] : '4200'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |         return $dsn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function getDatabasePlatform() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return new \Crate\DBAL\Platforms\CratePlatform(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |     public function getSchemaManager(\Doctrine\DBAL\Connection $conn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |         return new \Crate\DBAL\Schema\CrateSchemaManager($conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     public function getName() | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         return self::NAME; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |     public function getDatabase(\Doctrine\DBAL\Connection $conn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 6 |  |     public function createDatabasePlatformForVersion($version) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 6 |  |         if (version_compare($version, self::SCHEMA_MIN_VERSION, ">=")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             return new Crate057Platform(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 6 |  |             return new CratePlatform(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                        
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.