Author: Vojtěch Kohout
Example #1
0
 /**
  * @param mixed $value
  * @param PropertyType $propertyType
  * @param bool $isNullable
  * @return mixed
  * @throws InvalidAnnotationException
  */
 private static function fixDefaultValue($value, PropertyType $propertyType, $isNullable)
 {
     if ($isNullable and strtolower($value) === 'null') {
         return null;
     } elseif (!$propertyType->isBasicType()) {
         throw new InvalidAnnotationException('Only properties of basic types may have default values specified.');
     }
     switch ($propertyType->getType()) {
         case 'boolean':
             $lower = strtolower($value);
             if ($lower !== 'true' and $lower !== 'false') {
                 throw new InvalidAnnotationException("Property of type boolean cannot have default value '{$value}'.");
             }
             return $lower === 'true';
         case 'integer':
             if (!is_numeric($value) && !preg_match('~0x[0-9a-f]+~', $value)) {
                 throw new InvalidAnnotationException("Property of type integer cannot have default value '{$value}'.");
             }
             return intval($value, 0);
         case 'float':
             if (!is_numeric($value)) {
                 throw new InvalidAnnotationException("Property of type float cannot have default value '{$value}'.");
             }
             return floatval($value);
         case 'array':
             if (strtolower($value) !== 'array()' and $value !== '[]') {
                 throw new InvalidAnnotationException("Property of type array cannot have default value '{$value}'.");
             }
             return [];
         case 'string':
             if ($value === '\'\'' or $value === '""') {
                 return '';
             }
             return $value;
         default:
             return $value;
     }
 }
Example #2
0
 /**
  * Tells whether property type is basic type (boolean|integer|float|string|array)
  *
  * @return bool
  */
 public function isBasicType()
 {
     return $this->type->isBasicType();
 }