Skip to content

Commit

Permalink
implement custom types and proxies for describe
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed May 17, 2017
1 parent 46f2dd6 commit 53df755
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 58 deletions.
127 changes: 69 additions & 58 deletions src/Dbal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ORM\Dbal\Column;
use ORM\Dbal\Type;
use ORM\Dbal\TypeInterface;
use ORM\Exceptions\NotScalar;
use ORM\Exceptions\UnsupportedDriver;

Expand All @@ -28,58 +29,6 @@ abstract class Dbal
protected static $registeredTypes = [];
protected static $typeMapping = [];

public static function setQuotingCharacter($char)
{
static::$quotingCharacter = $char;
}

public static function setIdentifierDivider($divider)
{
static::$identifierDivider = $divider;
}

public static function setBooleanTrue($true)
{
static::$booleanTrue = $true;
}

public static function setBooleanFalse($false)
{
static::$booleanFalse = $false;
}

/**
* @return string
*/
public static function getQuotingCharacter()
{
return static::$quotingCharacter;
}

/**
* @return string
*/
public static function getIdentifierDivider()
{
return static::$identifierDivider;
}

/**
* @return string
*/
public static function getBooleanTrue()
{
return static::$booleanTrue;
}

/**
* @return string
*/
public static function getBooleanFalse()
{
return static::$booleanFalse;
}

/**
* Dbal constructor.
*
Expand Down Expand Up @@ -224,6 +173,65 @@ public function delete(Entity $entity)
return true;
}

public static function registerType($type)
{
if (!in_array($type, static::$registeredTypes)) {
array_unshift(static::$registeredTypes, $type);
}
}

public static function setQuotingCharacter($char)
{
static::$quotingCharacter = $char;
}

public static function setIdentifierDivider($divider)
{
static::$identifierDivider = $divider;
}

public static function setBooleanTrue($true)
{
static::$booleanTrue = $true;
}

public static function setBooleanFalse($false)
{
static::$booleanFalse = $false;
}

/**
* @return string
*/
public static function getQuotingCharacter()
{
return static::$quotingCharacter;
}

/**
* @return string
*/
public static function getIdentifierDivider()
{
return static::$identifierDivider;
}

/**
* @return string
*/
public static function getBooleanTrue()
{
return static::$booleanTrue;
}

/**
* @return string
*/
public static function getBooleanFalse()
{
return static::$booleanFalse;
}

/**
* Build the insert statement for $entity
*
Expand Down Expand Up @@ -271,13 +279,16 @@ protected function extractParenthesis($type)
protected function getType($columnDefinition)
{
if (isset(static::$typeMapping[$columnDefinition['data_type']])) {
return (static::$typeMapping[$columnDefinition['data_type']])::factory($columnDefinition);
return call_user_func([static::$typeMapping[$columnDefinition['data_type']], 'factory'], $columnDefinition);
} else {
// foreach (self::$registeredTypes as $class) {
// if ($type = $class::fromDefinition($columnDefinition)) {
// return $type;
// }
// }
foreach (self::$registeredTypes as $class) {
if ($type = $class::fromDefinition($columnDefinition)) {
if (!$type instanceof TypeInterface) {
throw new Exception('Returned object does not implement TypeInterface');
}
return $type;
}
}

return new Type\Text();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Dbal/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

abstract class Type implements TypeInterface
{
/**
* {@inheritdoc}
* @codeCoverageIgnore void method for types covered by mapping
*/
public static function fromDefinition($columnDefinitoin)
{
return null;
Expand Down
18 changes: 18 additions & 0 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ORM;

use ORM\Dbal\Column;
use ORM\Exceptions\IncompletePrimaryKey;
use ORM\Exceptions\InvalidConfiguration;
use ORM\Exceptions\InvalidRelation;
Expand Down Expand Up @@ -105,6 +106,11 @@ abstract class Entity implements \Serializable
* @var \ReflectionClass[] */
protected static $reflections = [];

/** The fetched column descriptions for the table.
* @internal
* @var Column[][] */
protected static $descriptions;

/**
* Get the table name
*
Expand Down Expand Up @@ -335,6 +341,18 @@ public static function isAutoIncremented()
return count(static::getPrimaryKeyVars()) > 1 ? false : static::$autoIncrement;
}

/**
* Get an array of Columns for this table.
*
* @param EntityManager $em
* @return mixed
* @codeCoverageIgnore This is just a proxy
*/
public static function describe(EntityManager $em)
{
return $em->describe(static::getTableName());
}

/**
* Enforce $namingScheme to $name
*
Expand Down
21 changes: 21 additions & 0 deletions src/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ORM;

use ORM\Dbal\Column;
use ORM\Dbal\Other;
use ORM\Exceptions\IncompletePrimaryKey;
use ORM\Exceptions\InvalidConfiguration;
Expand Down Expand Up @@ -57,6 +58,10 @@ class EntityManager
* @var array */
protected $options = [];

/** Already fetched column descriptions
* @var Column[][] */
protected $descriptions = [];

/**
* Constructor
*
Expand Down Expand Up @@ -373,6 +378,7 @@ public function getDbal()
*
* @param mixed $value The variable that should be returned in SQL syntax
* @return string
* @codeCoverageIgnore This is just a proxy
*/
public function escapeValue($value)
{
Expand All @@ -384,9 +390,24 @@ public function escapeValue($value)
*
* @param string $identifier Identifier to quote
* @return string
* @codeCoverageIgnore This is just a proxy
*/
public function escapeIdentifier($identifier)
{
return $this->getDbal()->escapeIdentifier($identifier);
}

/**
* Returns an array of columns from $table.
*
* @param string $table
* @return Dbal\Column[]
*/
public function describe($table)
{
if (!isset($this->descriptions[$table])) {
$this->descriptions[$table] = $this->getDbal()->describe($table);
}
return $this->descriptions[$table];
}
}
16 changes: 16 additions & 0 deletions tests/Dbal/Type/Custom/Dbal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ORM\Test\Dbal\Type\Custom;

class Dbal extends \ORM\Dbal
{
public static function getRegisteredTypes()
{
return self::$registeredTypes;
}

public static function resetRegisteredTypes()
{
self::$registeredTypes = [];
}
}
10 changes: 10 additions & 0 deletions tests/Dbal/Type/Custom/Point.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ORM\Test\Dbal\Type\Custom;

use ORM\Dbal\Type;

class Point extends Type
{

}

0 comments on commit 53df755

Please sign in to comment.