Carbon_Fields\Field\Field::factory PHP Method

factory() public static method

Create a new field of type $type and name $name and label $label.
public static factory ( string $type, string $name, string $label = null ) : object
$type string
$name string lower case and underscore-delimited
$label string (optional) Automatically generated from $name if not present
return object $field
    public static function factory($type, $name, $label = null)
    {
        // backward compatibility: `file` type used to be called `attachment`
        if ($type === 'attachment') {
            $type = 'file';
        }
        $type = str_replace(' ', '_', ucwords(str_replace('_', ' ', $type)));
        $class = __NAMESPACE__ . '\\' . $type . '_Field';
        if (!class_exists($class)) {
            Incorrect_Syntax_Exception::raise('Unknown field "' . $type . '".');
            $class = __NAMESPACE__ . '\\Broken_Field';
        }
        $field = new $class($name, $label);
        $field->type = $type;
        return $field;
    }

Usage Example

示例#1
0
 /**
  * A proxy for the abstract field factory method.
  *
  * @see Carbon_Fields\Field\Field::factory()
  **/
 public static function factory($type, $name, $label = null)
 {
     return Abstract_Field::factory($type, $name, $label);
 }