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: * The fields in this schema.
24: *
25: * @var array
26: */
27: protected $_fields = [];
28:
29: /**
30: * The default values for fields.
31: *
32: * @var array
33: */
34: protected $_fieldDefaults = [
35: 'type' => null,
36: 'length' => null,
37: 'precision' => null,
38: 'default' => null,
39: ];
40:
41: /**
42: * Add multiple fields to the schema.
43: *
44: * @param array $fields The fields to add.
45: * @return $this
46: */
47: public function addFields(array $fields)
48: {
49: foreach ($fields as $name => $attrs) {
50: $this->addField($name, $attrs);
51: }
52:
53: return $this;
54: }
55:
56: /**
57: * Adds a field to the schema.
58: *
59: * @param string $name The field name.
60: * @param string|array $attrs The attributes for the field, or the type
61: * as a string.
62: * @return $this
63: */
64: public function addField($name, $attrs)
65: {
66: if (is_string($attrs)) {
67: $attrs = ['type' => $attrs];
68: }
69: $attrs = array_intersect_key($attrs, $this->_fieldDefaults);
70: $this->_fields[$name] = $attrs + $this->_fieldDefaults;
71:
72: return $this;
73: }
74:
75: /**
76: * Removes a field to the schema.
77: *
78: * @param string $name The field to remove.
79: * @return $this
80: */
81: public function removeField($name)
82: {
83: unset($this->_fields[$name]);
84:
85: return $this;
86: }
87:
88: /**
89: * Get the list of fields in the schema.
90: *
91: * @return string[] The list of field names.
92: */
93: public function fields()
94: {
95: return array_keys($this->_fields);
96: }
97:
98: /**
99: * Get the attributes for a given field.
100: *
101: * @param string $name The field name.
102: * @return array|null The attributes for a field, or null.
103: */
104: public function field($name)
105: {
106: if (!isset($this->_fields[$name])) {
107: return null;
108: }
109:
110: return $this->_fields[$name];
111: }
112:
113: /**
114: * Get the type of the named field.
115: *
116: * @param string $name The name of the field.
117: * @return string|null Either the field type or null if the
118: * field does not exist.
119: */
120: public function fieldType($name)
121: {
122: $field = $this->field($name);
123: if (!$field) {
124: return null;
125: }
126:
127: return $field['type'];
128: }
129:
130: /**
131: * Get the printable version of this object
132: *
133: * @return array
134: */
135: public function __debugInfo()
136: {
137: return [
138: '_fields' => $this->_fields
139: ];
140: }
141: }
142: