upgrade zend (#1559)
[openemr.git] / vendor / zendframework / zend-db / src / TableGateway / Feature / MetadataFeature.php
blob55ad5823012090ae2a997646a4cc2809015cf3a5
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Db\TableGateway\Feature;
12 use Zend\Db\Metadata\MetadataInterface;
13 use Zend\Db\TableGateway\Exception;
14 use Zend\Db\Metadata\Object\TableObject;
15 use Zend\Db\Metadata\Source\Factory as SourceFactory;
17 class MetadataFeature extends AbstractFeature
19 /**
20 * @var MetadataInterface
22 protected $metadata = null;
24 /**
25 * Constructor
27 * @param MetadataInterface $metadata
29 public function __construct(MetadataInterface $metadata = null)
31 if ($metadata) {
32 $this->metadata = $metadata;
34 $this->sharedData['metadata'] = [
35 'primaryKey' => null,
36 'columns' => []
40 public function postInitialize()
42 if ($this->metadata === null) {
43 $this->metadata = SourceFactory::createSourceFromAdapter($this->tableGateway->adapter);
46 // localize variable for brevity
47 $t = $this->tableGateway;
48 $m = $this->metadata;
50 // get column named
51 $columns = $m->getColumnNames($t->table);
52 $t->columns = $columns;
54 // set locally
55 $this->sharedData['metadata']['columns'] = $columns;
57 // process primary key only if table is a table; there are no PK constraints on views
58 if (! ($m->getTable($t->table) instanceof TableObject)) {
59 return;
62 $pkc = null;
64 foreach ($m->getConstraints($t->table) as $constraint) {
65 /** @var $constraint \Zend\Db\Metadata\Object\ConstraintObject */
66 if ($constraint->getType() == 'PRIMARY KEY') {
67 $pkc = $constraint;
68 break;
72 if ($pkc === null) {
73 throw new Exception\RuntimeException('A primary key for this column could not be found in the metadata.');
76 if (count($pkc->getColumns()) == 1) {
77 $pkck = $pkc->getColumns();
78 $primaryKey = $pkck[0];
79 } else {
80 $primaryKey = $pkc->getColumns();
83 $this->sharedData['metadata']['primaryKey'] = $primaryKey;