Guest User

Untitled

a guest
Jun 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. <?php
  2. class RecordField{
  3. private $_name = NULL;
  4. private $_type = NULL; //string|integer|float
  5. private $_value = NULL; //real DB NULL is 'NULL'
  6. private $_default = NULL;
  7. /**
  8. * Contains an associative array with the reference of the RecordField
  9. * object of this table as key and as its element an array of references
  10. * to the RecordField objets to which this Record item is constraint.
  11. *
  12. * @var array
  13. */
  14. private $_relationships = array();
  15. /**
  16. * Reference to the Record object instantiated
  17. * @var <type>
  18. */
  19. private $_record = NULL;
  20. //Constructor
  21.  
  22. public function __construct(){
  23. $_args = func_get_args();
  24. if(is_array($_args[0])){
  25. $_args = array_values($_args[0]);
  26. }
  27. $_numargs = count($_args);
  28.  
  29. if($_numargs === 0){
  30. $this->_name = NULL;
  31. $this->_type = NULL;
  32. $this->_value = NULL;
  33. $this->_default = NULL;
  34. }else{
  35. if($_numargs >= 1){$this->setName ($_args[0]);}
  36. if($_numargs >= 2){$this->setType ($_args[1]);}
  37. if($_numargs >= 3){$this->setValue ($_args[2]);}
  38. if($_numargs >= 4){$this->setDefault($_args[3]);}
  39. if($_numargs >= 5){$this->setRecord ($_args[4]);}
  40. }
  41. }
  42. //Accessors & Mutators
  43. public function getName(){
  44. return $this->_name;
  45. }
  46. public function getType(){
  47. return $this->_type;
  48. }
  49. public function getValue(){
  50. if(preg_match('/(text|TEXT|blob|BLOB)$/',$this->_type) == true){
  51. return stripslashes($this->_value);
  52. }return $this->_value;
  53. }
  54. public function getDefault(){
  55. return $this->_default;
  56. }
  57. protected function &getRelationships($fieldname = NULL){
  58. return ($fieldname)?
  59. ((array_key_exists($fieldname,$this->_relationships))?($this->_relationships[$fieldname]):(NULL))
  60. :
  61. ($this->_relationships);
  62. }
  63. public function &getRecord(){
  64. return $this->_record;
  65. }
  66.  
  67. public function setName($n){
  68. $this->_name = (string)($n);
  69. return $this;
  70. }
  71. public function setType($t){
  72. $this->_type = (string)($t);
  73. return $this;
  74. }
  75. public function setValue($v){
  76. $this->_value = $this->cast($v);
  77. return $this;
  78. }
  79. public function setDefault($d){
  80. $this->_default = $this->cast($d);
  81. return $this;
  82. }
  83. public function setRelationship(RecordField $_record_field){
  84. if(!isset($this->_relationships[$_record_field->getName()])){
  85. $this->_relationships[$_record_field->getName()] = &$_record_field;
  86. }
  87. }
  88. public function setRecord(Record $_record){
  89. $this->_record = &$_record;
  90. return $this;
  91. }
  92. //Methods & Functions
  93. private function cast($v){
  94. return sql_data_cast($v, $this->_type);
  95. }
  96. public function buildXML(){
  97. $xml_node = new SimpleXMLElement('<RecordField/>');
  98. $xml_node->addAttribute('name', (string)($this->_name));
  99. $xml_node->addAttribute('type', (string)($this->_type));
  100. $xml_node->addAttribute('value', (string)($this->_value));
  101. $xml_node->addAttribute('default', (string)($this->_default));
  102. return $xml_node;
  103. }
  104. public function parseXML(SimpleXMLElement $xml_node){
  105. if($xml_node->getName() == 'RecordField'){
  106. $this->setName($xml_node['name']);
  107. $this->setType($xml_node['type']);
  108. $this->setValue($xml_node['value']);
  109. $this->setDefault($xml_node['default']);
  110. return true;
  111. }return false;
  112. }
  113. }
Add Comment
Please, Sign In to add comment