Browse Source

Add interface class for form contexts.

Having an interface will make type checks simpler, and make it clear
to userland extensions what methods are required.
mark_story 12 years ago
parent
commit
9a54547ae1

+ 1 - 1
src/View/Form/ArrayContext.php

@@ -56,7 +56,7 @@ use Cake\Utility\Hash;
  *    ]
  *  ];
  */
-class ArrayContext {
+class ArrayContext implements ContextInterface {
 
 /**
  * The request object.

+ 89 - 0
src/View/Form/ContextInterface.php

@@ -0,0 +1,89 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         CakePHP(tm) v 3.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\View\Form;
+
+/**
+ * Interface for FormHelper context implementations.
+ */
+interface ContextInterface {
+
+/**
+ * Get the fields used in the context as a primary key.
+ *
+ * @return array
+ */
+	public function primaryKey();
+
+/**
+ * Returns whether or not this form is for a create operation.
+ *
+ * @return boolean
+ */
+	public function isCreate();
+
+/**
+ * Get the current value for a given field.
+ *
+ * @param string $field A dot separated path to the field a value
+ *   is needed for.
+ * @return mixed
+ */
+	public function val($field);
+
+/**
+ * Check if a given field is 'required'.
+ *
+ * In this context class, this is simply defined by the 'required' array.
+ *
+ * @param string $field A dot separated path to check required-ness for.
+ * @return boolean
+ */
+	public function isRequired($field);
+
+/**
+ * Get the abstract field type for a given field name.
+ *
+ * @param string $field A dot separated path to get a schema type for.
+ * @return null|string An abstract data type or null.
+ * @see Cake\Database\Type
+ */
+	public function type($field);
+
+/**
+ * Get an associative array of other attributes for a field name.
+ *
+ * @param string $field A dot separated path to get additional data on.
+ * @return array An array of data describing the additional attributes on a field.
+ */
+	public function attributes($field);
+
+/**
+ * Check whether or not a field has an error attached to it
+ *
+ * @param string $field A dot separated path to check errors on.
+ * @return boolean Returns true if the errors for the field are not empty.
+ */
+	public function hasError($field);
+
+/**
+ * Get the errors for a given field
+ *
+ * @param string $field A dot separated path to check errors on.
+ * @return array An array of errors, an empty array will be returned when the
+ *    context has no errors.
+ */
+	public function error($field);
+
+}

+ 2 - 1
src/View/Form/EntityContext.php

@@ -20,6 +20,7 @@ use Cake\ORM\Entity;
 use Cake\ORM\TableRegistry;
 use Cake\Utility\Inflector;
 use Cake\Validation\Validator;
+use Cake\View\Form\ContextInterface;
 use Traversable;
 
 /**
@@ -42,7 +43,7 @@ use Traversable;
  *   Defaults to 'default'. Can be an array of table alias=>validators when
  *   dealing with associated forms.
  */
-class EntityContext {
+class EntityContext implements ContextInterface {
 
 /**
  * The request object.

+ 10 - 39
src/View/Form/NullContext.php

@@ -15,6 +15,7 @@
 namespace Cake\View\Form;
 
 use Cake\Network\Request;
+use Cake\View\Form\ContextInterface;
 
 /**
  * Provides a context provider that does nothing.
@@ -22,7 +23,7 @@ use Cake\Network\Request;
  * This context provider simply fulfils the interface requirements
  * that FormHelper has and allows access to the request data.
  */
-class NullContext {
+class NullContext implements ContextInterface {
 
 /**
  * The request object.
@@ -42,86 +43,56 @@ class NullContext {
 	}
 
 /**
- * Get the fields used in the context as a primary key.
- *
- * @return array
+ * {@inheritDoc}
  */
 	public function primaryKey() {
 		return [];
 	}
 
 /**
- * Returns whether or not this form is for a create operation.
- *
- * @return boolean
+ * {@inheritDoc}
  */
 	public function isCreate() {
 		return true;
 	}
 
 /**
- * Get the current value for a given field.
- *
- * This method will coalesce the current request data and the 'defaults'
- * array.
- *
- * @param string $field A dot separated path to the field a value
- *   is needed for.
- * @return mixed
+ * {@inheritDoc}
  */
 	public function val($field) {
 		return $this->_request->data($field);
 	}
 
 /**
- * Check if a given field is 'required'.
- *
- * In this context class, this is simply defined by the 'required' array.
- *
- * @param string $field A dot separated path to check required-ness for.
- * @return boolean
+ * {@inheritDoc}
  */
 	public function isRequired($field) {
 		return false;
 	}
 
 /**
- * Get the abstract field type for a given field name.
- *
- * @param string $field A dot separated path to get a schema type for.
- * @return null|string An abstract data type or null.
- * @see Cake\Database\Type
+ * {@inheritDoc}
  */
 	public function type($field) {
 		return null;
 	}
 
 /**
- * Get an associative array of other attributes for a field name.
- *
- * @param string $field A dot separated path to get additional data on.
- * @return array An array of data describing the additional attributes on a field.
+ * {@inheritDoc}
  */
 	public function attributes($field) {
 		return [];
 	}
 
 /**
- * Check whether or not a field has an error attached to it
- *
- * @param string $field A dot separated path to check errors on.
- * @return boolean Returns true if the errors for the field are not empty.
+ * {@inheritDoc}
  */
 	public function hasError($field) {
 		return false;
 	}
 
 /**
- * Get the errors for a given field
- *
- * @param string $field A dot separated path to check errors on.
- * @return array An array of errors, an empty array will be returned when the
- *    context has no errors.
+ * {@inheritDoc}
  */
 	public function error($field) {
 		return [];