<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Mail\Header\Exception;

use 
Zend\Mail\Exception\ExceptionInterface as MailException;

interface 
ExceptionInterface extends MailException
{
}
etadataFeature extends AbstractFeature
{

    
/**
     * @var MetadataInterface
     */
    
protected $metadata = null;

    
/**
     * Constructor
     *
     * @param MetadataInterface $metadata
     */
    
public function __construct(MetadataInterface $metadata = null)
    {
        if (
$metadata) {
            
$this->metadata = $metadata;
        }
        
$this->sharedData['metadata'] = array(
            
'primaryKey' => null,
            
'columns' => array()
        );
    }

    public function 
postInitialize()
    {
        if (
$this->metadata == null) {
            
$this->metadata = new Metadata($this->tableGateway->adapter);
        }

        
// localize variable for brevity
        
$t = $this->tableGateway;
        
$m = $this->metadata;

        
// get column named
        
$columns = $m->getColumnNames($t->table);
        
$t->columns = $columns;

        
// set locally
        
$this->sharedData['metadata']['columns'] = $columns;

        
// process primary key
        
$pkc = null;

        foreach (
$m->getConstraints($t->table) as $constraint) {
            
/** @var $constraint \Zend\Db\Metadata\Object\ConstraintObject */
            
if ($constraint->getType() == 'PRIMARY KEY') {
                
$pkc = $constraint;
                break;
            }
        }

        if (
$pkc === null) {
            throw new 
Exception\RuntimeException('A primary key for this column could not be found in the metadata.');
        }

        if (
count($pkc->getColumns()) == 1) {
            
$pkck = $pkc->getColumns();
            
$primaryKey = $pkck[0];
        } else {
            
$primaryKey = $pkc->getColumns();
        }

        
$this->sharedData['metadata']['primaryKey'] = $primaryKey;
    }


}