Schema.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Form;
  16. /**
  17. * Contains the schema information for Form instances.
  18. */
  19. class Schema
  20. {
  21. /**
  22. * The fields in this schema.
  23. *
  24. * @var array
  25. */
  26. protected $_fields = [];
  27. /**
  28. * The default values for fields.
  29. *
  30. * @var array
  31. */
  32. protected $_fieldDefaults = [
  33. 'type' => null,
  34. 'length' => null,
  35. 'precision' => null,
  36. ];
  37. /**
  38. * Add multiple fields to the schema.
  39. *
  40. * @param array $fields The fields to add.
  41. * @return $this
  42. */
  43. public function addFields(array $fields)
  44. {
  45. foreach ($fields as $name => $attrs) {
  46. $this->addField($name, $attrs);
  47. }
  48. return $this;
  49. }
  50. /**
  51. * Adds a field to the schema.
  52. *
  53. * @param string $name The field name.
  54. * @param string|array $attrs The attributes for the field, or the type
  55. * as a string.
  56. * @return $this
  57. */
  58. public function addField($name, $attrs)
  59. {
  60. if (is_string($attrs)) {
  61. $attrs = ['type' => $attrs];
  62. }
  63. $attrs = array_intersect_key($attrs, $this->_fieldDefaults);
  64. $this->_fields[$name] = $attrs + $this->_fieldDefaults;
  65. return $this;
  66. }
  67. /**
  68. * Removes a field to the schema.
  69. *
  70. * @param string $name The field to remove.
  71. * @return $this
  72. */
  73. public function removeField($name)
  74. {
  75. unset($this->_fields[$name]);
  76. return $this;
  77. }
  78. /**
  79. * Get the list of fields in the schema.
  80. *
  81. * @return array The list of field names.
  82. */
  83. public function fields()
  84. {
  85. return array_keys($this->_fields);
  86. }
  87. /**
  88. * Get the attributes for a given field.
  89. *
  90. * @param string $name The field name.
  91. * @return null|array The attributes for a field, or null.
  92. */
  93. public function field($name)
  94. {
  95. if (!isset($this->_fields[$name])) {
  96. return null;
  97. }
  98. return $this->_fields[$name];
  99. }
  100. /**
  101. * Get the type of the named field.
  102. *
  103. * @param string $name The name of the field.
  104. * @return string|null Either the field type or null if the
  105. * field does not exist.
  106. */
  107. public function fieldType($name)
  108. {
  109. $field = $this->field($name);
  110. if (!$field) {
  111. return null;
  112. }
  113. return $field['type'];
  114. }
  115. /**
  116. * Get the printable version of this object
  117. *
  118. * @return array
  119. */
  120. public function __debugInfo()
  121. {
  122. return [
  123. '_fields' => $this->_fields
  124. ];
  125. }
  126. }