本文整理汇总了PHP中Type::hasAssociatedString方法的典型用法代码示例。如果您正苦于以下问题:PHP Type::hasAssociatedString方法的具体用法?PHP Type::hasAssociatedString怎么用?PHP Type::hasAssociatedString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type::hasAssociatedString方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: TryParse
/**
* Extracts a {@see \Beluga\GIS\Longitude} instance from defined value and returns it by reference with the
* $output parameter. The Method returns TRUE on success, FALSE otherwise.
*
* @param string|double|float|\SimpleXMLElement|\Beluga\GIS\Longitude|\Beluga\GIS\Coordinate $value
* @param \Beluga\GIS\Longitude|null &$output Returns the resulting Longitude reference, if the method returns TRUE
* @return boolean
*/
public static function TryParse($value, &$output) : bool
{
if (\is_null($value)) {
return false;
}
if ($value instanceof Longitude) {
$output = $value;
return true;
}
if ($value instanceof Coordinate) {
$output = $value->Longitude;
return true;
}
if (\is_double($value) || \is_float($value)) {
$data = AbstractElement::_DecToDDMS($value, true);
try {
$output = new Longitude($data['DIR'], $data['DEG'], $data['MIN'], $data['SEC']);
} catch (\Throwable $ex) {
return false;
}
return true;
}
$type = new Type($value);
if (!$type->hasAssociatedString()) {
return false;
}
return self::TryParseString($type->getStringValue(), $output);
}
示例2: StrToType
/**
* Converts a string to defined native PHP type.
*
* @param string $string The string to convert
* @param string $typename The name of the required resulting PHP type.
* Allowed types are (bool|boolean|double|float|int|integer|string|array)
* @return mixed
*/
public static function StrToType(string $string, $typename)
{
if (null === $string) {
return null;
}
$t = new Type($string);
if (!$t->hasAssociatedString()) {
return null;
}
$string = $t->getStringValue();
switch (\strtolower($typename)) {
case 'bool':
case 'boolean':
$res = false;
static::IsBoolConvertible($string, $res);
return $res;
case 'float':
return \floatval(\str_replace(',', '.', $string));
case 'double':
if (!static::IsDecimal($string, true)) {
return null;
}
$res = \str_replace(',', '.', $string);
$tmp = \explode('.', $res);
$ts = \count($tmp);
if ($ts > 2) {
$dv = $tmp[$ts - 1];
unset($tmp[$ts - 1]);
$dv = \join('', $tmp) . '.' . $dv;
return \doubleval($dv);
}
return \doubleval($res);
case 'int':
case 'integer':
if (static::IsInteger($string) || static::IsDecimal($string, true)) {
return \intval($string);
}
return null;
case 'string':
return $string;
case 'array':
if (\strlen($string) < 1) {
return [];
}
if (\strlen($string) > 3) {
if (\substr($string, 0, 2) == 'a:') {
try {
$res = \unserialize($string);
if (\is_array($res)) {
return $res;
}
} catch (\Exception $ex) {
}
}
if (strStartsWith($string, '[') && strEndsWith($string, ']')) {
try {
return (array) \json_decode($string);
} catch (\Exception $ex) {
}
} else {
if (strStartsWith($string, '{') && strEndsWith($string, '}')) {
try {
return (array) \json_decode($string);
} catch (\Exception $ex) {
}
}
}
}
return array($string);
default:
if (\strlen($string) < 1) {
return null;
}
if (\strlen($string) > 3) {
if (\substr($string, 0, 2) == 'O:' && \preg_match('~^O:[^"]+"' . $typename . '":~', $string)) {
try {
$res = \unserialize($string);
if (!\is_object($res)) {
return null;
}
if (\get_class($res) == $typename) {
return $res;
}
} catch (\Throwable $ex) {
}
}
}
return null;
}
}
示例3: TryParse
/**
* Extracts a {@see \Beluga\GIS\Coordinate} instance from defined value and returns it by reference with the
* $output parameter. The Method returns TRUE on success, FALSE otherwise.
*
* @param string|double|float|\Beluga\GIS\Coordinate $value The value to parse.
* @param \Beluga\GIS\Coordinate|null &$output Returns the resulting Coordinate reference, if the method returns TRUE
* @return boolean
*/
public static function TryParse($value, &$output) : bool
{
if (empty($value)) {
return false;
}
if ($value instanceof Coordinate) {
$output = $value;
return true;
}
$type = new Type($value);
if (!$type->hasAssociatedString()) {
return false;
}
return self::TryParseString($type->getStringValue(), $output);
}