Skip to main content

Encoding and Data

@Var — type utilities and serialization (30 methods)

JSON encoding/decoding

ZYMBA
$data = [name: "Alice", age: 30, tags: ["admin", "user"]];

$json = @Var.toJSON($data);
// '{"name":"Alice","age":30,"tags":["admin","user"]}'

$pretty = @Var.toPrettyJSON($data);
// Formatted with indentation

$parsed = @Var.fromJSON($json);
echo $parsed.name; // "Alice"

// Validate before parsing
if (@Var.isValidJSON($input)) {
$data = @Var.fromJSON($input);
}

PHP serialization

ZYMBA
$serialized = @Var.toSerializedPHP([a: 1, b: 2]);
$restored = @Var.fromSerializedPHP($serialized);
echo $restored.a; // 1

Binary pack/unpack

ZYMBA
$packed = @Var.pack("N", 12345); // Pack as network byte order
$unpacked = @Var.unpack("N", $packed); // Unpack

Type inspection

ZYMBA
echo @Var.typeOf(42); // "int"
echo @Var.typeOf("hello"); // "string"
echo @Var.typeOf([1, 2]); // "object"

echo @Var.isInt(42); // true
echo @Var.isFloat(3.14); // true
echo @Var.isNumber(42); // true (int or float)
echo @Var.isNumeric("42"); // true (string that looks like a number)
echo @Var.isString("hi"); // true
echo @Var.isBool(true); // true
echo @Var.isObject([]); // true
echo @Var.isNull(null); // true
echo @Var.isFunction(function(){}); // true
echo @Var.isScalar("hi"); // true (string, int, float, or bool)
echo @Var.isComplex([1,2]); // true (object/array)
echo @Var.isEmpty(""); // true
echo @Var.exists(null); // false
echo @Var.count([1,2,3]); // 3
echo @Var.compare(1, 2); // -1

Cloning

ZYMBA
$shallow = @Var.clone([a: [1, 2]]); // Shallow copy
$deep = @Var.cloneDeep([a: [1, 2]]); // Deep copy

$deep.a[] = 3; // Only affects the deep copy

Source representation

ZYMBA
echo @Var.toSource([1, "two", true, null]);
// Zymba-syntax representation of the value

Global variable introspection

ZYMBA
$count = @Var.countGlobalVariables();
$names = @Var.listGlobalVariableNames();
$vars = @Var.listGlobalVariables();

@URI — URL operations (20 methods)

Encoding/decoding

ZYMBA
echo @URI.encode("hello world"); // "hello%20world"
echo @URI.decode("hello%20world"); // "hello world"

Query strings

ZYMBA
// Build query string from array
$qs = @URI.createQuery([name: "Alice", age: "30", city: "NYC"]);
// "name=Alice&age=30&city=NYC"

// Parse query string to array
$params = @URI.parseQuery("name=Alice&age=30");
// [name: "Alice", age: "30"]

URL parsing

ZYMBA
$url = "https://user:[email protected]:8080/path?q=1#frag";

echo @URI.getScheme($url); // "https"
echo @URI.getHost($url); // "example.com"
echo @URI.getPort($url); // "8080"
echo @URI.getPath($url); // "/path"
echo @URI.getQuery($url); // "q=1"
echo @URI.getFragment($url); // "frag"
echo @URI.getUser($url); // "user"
echo @URI.getPassword($url); // "pass"

Validation

ZYMBA
echo @URI.isValid("https://example.com"); // true
echo @URI.isAbsolute("https://example.com/path"); // true
echo @URI.normalize("https://example.com/../a/./b"); // normalized

Data URIs

ZYMBA
$dataUri = @URI.createDataURI("Hello", "text/plain", "utf-8");
// "data:text/plain;charset=utf-8,Hello"

@MIME — MIME encoding (12 methods)

ZYMBA
echo @MIME.toBase64("Hello World");
// "SGVsbG8gV29ybGQ="

echo @MIME.fromBase64("SGVsbG8gV29ybGQ=");
// "Hello World"

@ZLIB — compression (6 methods)

ZYMBA
// Deflate/inflate (raw compression)
$compressed = @ZLIB.deflate("Hello World");
$original = @ZLIB.inflate($compressed);

// Raw deflate (no headers)
$raw = @ZLIB.deflateRaw("Hello World");
$original = @ZLIB.inflateRaw($raw);

// Gzip format
$gzipped = @ZLIB.gzip("Hello World");
$original = @ZLIB.gunzip($gzipped);

@XML — XML parsing (5 methods)

ZYMBA
// Parse XML to tree structure
$xml = "<root><item id=\"1\">Hello</item><item id=\"2\">World</item></root>";
$tree = @XML.parseToTree($xml);
echo $tree.children[0].cdata; // "Hello"

// Create XML from tree
$xmlStr = @XML.createFromTree($tree);

// Parse to array
$data = @XML.parse($xml);

// Escape/unescape XML content
echo @XML.escape("<Hello & World>"); // "&lt;Hello &amp; World&gt;"
echo @XML.unescape("&lt;test&gt;"); // "<test>"