diff --git a/src/Data.php b/src/Data.php index adf3736..3d728da 100644 --- a/src/Data.php +++ b/src/Data.php @@ -90,8 +90,6 @@ public function setKeys($var) { /** * add to the global arrays of unique keys. * - * @param mixed $var - * * @return $this */ public function addKey($var) { diff --git a/src/Data/Feature.php b/src/Data/Feature.php index 337ff12..f9aae13 100644 --- a/src/Data/Feature.php +++ b/src/Data/Feature.php @@ -178,8 +178,6 @@ public function setValues($var) { /** * add unique value. * - * @param mixed $var - * * @return $this */ public function addValue($var) { @@ -218,8 +216,6 @@ public function setProperties($var) { /** * Add property. * - * @param mixed $var - * * @return $this */ public function addProperty($var) { @@ -258,8 +254,6 @@ public function setCustomProperties($var) { /** * add arbitrary property. * - * @param mixed $var - * * @return $this */ public function addCustomProperty($var) { diff --git a/src/Data/FeatureCollection.php b/src/Data/FeatureCollection.php index 962b565..1846e52 100644 --- a/src/Data/FeatureCollection.php +++ b/src/Data/FeatureCollection.php @@ -92,8 +92,6 @@ public function setValues($var) { } /** - * @param mixed $var - * * @return $this */ public function addValue($var) { @@ -128,8 +126,6 @@ public function setCustomProperties($var) { /** * Add a custom property. * - * @param mixed $var - * * @return $this */ public function addCustomProperty($var) { diff --git a/src/Data/Geometry.php b/src/Data/Geometry.php index 381aa60..dd7b7e4 100644 --- a/src/Data/Geometry.php +++ b/src/Data/Geometry.php @@ -120,8 +120,6 @@ public function setLengths($var) { /** * add length to coordinate structure. * - * @param mixed $var - * * @return $this */ public function addLength($var) { @@ -204,8 +202,6 @@ public function setValues($var) { } /** - * @param mixed $var - * * @return $this */ public function addValue($var) { @@ -240,8 +236,6 @@ public function setCustomProperties($var) { /** * Add custom property. * - * @param mixed $var - * * @return $this */ public function addCustomProperty($var) { diff --git a/src/Data/Geometry/Type.php b/src/Data/Geometry/Type.php index 7f8897c..67a5acb 100644 --- a/src/Data/Geometry/Type.php +++ b/src/Data/Geometry/Type.php @@ -50,7 +50,7 @@ class Type { public static function name($value) { if (!isset(self::$valueToName[$value])) { - throw new \UnexpectedValueException(sprintf('Enum %s has no name defined for value %s', __CLASS__, $value)); + throw new \UnexpectedValueException(\sprintf('Enum %s has no name defined for value %s', __CLASS__, $value)); } return self::$valueToName[$value]; @@ -59,7 +59,7 @@ public static function name($value) { public static function value($name) { $const = __CLASS__ . '::' . mb_strtoupper($name); if (!\defined($const)) { - throw new \UnexpectedValueException(sprintf('Enum %s has no value defined for name %s', __CLASS__, $name)); + throw new \UnexpectedValueException(\sprintf('Enum %s has no value defined for name %s', __CLASS__, $name)); } return \constant($const); diff --git a/src/Decoder.php b/src/Decoder.php index d77b5e3..1f3ca9e 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -26,7 +26,7 @@ final class Decoder { * @throws GeobufException */ public static function decodeFileToJsonFile(string $geobufFile, string $jsonFile): bool|int { - return file_put_contents($jsonFile, static::decodeFileToJson($geobufFile)); + return file_put_contents($jsonFile, self::decodeFileToJson($geobufFile)); } /** @@ -35,7 +35,7 @@ public static function decodeFileToJsonFile(string $geobufFile, string $jsonFile * @throws GeobufException */ public static function decodeFileToJson(string $fileName): string { - return static::decodeToJson(file_get_contents($fileName)); + return self::decodeToJson(file_get_contents($fileName)); } /** @@ -44,7 +44,7 @@ public static function decodeFileToJson(string $fileName): string { * @throws GeobufException */ public static function decodeToJson(string $encodedInput): string { - return json_encode(static::decodeToArray($encodedInput)); + return json_encode(self::decodeToArray($encodedInput)); } /** @@ -53,7 +53,7 @@ public static function decodeToJson(string $encodedInput): string { * @throws GeobufException */ public static function decodeFileToArray(string $fileName): array { - return static::decodeToArray(file_get_contents($fileName)); + return self::decodeToArray(file_get_contents($fileName)); } /** @@ -62,36 +62,36 @@ public static function decodeFileToArray(string $fileName): array { * @throws GeobufException */ public static function decodeToArray(string $encodedInput): array { - static::$data = new Data(); - static::$data->mergeFromString($encodedInput); - static::$e = 10 ** static::$data->getPrecision(); - static::$dim = static::$data->getDimensions(); + self::$data = new Data(); + self::$data->mergeFromString($encodedInput); + self::$e = 10 ** self::$data->getPrecision(); + self::$dim = self::$data->getDimensions(); try { - static::$data->mergeFromString($encodedInput); + self::$data->mergeFromString($encodedInput); } catch (\Exception $e) { throw new GeobufException('Error while decoding Geobuf: ' . $e->getMessage(), 0, $e); } - switch (static::$data->getDataType()) { + switch (self::$data->getDataType()) { case 'feature_collection': - return static::decodeFeatureCollection(static::$data->getFeatureCollection()); + return self::decodeFeatureCollection(self::$data->getFeatureCollection()); case 'feature': - return static::decodeFeature(static::$data->getFeature()); + return self::decodeFeature(self::$data->getFeature()); case 'geometry': - return static::decodeGeometry(static::$data->getGeometry()); + return self::decodeGeometry(self::$data->getGeometry()); } - throw new GeobufException('Unknown data type ' . static::$data->getDataType()); + throw new GeobufException('Unknown data type ' . self::$data->getDataType()); } private static function decodeFeatureCollection(FeatureCollection $featureCollection): array { $obj = ['type' => 'FeatureCollection', 'features' => []]; - static::decodeProperties($featureCollection->getCustomProperties(), $featureCollection->getValues(), $obj); + self::decodeProperties($featureCollection->getCustomProperties(), $featureCollection->getValues(), $obj); foreach ($featureCollection->getFeatures() as $feature) { - $obj['features'][] = static::decodeFeature($feature); + $obj['features'][] = self::decodeFeature($feature); } return $obj; @@ -100,13 +100,13 @@ private static function decodeFeatureCollection(FeatureCollection $featureCollec private static function decodeFeature(Feature $feature): array { $obj = ['type' => 'Feature']; - static::decodeProperties($feature->getCustomProperties(), $feature->getValues(), $obj); - static::decodeId($feature, $obj); + self::decodeProperties($feature->getCustomProperties(), $feature->getValues(), $obj); + self::decodeId($feature, $obj); - $obj['geometry'] = static::decodeGeometry($feature->getGeometry()); + $obj['geometry'] = self::decodeGeometry($feature->getGeometry()); if (\count($feature->getProperties()) > 0) { - $obj['properties'] = static::decodeProperties($feature->getProperties(), $feature->getValues()); + $obj['properties'] = self::decodeProperties($feature->getProperties(), $feature->getValues()); } return $obj; @@ -115,14 +115,13 @@ private static function decodeFeature(Feature $feature): array { /** * @param RepeatedField|Value[] $values */ - private static function decodeProperties(array|RepeatedField $props, array|RepeatedField $values, ?array &$dest = null): array { - $dest ??= []; + private static function decodeProperties(array|RepeatedField $props, array|RepeatedField $values, array &$dest = []): array { $numProps = \count($props); if ($numProps === 0) { return $dest; } - $keys = static::$data->getKeys(); + $keys = self::$data->getKeys(); $r = $numProps > 2 ? range(0, $numProps - 1, 2) : [0]; foreach ($r as $i) { @@ -157,45 +156,42 @@ private static function decodeId(Feature $feature, array &$objJson): void { } } - /** - * @return array - */ private static function decodeGeometry(?Geometry $geometry): ?array { if ($geometry === null) { return null; } - $gt = static::GEOMETRY_TYPES[$geometry->getType()]; + $gt = self::GEOMETRY_TYPES[$geometry->getType()]; $obj = ['type' => $gt]; - static::decodeProperties($geometry->getCustomProperties(), $geometry->getValues(), $obj); + self::decodeProperties($geometry->getCustomProperties(), $geometry->getValues(), $obj); switch ($gt) { case 'GeometryCollection': $obj['geometries'] = array_map( - fn ($g) => static::decodeGeometry($g), + fn ($g) => self::decodeGeometry($g), $geometry->getGeometries() ); break; case 'Point': - $obj['coordinates'] = static::decodePoint($geometry->getCoords()); + $obj['coordinates'] = self::decodePoint($geometry->getCoords()); break; case 'LineString': case 'MultiPoint': - $obj['coordinates'] = static::decodeLine($geometry->getCoords()); + $obj['coordinates'] = self::decodeLine($geometry->getCoords()); break; case 'MultiLineString': case 'Polygon': - $obj['coordinates'] = static::decodeMultiLine($geometry, $gt === 'Polygon'); + $obj['coordinates'] = self::decodeMultiLine($geometry, $gt === 'Polygon'); break; case 'MultiPolygon': - $obj['coordinates'] = static::decodeMultiPolygon($geometry); + $obj['coordinates'] = self::decodeMultiPolygon($geometry); } return $obj; } private static function decodeCoord(float $coord): float { - return $coord / static::$e; + return $coord / self::$e; } /** @@ -204,7 +200,7 @@ private static function decodeCoord(float $coord): float { private static function decodePoint($coords): array { $return = []; foreach ($coords as $coord) { // can't use array_map as $coords might be a RepeatedField - $return[] = static::decodeCoord((float) $coord); + $return[] = self::decodeCoord((float) $coord); } return $return; @@ -216,22 +212,22 @@ private static function decodePoint($coords): array { private static function decodeLine($coords, ?bool $isClosed = false): array { $obj = []; $numCoords = \count($coords); - $r = range(0, static::$dim - 1); - $r2 = $numCoords > static::$dim ? range(0, $numCoords - 1, static::$dim) : [0]; - $p0 = array_fill(0, static::$dim, 0); + $r = range(0, self::$dim - 1); + $r2 = $numCoords > self::$dim ? range(0, $numCoords - 1, self::$dim) : [0]; + $p0 = array_fill(0, self::$dim, 0); foreach ($r2 as $i) { $p = array_map( fn ($j) => ($p0[$j] ?? 0) + ($coords[$i + $j] ?? 0), $r ); - $obj[] = static::decodePoint($p); + $obj[] = self::decodePoint($p); $p0 = $p; } if ($isClosed === true) { $p = array_map(fn ($j) => $coords[$j], $r); - $obj[] = static::decodePoint($p); + $obj[] = self::decodePoint($p); } return $obj; @@ -240,15 +236,15 @@ private static function decodeLine($coords, ?bool $isClosed = false): array { private static function decodeMultiLine(Geometry $geometry, ?bool $isClosed = false): array { $coords = $geometry->getCoords(); if (\count($geometry->getLengths()) === 0) { - return [static::decodeLine($coords, $isClosed)]; + return [self::decodeLine($coords, $isClosed)]; } $obj = []; $i = 0; foreach ($geometry->getLengths() as $length) { - $obj[] = static::decodeLine(\array_slice($coords, $i, $i + $length * static::$dim), $isClosed); - $i += $length * static::$dim; + $obj[] = self::decodeLine(\array_slice($coords, $i, $i + $length * self::$dim), $isClosed); + $i += $length * self::$dim; } return $obj; @@ -258,7 +254,7 @@ private static function decodeMultiPolygon(Geometry $geometry): array { $lengths = $geometry->getLengths(); $coords = $geometry->getCoords(); if (\count($lengths) === 0) { - return [[static::decodeLine($coords, true)]]; + return [[self::decodeLine($coords, true)]]; } $obj = []; @@ -272,9 +268,9 @@ private static function decodeMultiPolygon(Geometry $geometry): array { $rings = []; foreach (\array_slice($coords, $j, $j + $numRings) as $l) { - $rings[] = static::decodeLine(\array_slice($coords, $i, $i + $l * static::$dim), true); + $rings[] = self::decodeLine(\array_slice($coords, $i, $i + $l * self::$dim), true); ++$j; - $i += $l * static::$dim; + $i += $l * self::$dim; } $obj[] = $rings; diff --git a/src/Encoder.php b/src/Encoder.php index a47cd7d..b638c60 100644 --- a/src/Encoder.php +++ b/src/Encoder.php @@ -36,7 +36,7 @@ final class Encoder { * @throws GeobufException */ public static function encodeFileToBufFile(string $jsonFile, string $geobufFile, int $precision = 6, int $dim = 2): bool|int { - return file_put_contents($geobufFile, static::encodeFileToBuf($jsonFile, $precision, $dim)); + return file_put_contents($geobufFile, self::encodeFileToBuf($jsonFile, $precision, $dim)); } /** @@ -45,7 +45,7 @@ public static function encodeFileToBufFile(string $jsonFile, string $geobufFile, * @throws GeobufException */ public static function encodeFileToBuf(string $fileName, int $precision = 6, int $dim = 2): string { - return static::encode(file_get_contents($fileName), $precision, $dim); + return self::encode(file_get_contents($fileName), $precision, $dim); } /** @@ -57,7 +57,7 @@ public static function encodeFileToBuf(string $fileName, int $precision = 6, int * @throws GeobufException */ public static function encodeToFile(string $filePath, string $dataJson, int $precision = 6, int $dim = 2): bool|int { - return file_put_contents($filePath, static::encode($dataJson)); + return file_put_contents($filePath, self::encode($dataJson)); } /** @@ -72,32 +72,32 @@ public static function encode(string $dataJson, int $precision = 6, int $dim = 2 throw new GeobufException('Error while decoding GeoJSON: ' . $e->getMessage(), 0, $e); } - static::$data = new Data(); - static::$data->setDimensions($dim); - static::$data->setPrecision($precision); + self::$data = new Data(); + self::$data->setDimensions($dim); + self::$data->setPrecision($precision); - static::$json = $geoJson; - static::$dim = $dim; - static::$e = 10 ** $precision; // multiplier for converting coordinates into integers + self::$json = $geoJson; + self::$dim = $dim; + self::$e = 10 ** $precision; // multiplier for converting coordinates into integers - $dataType = static::$json['type']; + $dataType = self::$json['type']; if ($dataType === 'FeatureCollection') { - static::$data->setFeatureCollection(static::encodeFeatureCollection()); + self::$data->setFeatureCollection(self::encodeFeatureCollection()); } elseif ($dataType === 'Feature') { - static::$data->setFeature(static::encodeFeature(static::$json)); + self::$data->setFeature(self::encodeFeature(self::$json)); } else { - static::$data->setGeometry(static::encodeGeometry(static::$json)); + self::$data->setGeometry(self::encodeGeometry(self::$json)); } - return static::$data->serializeToString(); + return self::$data->serializeToString(); } private static function encodeFeatureCollection(): FeatureCollection { $featureCollection = new FeatureCollection(); - static::encodeCustomProperties($featureCollection, static::$json, ['type', 'features']); + self::encodeCustomProperties($featureCollection, self::$json, ['type', 'features']); $features = []; - foreach (static::$json['features'] as $featureJson) { - $features[] = static::encodeFeature($featureJson); + foreach (self::$json['features'] as $featureJson) { + $features[] = self::encodeFeature($featureJson); } $featureCollection->setFeatures($features); @@ -108,22 +108,22 @@ private static function encodeFeature(array $featureJson): Feature { $feature = new Feature(); // encode id - static::encodeId($feature, $featureJson['id'] ?? null); + self::encodeId($feature, $featureJson['id'] ?? null); // encode properties if (\array_key_exists('properties', $featureJson)) { - static::encodeProperties($feature, $featureJson['properties']); + self::encodeProperties($feature, $featureJson['properties']); } // encode custom properties - static::encodeCustomProperties($feature, $featureJson, ['type', 'id', 'properties', 'geometry']); + self::encodeCustomProperties($feature, $featureJson, ['type', 'id', 'properties', 'geometry']); // encode geometry if (\array_key_exists('geometry', $featureJson)) { if ($featureJson['geometry'] === null) { $feature->setGeometry(null); } else { - $feature->setGeometry(static::encodeGeometry($featureJson['geometry'])); + $feature->setGeometry(self::encodeGeometry($featureJson['geometry'])); } } @@ -135,9 +135,9 @@ private static function encodeGeometry(array $geometryJson): Geometry { $gt = $geometryJson['type']; $coords = $geometryJson['coordinates']; - $geometry->setType(static::GEOMETRY_TYPES[$gt]); + $geometry->setType(self::GEOMETRY_TYPES[$gt]); - static::encodeCustomProperties( + self::encodeCustomProperties( $geometry, $geometryJson, ['type', 'id', 'coordinates', 'arcs', 'geometries', 'properties'] @@ -147,25 +147,25 @@ private static function encodeGeometry(array $geometryJson): Geometry { case 'GeometryCollection': $geometries = []; foreach ($geometryJson['geometries'] as $geom) { - $geometries[] = static::encodeGeometry($geom); + $geometries[] = self::encodeGeometry($geom); } $geometry->setGeometries($geometries); break; case 'Point': - $geometry->setCoords(static::addPoint($coords)); + $geometry->setCoords(self::addPoint($coords)); break; case 'LineString': case 'MultiPoint': - $geometry->setCoords(static::addLine($coords)); + $geometry->setCoords(self::addLine($coords)); break; case 'MultiLineString': - static::addMultiLine($geometry, $coords); + self::addMultiLine($geometry, $coords); break; case 'Polygon': - static::addMultiLine($geometry, $coords, true); + self::addMultiLine($geometry, $coords, true); break; case 'MultiPolygon': - static::addMultiPolygon($geometry, $coords); + self::addMultiPolygon($geometry, $coords); break; } @@ -184,29 +184,28 @@ private static function encodeProperties(IHasProperties $obj, ?array $propsJson) } foreach ($propsJson as $key => $val) { - static::encodeProperty($key, $val, $obj); + self::encodeProperty($key, $val, $obj); } } private static function encodeCustomProperties(IHasCustomProperties $obj, array $objJson, array $exclude): void { foreach ($objJson as $key => $val) { if (!\in_array($key, $exclude, true)) { - static::encodeProperty($key, $val, $obj, true); + self::encodeProperty($key, $val, $obj, true); } } } /** * @param IHasCustomProperties|IHasProperties $obj - * @param mixed $val */ private static function encodeProperty(string $key, $val, $obj, bool $custom = false): void { - $keyIndex = array_search($key, static::$keys, true); + $keyIndex = array_search($key, self::$keys, true); if ($keyIndex === false) { - static::$keys[$key] = true; - static::$data->addKey($key); - $keyIndex = \count(static::$data->getKeys()) - 1; + self::$keys[$key] = true; + self::$data->addKey($key); + $keyIndex = \count(self::$data->getKeys()) - 1; } $value = new Value(); @@ -216,7 +215,7 @@ private static function encodeProperty(string $key, $val, $obj, bool $custom = f } elseif (\is_float($val)) { $value->setDoubleValue($val); } elseif (\is_int($val)) { - static::encodeInt($value, $val); + self::encodeInt($value, $val); } elseif (\is_bool($val)) { $value->setBoolValue($val); } else { @@ -255,13 +254,13 @@ private static function encodeId(Feature $feature, $id): void { } private static function addCoord(array &$coords, $coord): void { - $coords[] = (int) round($coord * static::$e); + $coords[] = (int) round($coord * self::$e); } private static function addPoint($point): array { $coords = []; foreach ($point as $x) { - static::addCoord($coords, $x); + self::addCoord($coords, $x); } return $coords; @@ -269,10 +268,10 @@ private static function addPoint($point): array { private static function addLine(array $points, bool $isClosed = false): array { $coords = []; - $sum = array_fill(0, static::$dim, 0); + $sum = array_fill(0, self::$dim, 0); for ($i = 0; $i < \count($points) - (int) $isClosed; ++$i) { - for ($j = 0; $j < static::$dim; ++$j) { - $n = (int) (round($points[$i][$j] * static::$e) - $sum[$j]); + for ($j = 0; $j < self::$dim; ++$j) { + $n = (int) (round($points[$i][$j] * self::$e) - $sum[$j]); $coords[] = $n; $sum[$j] += $n; } @@ -290,7 +289,7 @@ private static function addMultiLine(Geometry $geometry, array $lines, bool $isC $coords = []; foreach ($lines as $points) { - $coords = array_merge($coords, static::addLine($points, $isClosed)); + $coords = array_merge($coords, self::addLine($points, $isClosed)); } $geometry->setCoords($coords); } @@ -310,7 +309,7 @@ private static function addMultiPolygon(Geometry $geometry, array $polygons): vo $coords = []; foreach ($polygons as $rings) { foreach ($rings as $points) { - $coords = array_merge($coords, static::addLine($points, true)); + $coords = array_merge($coords, self::addLine($points, true)); } } $geometry->setCoords($coords); diff --git a/src/GeobufException.php b/src/GeobufException.php index 0c0b54d..ac2746d 100644 --- a/src/GeobufException.php +++ b/src/GeobufException.php @@ -3,7 +3,7 @@ namespace MBolli\PhpGeobuf; class GeobufException extends \Exception { - public function __construct($message = '', $code = 0, \Throwable $previous = null) { + public function __construct($message = '', $code = 0, ?\Throwable $previous = null) { $message = 'PhpGeobuf: ' . $message; parent::__construct($message, $code, $previous); }