silverstripe-framework/forms/FormTransformation.php
Sean Harvey 4da2743994 MINOR If a FormField hasn't called parent::__construct() properly, throw an error before attempting to call ClassInfo::ancestry() on a NULL value
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@78129 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-05-29 02:11:41 +00:00

57 lines
2.3 KiB
PHP
Executable File

<?php
/**
* This class represents "transformations" of a form - such as making it printable or making it readonly.
* The idea is that sometimes you will want to make your own such transformations, and you shouldn't have
* to edit the underlying code to support this.
*
* The first step in creating a transformation is subclassing FormTransformation. After that, you have two
* ways of defining specific functionality:
* - Define performMyTransformation() methods on each applicable FormField() object.
* - Define transformFieldType($field) methods on your subclass of FormTransformation.
*
* To actually perform the transformation, call $form->transform(new MyTransformation());
* @package forms
* @subpackage transformations
*/
class FormTransformation extends Object {
function transform(FormField $field) {
// Look for a performXXTransformation() method on the field itself.
// performReadonlyTransformation() is a pretty commonly applied method.
// Otherwise, look for a transformXXXField() method on this object.
// This is more commonly done in custom transformations
// We iterate through each array simultaneously, looking at [0] of both, then [1] of both.
// This provides a more natural failover scheme.
if(!$field->class) {
user_error(get_class($field) . ' is missing a class property, please ensure constructors call parent', E_USER_ERROR);
}
$transNames = array_reverse(array_values(ClassInfo::ancestry($this->class)));
$fieldClasses = array_reverse(array_values(ClassInfo::ancestry($field->class)));
$len = max(sizeof($transNames), sizeof($fieldClasses));
for($i=0;$i<$len;$i++) {
// This is lets fieldClasses be longer than transNames
if($transName = $transNames[$i]) {
if($field->hasMethod('perform' . $transName)) {
$funcName = 'perform' . $transName;
//echo "<li>$field->class used $funcName";
return $field->$funcName($this);
}
}
// And this one does the reverse.
if($fieldClass = $fieldClasses[$i]) {
if($this->hasMethod('transform' . $fieldClass)) {
$funcName = 'transform' . $fieldClass;
//echo "<li>$field->class used $funcName";
return $this->$funcName($field);
}
}
}
user_error("FormTransformation:: Can't perform '$this->class' on '$field->class'", E_USER_ERROR);
}
}
?>