Overview

Namespaces

  • JP
    • LeanMapperExtension

Classes

  • JP\LeanMapperExtension\LeanMapperExtension
  • JP\LeanMapperExtension\Mapper

Interfaces

  • JP\LeanMapperExtension\IEntityProvider
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 
<?php

    namespace JP\LeanMapperExtension;

    use LeanMapper\Exception\InvalidStateException;
    use LeanMapper\Row;


    class Mapper extends \LeanMapper\DefaultMapper
    {
        const K_ENTITY_CLASS = 'entityClass',
            K_PRIMARY_KEY = 'primaryKey';

        /** @var  array  [tableName => [entityClass, primaryKey]] */
        protected $tables;

        /** @var  array  [entityClass => tableName] */
        protected $entities;

        /** @var  array  [repositoryClass => tableName] */
        protected $repositories;


        public function __construct($defaultEntityNamespace = NULL)
        {
            if (is_string($defaultEntityNamespace)) {
                $this->defaultEntityNamespace = $defaultEntityNamespace;
            }
        }


        /**
         * Registers entity
         * @param  string|NULL  table name in database
         * @param  string|NULL
         * @param  string|NULL
         * @param  string|NULL
         * @return self
         */
        public function register($tableName, $entityClass = NULL, $repositoryClass = NULL, $primaryKey = NULL)
        {
            if (isset($this->tables[$tableName])) {
                throw new InvalidStateException("Table '$tableName' is already registered for entity " . $this->tables[$tableName][self::K_ENTITY_CLASS]);
            }

            if (isset($this->entities[$entityClass])) {
                throw new InvalidStateException("Entity $entityClass is already registered for table '{$this->entities[$entityClass]}'");
            }

            if (isset($this->repositories[$repositoryClass])) {
                throw new InvalidStateException("Repository $repositoryClass is already registered for table '{$this->repositories[$repositoryClass]}'");
            }

            $this->tables[$tableName] = array(
                self::K_ENTITY_CLASS => $entityClass,
                self::K_PRIMARY_KEY => $primaryKey,
            );

            if (is_string($entityClass)) {
                $this->entities[$entityClass] = $tableName;
            }

            if (is_string($repositoryClass)) {
                $this->repositories[$repositoryClass] = $tableName;
            } elseif ($repositoryClass !== NULL) {
                throw new \RuntimeException('RepositoryClass must be string or NULL, ' . gettype($repositoryClass) . ' given');
            }
            return $this;
        }

        /**
         * @inheritdoc
         */
        public function getPrimaryKey($table)
        {
            if (isset($this->tables[$table][self::K_PRIMARY_KEY])) {
                return $this->tables[$table][self::K_PRIMARY_KEY];
            }
            return parent::getPrimaryKey($table);
        }

        /**
         * @inheritdoc
         */
        public function getTable($entityClass)
        {
            if (isset($this->entities[$entityClass])) {
                return $this->entities[$entityClass];
            }
            return parent::getTable($entityClass);
        }

        /**
         * @inheritdoc
         */
        public function getEntityClass($table, Row $row = NULL)
        {
            if (isset($this->tables[$table][self::K_ENTITY_CLASS])) {
                return $this->tables[$table][self::K_ENTITY_CLASS];
            }
            return parent::getEntityClass($table, $row);
        }

        /**
         * @inheritdoc
         */
        public function getTableByRepositoryClass($repositoryClass)
        {
            if (isset($this->repositories[$repositoryClass])) {
                return $this->repositories[$repositoryClass];
            }
            return parent::getTableByRepositoryClass($repositoryClass);
        }
    }
janpecha/leanmapper-extension v0.9.2 API documentation API documentation generated by ApiGen