The following interfaces provide conversion methods for specialized types.
The Arrayable interface provides a toArray() method that returns a key-value array.
class Foo<Tk, Tv> implements Titon\Common\Arrayable<Tk, Tv> {
public function toArray(): array<Tk, Tv> {
return [];
}
}
$foo = new Foo();
$foo->toArray();The Jsonable interface provides a toJson() method that returns a JSON string. An optional JSON flag can be passed to the 1st argument.
class Foo implements Titon\Common\Jsonable {
public function toJson(int $options = 0): string {
return (string) json_encode($value, $options);
}
}
$foo = new Foo();
$foo->toJson();The Mapable interface provides a toMap() method that returns a key-value map.
class Foo<Tk, Tv> implements Titon\Common\Mapable<Tk, Tv> {
public function toMap(): Map<Tk, Tv> {
return Map {};
}
}
$foo = new Foo();
$foo->toMap();The Vectorable interface provides a toVector() method that returns an index based vector.
class Foo<Tv> implements Titon\Common\Vectorable<Tv> {
public function toVector(): Vector<Tv> {
return Vector {};
}
}
$foo = new Foo();
$foo->toVector();The Xmlable interface provides a toXml() method that returns an XML structured string. We suggest using the built-in XML class.
class Foo implements Titon\Common\Xmlable {
public function toXml(string $root = 'root'): string {
return Titon\Type\Xml::from($value, $root)->toString();
}
}
$foo = new Foo();
$foo->toXml();