ArrayContext.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 CakePHP(tm) v 3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View\Form;
  16. use Cake\Network\Request;
  17. use Cake\Utility\Hash;
  18. /**
  19. * Provides a basic array based context provider for FormHelper
  20. * this adapter is useful in testing or when you have forms backed by
  21. * by simple array data structures.
  22. *
  23. * Important keys:
  24. *
  25. * - `defaults` The default values for fields. These values
  26. * will be used when there is no request data set. Data should be nested following
  27. * the dot separated paths you access your fields with.
  28. * - `required` A nested array of fields, relationships and boolean
  29. * flags to indicate a field is required.
  30. * - `schema` An array of data that emulate the column structures that
  31. * Cake\Database\Schema\Table uses. This array allows you to control
  32. * the inferred type for fields and allows auto generation of attributes
  33. * like maxlength, step and other HTML attributes.
  34. * - `errors` An array of validation errors. Errors should be nested following
  35. * the dot separated paths you access your fields with.
  36. */
  37. class ArrayContext {
  38. /**
  39. * The request object.
  40. *
  41. * @var Cake\Network\Request
  42. */
  43. protected $_request;
  44. /**
  45. * Context data for this object.
  46. *
  47. * @var array
  48. */
  49. protected $_context;
  50. /**
  51. * Constructor.
  52. *
  53. * @param Cake\Network\Request
  54. * @param array
  55. */
  56. public function __construct(Request $request, array $context) {
  57. $this->_request = $request;
  58. $context += [
  59. 'schema' => [],
  60. 'required' => [],
  61. 'defaults' => [],
  62. 'errors' => [],
  63. ];
  64. $this->_context = $context;
  65. }
  66. /**
  67. * Get the current value for a given field.
  68. *
  69. * This method will coalesce the current request data and the 'defaults'
  70. * array.
  71. *
  72. * @param string $field A dot separated path to the field a value
  73. * is needed for.
  74. * @return mixed
  75. */
  76. public function val($field) {
  77. $val = $this->_request->data($field);
  78. if ($val !== null) {
  79. return $val;
  80. }
  81. if (empty($this->_context['defaults']) || !is_array($this->_context['defaults'])) {
  82. return null;
  83. }
  84. return Hash::get($this->_context['defaults'], $field);
  85. }
  86. /**
  87. * Check if a given field is 'required'.
  88. *
  89. * In this context class, this is simply defined by the 'required' array.
  90. *
  91. * @param string $field A dot separated path to check required-ness for.
  92. * @return boolean
  93. */
  94. public function isRequired($field) {
  95. if (!is_array($this->_context['required'])) {
  96. return false;
  97. }
  98. $required = Hash::get($this->_context['required'], $field);
  99. return (bool)$required;
  100. }
  101. /**
  102. * Get the abstract field type for a given field name.
  103. *
  104. * @param string $field A dot separated path to get a schema type for.
  105. * @return string An abstract data type.
  106. * @see Cake\Database\Type
  107. */
  108. public function type($field) {
  109. if (!is_array($this->_context['schema'])) {
  110. return false;
  111. }
  112. $schema = Hash::get($this->_context['schema'], $field);
  113. return isset($schema['type']) ? $schema['type'] : null;
  114. }
  115. /**
  116. * Get an associative array of other attributes for a field name.
  117. *
  118. * @param string $field A dot separated path to get a additional data on.
  119. * @return array An array of data describing the additional attributes on a field.
  120. */
  121. public function attributes($field) {
  122. if (!is_array($this->_context['schema'])) {
  123. return [];
  124. }
  125. $schema = Hash::get($this->_context['schema'], $field);
  126. $whitelist = ['length' => null, 'precision' => null];
  127. return array_intersect_key($schema, $whitelist);
  128. }
  129. /**
  130. * Check whether or not a field has an error attached to it
  131. *
  132. * @param string $field A dot separated path to check errors on.
  133. * @return boolean Returns true if the errors for the field are not empty.
  134. */
  135. public function hasError($field) {
  136. if (empty($this->_context['errors'])) {
  137. return false;
  138. }
  139. return (bool)Hash::check($this->_context['errors'], $field);
  140. }
  141. /**
  142. * Get the errors for a given field
  143. *
  144. * @param string $field A dot separated path to check errors on.
  145. * @return mixed Either a string or an array of errors. Null
  146. * will be returned when the field path is undefined.
  147. */
  148. public function error($field) {
  149. if (empty($this->_context['errors'])) {
  150. return null;
  151. }
  152. return Hash::get($this->_context['errors'], $field);
  153. }
  154. }