This library encodes native PHP data in to Valve Software's VDF KeyValue format. This is used by the Source engine and many SourceMod mods.
VDFKeyValue is available on Packagist and installable via Composer.
$ composer require mintopia/vdfkeyvalue
If you do not use composer, you can grab the code from GitHub and use any PSR-4 compatible autoloader.
<?php
use Mintopia\VDFKeyValue\Encoder;
// Create a new instance of the encoder and use it
$encoder = new Encoder;
$encoder->encode('foobar', $myObject);
You can encode objects, arrays, string, integers and anything else that can be represented by a string.
The KeyValue format doesn't strictly support anything more than nested keys and values, so any objects or numerical arrays ended up being treated as associative arrays before encoding.
<?php
use Mintopia\VDFKeyValue\Encoder;
$maps = new \stdClass;
$maps->payload = 'pl_goldrush';
$maps->cp = 'cp_badlands';
$data = new \stdClass;
$data->game = 'Team Fortress 2';
$data->appid = 440;
$data->bestmaps = $maps;
$data->characters = [
'Demoman',
'Scout',
'Heavy Weapons Guy'
];
$encoder = new Encoder;
echo $encoder->encode('gameinfo', $data);
$ php example.php
"gameinfo"
{
"game" "Team Fortress 2"
"appid" "440"
"bestmaps"
{
"payload" "pl_goldrush"
"cp" "cp_badlands"
}
"characters"
{
"0" "Demoman"
"1" "Scout"
"2" "Heavy Weapons Guy"
}
}
The format is a simple nested tree structure, similar to JSON but without arrays and requiring a root key.
Bugs and feature request are tracked on GitHub
Jessica Smith - [email protected] - http://mintopia.net
VDFKeyValue is licensed under the MIT License - see the LICENSE
file for details