/** * 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); }
/** * 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; } }
/** * 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); }