Column.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @package ActiveRecord
  4. */
  5. namespace ActiveRecord;
  6. /**
  7. * Class for a table column.
  8. *
  9. * @package ActiveRecord
  10. */
  11. class Column
  12. {
  13. // types for $type
  14. const STRING = 1;
  15. const INTEGER = 2;
  16. const DECIMAL = 3;
  17. const DATETIME = 4;
  18. const DATE = 5;
  19. const TIME = 6;
  20. /**
  21. * Map a type to an column type.
  22. * @static
  23. * @var array
  24. */
  25. static $TYPE_MAPPING = array(
  26. 'datetime' => self::DATETIME,
  27. 'timestamp' => self::DATETIME,
  28. 'date' => self::DATE,
  29. 'time' => self::TIME,
  30. 'int' => self::INTEGER,
  31. 'tinyint' => self::INTEGER,
  32. 'smallint' => self::INTEGER,
  33. 'mediumint' => self::INTEGER,
  34. 'bigint' => self::INTEGER,
  35. 'float' => self::DECIMAL,
  36. 'double' => self::DECIMAL,
  37. 'numeric' => self::DECIMAL,
  38. 'decimal' => self::DECIMAL,
  39. 'dec' => self::DECIMAL);
  40. /**
  41. * The true name of this column.
  42. * @var string
  43. */
  44. public $name;
  45. /**
  46. * The inflected name of this columns .. hyphens/spaces will be => _.
  47. * @var string
  48. */
  49. public $inflected_name;
  50. /**
  51. * The type of this column: STRING, INTEGER, ...
  52. * @var integer
  53. */
  54. public $type;
  55. /**
  56. * The raw database specific type.
  57. * @var string
  58. */
  59. public $raw_type;
  60. /**
  61. * The maximum length of this column.
  62. * @var int
  63. */
  64. public $length;
  65. /**
  66. * True if this column allows null.
  67. * @var boolean
  68. */
  69. public $nullable;
  70. /**
  71. * True if this column is a primary key.
  72. * @var boolean
  73. */
  74. public $pk;
  75. /**
  76. * The default value of the column.
  77. * @var mixed
  78. */
  79. public $default;
  80. /**
  81. * True if this column is set to auto_increment.
  82. * @var boolean
  83. */
  84. public $auto_increment;
  85. /**
  86. * Name of the sequence to use for this column if any.
  87. * @var boolean
  88. */
  89. public $sequence;
  90. /**
  91. * Casts a value to the column's type.
  92. *
  93. * @param mixed $value The value to cast
  94. * @param Connection $connection The Connection this column belongs to
  95. * @return mixed type-casted value
  96. */
  97. public function cast($value, $connection)
  98. {
  99. if ($value === null)
  100. return null;
  101. switch ($this->type)
  102. {
  103. case self::STRING: return (string)$value;
  104. case self::INTEGER: return (int)$value;
  105. case self::DECIMAL: return (double)$value;
  106. case self::DATETIME:
  107. case self::DATE:
  108. if (!$value)
  109. return null;
  110. if ($value instanceof DateTime)
  111. return $value;
  112. if ($value instanceof \DateTime)
  113. return new DateTime($value->format('Y-m-d H:i:s T'));
  114. return $connection->string_to_datetime($value);
  115. }
  116. return $value;
  117. }
  118. /**
  119. * Sets the $type member variable.
  120. * @return mixed
  121. */
  122. public function map_raw_type()
  123. {
  124. if ($this->raw_type == 'integer')
  125. $this->raw_type = 'int';
  126. if (array_key_exists($this->raw_type,self::$TYPE_MAPPING))
  127. $this->type = self::$TYPE_MAPPING[$this->raw_type];
  128. else
  129. $this->type = self::STRING;
  130. return $this->type;
  131. }
  132. }
  133. ?>