Repository navigation
v1.0.0
This release replaces the JSON round-trip with a direct parser. parse() keeps its return values. A new parseTyped() returns a tag tree that keeps the NBT types and can serialize back to SNBT.
What's Changed
- Direct SNBT parser with a typed tag tree by @m-stilling in #1
Added
SNBTParser::parseTyped(string $input): Tagparses SNBT into a typed tag tree. Every value is aTagsubclass underStilling\SNBTParser\Tag:ByteTag,ShortTag,IntTag,LongTag,FloatTag,DoubleTag,BooleanTag,StringTag,ByteArrayTag,IntArrayTag,LongArrayTag,ListTagandCompoundTag. The tree tells0b,0s,0and0Lapart.Tag::toPhp()returns the native PHP value. This is the same value thatparse()returns.Tag::toSnbt(SNBTFormat $format = SNBTFormat::Compact)serializes the tag back to SNBT and keeps the NBT types. A compactdata getblob round-trips byte for byte.SNBTFormatselects the layout oftoSnbt().Compactgives{a:1,b:[1,2]}.Spacedgives{ a: 1, b: [ 1, 2 ] }.Prettyputs compounds and lists on multiple lines with four-space indentation, and keeps typed number arrays on one line.CompoundTag::get(string $key): ?TagandCompoundTag::has(string $key): boolread compound entries.ListTag::get(int $index): ?Tagreads list items.CompoundTag,ListTagand the three number array tags implementCountableandIteratorAggregate.count($tag)andforeach ($tag as $key => $value)work on them. The readonlyentries,itemsandvaluesproperties expose the contents as arrays.examples/parse-typed.phpdumps a typed tree, itstoPhp()value and itstoSnbt()output.
Changed
parse()now delegates toparseTyped()->toPhp(). The return values do not change: every integer type becomesint, every floating-point type becomesfloat,trueandfalsebecomebool.- Unquoted string values parse.
{ id: stone }returns[ "id" => "stone" ]. Earlier versions threw on this input. - The escape sequences
\n,\rand\tdecode to the control characters.toSnbt()writes them back as escape sequences. [B; true, false]parses to the bytes1and0. Earlier versions returned0for both.- Invalid typed-array elements throw
SNBTParseException. This covers[I; foo],[I; 1.5],[I; 1f]and boolean keywords outside a byte array. Earlier versions returned0for these elements. Any integer suffix is still accepted in any array type, so[I; 1b, 2s]parses. - Parse errors report the byte position and the text near it. Example:
Expected ',' or '}' at position 6 near "x: 1}".
Removed
- The
ext-mbstringrequirement. The package needs no PHP extensions. - The internal
Stilling\SNBTParser\Tokensnamespace. It was not part of the public API.
Compatibility
- PHP 8.3, 8.4 and 8.5 are supported. The CI matrix tests all three with the lowest and the highest dependency versions.
SNBTParser::parse()andSNBTParser::intsToUuid()keep their signatures. The declared return type ofparse()no longer listsobject, which it never returned.
Upgrading from 0.3
No code changes are needed for parse() and intsToUuid(). Check these points:
- If your input relied on invalid typed-array elements parsing as
0, it now throws. Fix the input, or catchSNBTParseException. - If your input contained
\n,\ror\tescape sequences, the values now contain the control characters instead of the two-character sequences. - If you catch parse errors by message text, update the patterns. The messages changed.
Full Changelog: v0.3.0...v1.0.0