PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tanora.org/www/framework/system/column.php

https://bitbucket.org/ekkl/tanora
PHP | 107 lines | 56 code | 26 blank | 25 comment | 6 complexity | 3596105a0df4791c797e526e8daffbcd MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * File: framework/system/column.php
  4. *
  5. * Defines a base class for ORM Fields when mapping to a database.
  6. */
  7. abstract class Column {
  8. protected $field;
  9. protected $db;
  10. protected $type;
  11. protected $constraints = array();
  12. /**
  13. * Sets the field member variable.
  14. */
  15. public function __construct(&$db, &$field) {
  16. $this->db = $db;
  17. $this->field = $field;
  18. }
  19. /**
  20. * Applies itself to a CreateQuery object.
  21. */
  22. public function create(&$query) {
  23. $column = $query->column();
  24. $column->name($this->field->get_name());
  25. $column->type($this->type);
  26. if(!empty($this->constraints)) {
  27. $handle = $column->constrain();
  28. foreach($this->constraints as $name=>$parameters) {
  29. // make sure method exists
  30. if(method_exists($handle, $name)) {
  31. call_user_func_array(array($handle, $name), $parameters);
  32. }
  33. }
  34. }
  35. }
  36. /**
  37. * Appends to the constraints member variable.
  38. */
  39. public function constrain($name) {
  40. $args = func_get_args();
  41. $this->constraints[array_shift($args)] = $args;
  42. }
  43. /**
  44. * Abstracts constrain() for convenience.
  45. */
  46. public function set_primary() {
  47. $this->constrain('primary');
  48. }
  49. /**
  50. * Gets the field value by default. Overwrite this to format values
  51. * specially for the database.
  52. */
  53. public function get_value() {
  54. return $this->field->get_value();
  55. }
  56. /**
  57. * Returns the Type object for this column from the respective database.
  58. */
  59. public function get_type() {
  60. $db_name = get_class($this->db);
  61. $db_name = substr($db_name, 0, -8);
  62. $dir_name = strtolower($db_name);
  63. $file_name = strtolower($this->type);
  64. $file = FRAMEWORK_PATH.'system/database/'.$dir_name.'/types.php';
  65. if(file_exists($file)) {
  66. require_once(FRAMEWORK_PATH.'system/types.php');
  67. require_once($file);
  68. $class = $db_name.'Types';
  69. $classes = new $class();
  70. $function = $this->type;
  71. if(function_exists(array($classes, $function))) {
  72. return $classes->$function();
  73. } else {
  74. Framework::error('Type does not exist: '.$this->type);
  75. }
  76. } else {
  77. Framework::error('File does not exist: '.$file);
  78. }
  79. }
  80. }
  81. ?>