/**
  * Casts a field to a string which is safe to insert into HTML
  *
  * @param GridField $gridField GridField to cast value for
  * @param string    $fieldName Field name to cast value for
  * @param string    $value     Value to cast
  * 
  * @return string 
  * 
  * @author Sebastian Diel <*****@*****.**>
  * @since 26.03.2013
  */
 protected function castValue($gridField, $fieldName, $value)
 {
     // If a fieldCasting is specified, we assume the result is safe
     if (array_key_exists($fieldName, $this->fieldCasting)) {
         $value = $gridField->getCastedValue($value, $this->fieldCasting[$fieldName]);
     } else {
         if (is_object($value)) {
             // If the value is an object, we do one of two things
             if (method_exists($value, 'Nice')) {
                 // If it has a "Nice" method, call that & make sure the result is safe
                 $value = Convert::raw2xml($value->Nice());
             } else {
                 // Otherwise call forTemplate - the result of this should already be safe
                 $value = $value->forTemplate();
             }
         } elseif (!$this->isHtmlAllowedFor($fieldName, $gridField)) {
             // Otherwise, just treat as a text string & make sure the result is safe
             $value = Convert::raw2xml($value);
         }
     }
     return $value;
 }
Ejemplo n.º 2
0
 /**
  * @covers GridField::getCastedValue
  */
 public function testGetCastedValueObject()
 {
     $obj = new GridField('testfield', 'testfield');
     $value = $obj->getCastedValue('This is a sentance. This ia another.', 'Date');
     $this->assertEquals(null, $value);
 }
Ejemplo n.º 3
0
 /**
  *
  * @param GridField $gridField
  * @param string $fieldName
  * @param string $value
  * @return string 
  */
 protected function castValue($gridField, $fieldName, $value)
 {
     if (array_key_exists($fieldName, $this->fieldCasting)) {
         return $gridField->getCastedValue($value, $this->fieldCasting[$fieldName]);
     } elseif (is_object($value) && method_exists($value, 'Nice')) {
         return $value->Nice();
     }
     return $value;
 }