コード例 #1
0
ファイル: TypeTool.php プロジェクト: sagittariusx/beluga.core
 /**
  * Converts the value! It must be a native PHP type (bool, int, float, double, string) to a other native PHP type.
  *
  * @param  mixed  $sourceValue The value to convert
  * @param  string $newType Native PHP type as target type. (See \Beluga\Type::PHP_* constants)
  * @return mixed
  * @throws \Beluga\ArgumentError
  */
 public static function ConvertNative($sourceValue, string $newType)
 {
     if (\is_null($sourceValue)) {
         return $sourceValue;
     }
     if (false === ($sourceType = static::GetNativeType($sourceValue))) {
         throw new ArgumentError('sourceValue', $sourceValue, 'Core TypeTool::ConvertNative', 'Can not convert a value of a type that is not a native PHP type! (bool, int, double, float, string)');
     }
     if ($sourceType == $newType) {
         return $sourceValue;
     }
     switch ($sourceType) {
         case Type::PHP_BOOLEAN:
         case 'boolean':
             switch ($newType) {
                 case Type::PHP_DOUBLE:
                     return (double) ($sourceValue ? 1 : 0);
                 case Type::PHP_FLOAT:
                     return (double) ($sourceValue ? 1 : 0);
                 case Type::PHP_INTEGER:
                     return $sourceValue ? 1 : 0;
                 case Type::PHP_STRING:
                     return $sourceValue ? '1' : '0';
             }
             break;
         case Type::PHP_DOUBLE:
             switch ($newType) {
                 case Type::PHP_FLOAT:
                     return \doubleval($sourceValue);
                 case Type::PHP_INTEGER:
                     return \intval($sourceValue);
                 case Type::PHP_STRING:
                     return '' . $sourceValue;
                 case Type::PHP_BOOLEAN:
                 case 'boolean':
                     return $sourceValue > 0;
             }
             break;
         case Type::PHP_FLOAT:
             switch ($newType) {
                 case Type::PHP_DOUBLE:
                     return \floatval($sourceValue);
                 case Type::PHP_INTEGER:
                     return \intval($sourceValue);
                 case Type::PHP_STRING:
                     return '' . $sourceValue;
                 case Type::PHP_BOOLEAN:
                 case 'boolean':
                     return $sourceValue > 0;
             }
             break;
         case Type::PHP_INTEGER:
             switch ($newType) {
                 case Type::PHP_DOUBLE:
                     return \intval($sourceValue);
                 case Type::PHP_FLOAT:
                     return \intval($sourceValue);
                 case Type::PHP_STRING:
                     return '' . $sourceValue;
                 case Type::PHP_BOOLEAN:
                 case 'boolean':
                     return $sourceValue > 0;
             }
             break;
         case Type::PHP_STRING:
             switch ($newType) {
                 case Type::PHP_INTEGER:
                 case Type::PHP_BOOLEAN:
                 case 'boolean':
                 case Type::PHP_FLOAT:
                 case Type::PHP_DOUBLE:
                     return TypeTool::StrToType($sourceValue, $newType);
             }
             break;
     }
     throw new ArgumentError('newType', $newType, 'Can not convert a value of a type that isnt a native PHP type! (bool, int, double, float, string)');
 }
コード例 #2
0
 /**
  * Extracts some associative array data from a HTML attribute format string. (e.g.: a="20" b=foo)
  *
  * Entities are auto converted to unicode UTF-8 characters!
  *
  * yes|no|on|off|true|false will be converted automatically to a boolean value.
  *
  * @param  string  $attributeStr The HTML attribute string to parse.
  * @param  boolean $lowerKeys    Convert all keys (attribute names) to lower case? (defaults to FALSE)
  * @param  boolean $autoBoolean  Auto convert the values yes|no|on|off|true|false to boolean? (defaults to FALSE)
  * @return array
  */
 public static function ParseHtmlAttributes(string $attributeStr, bool $lowerKeys = false, bool $autoBoolean = false) : array
 {
     // This init the resulting attribute array
     $attributes = array();
     $dc = new \DOMDocument();
     try {
         if (!$dc->loadHTML('<html><body><p ' . $attributeStr . '></p></body></html>')) {
             return $attributes;
         }
     } catch (\Throwable $ex) {
         return $attributes;
     }
     $element = $dc->getElementsByTagName('p')->item(0);
     if (!$element->hasAttributes()) {
         return $attributes;
     }
     foreach ($element->attributes as $attr) {
         $key = $lowerKeys ? \strtolower($attr->nodeName) : (string) $attr->nodeName;
         $attributes[$key] = (string) $attr->nodeValue;
         // Convert the value to boolean if required and if it makes sense
         if ($autoBoolean) {
             if (\preg_match('~^(yes|no|on|off|true|false)$~i', $attributes[$key])) {
                 $attributes[$key] = TypeTool::StrToType($attributes[$key], Type::PHP_BOOLEAN);
             } else {
                 if (\preg_match('~^(multiple|selected|disabled|readonly|checked)$~i', $key)) {
                     $attributes[$key] = \strtolower($key) === \strtolower($attributes[$key]);
                 }
             }
         }
     }
     return $attributes;
 }