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