Beispiel #1
0
 /**
  * Construct enum data type.
  *
  * @param string|string[] $valuesOrClass
  *            Either an enum class or a list of
  *            enum values.
  * @param string $null
  *            Is nullable.
  * @param string $default
  *            Default value.
  * @throws InvalidEnumException If default values is not part of enum.
  */
 protected function __construct($valuesOrClass, $null = false, $default = null)
 {
     parent::__construct(DataType::ENUM, $null, $default);
     if (is_array($valuesOrClass)) {
         $this->values = $valuesOrClass;
     } else {
         $this->class = $valuesOrClass;
         $this->values = Enum::getValues($this->class);
     }
     if (isset($default) and !in_array($default, $this->values)) {
         throw new InvalidEnumException('Default value must be part of enum');
     }
 }
Beispiel #2
0
 /**
  * Create data type from a placeholder.
  * @param string $placeholder Placeholder string.
  * @return DataType Type object.
  */
 public static function fromPlaceholder($placeholder)
 {
     switch (strtolower($placeholder)) {
         case 'i':
         case 'int':
         case 'integer':
             return self::integer(self::BIG);
         case 'f':
         case 'float':
             return self::float();
         case 's':
         case 'str':
         case 'string':
             return self::string(255);
         case 't':
         case 'text':
             return self::text();
         case 'b':
         case 'bool':
         case 'boolean':
             return self::boolean();
         case 'date':
             return self::date();
         case 'd':
         case 'datetime':
             return self::dateTime();
         case 'n':
         case 'bin':
         case 'binary':
             return self::binary();
         case 'a':
         case 'o':
         case 'array':
         case 'object':
             return self::binary();
     }
     if (Enum::classExists($placeholder)) {
         return self::enum($placeholder);
     }
     throw new InvalidDataTypeException(tr('Invalid data type placeholder: %1', '%' . $placeholder));
 }