mjohnson / type-converter
此包已被废弃且不再维护。未建议替代包。
将一种资源转换为另一种(XML,JSON,对象,数组,序列化)。
2.0.0
2012-11-21 04:16 UTC
Requires
- php: >=5.2
This package is auto-updated.
Last update: 2022-02-01 12:22:04 UTC
README
一个处理将特定资源格式/内容类型检测和转换为其他格式的类。当前支持的格式包括:XML,JSON,数组,对象,序列化
要求
- PHP 5.3+ - v2.x
- PHP 5.2+ - v1.x
- SimpleXML - https://php.ac.cn/manual/book.simplexml.php
文档
类非常直接。如果您想将某些内容转换为另一种格式,请使用“to”方法。
$object = TypeConverter::toObject($resource);
$array = TypeConverter::toArray($resource);
$json = TypeConverter::toJson($resource);
$xml = TypeConverter::toXml($resource);
$ser = TypeConverter::toSerialize($resource);
如果您想检测资源类型,请使用“is”方法。如果您使用上述“to”方法,它将自动进行“is”检测。
TypeConverter::isObject($resource);
TypeConverter::isArray($resource);
TypeConverter::isJson($resource);
TypeConverter::isXml($resource);
TypeConverter::isSerialized($resource);
如果您想获取资源的字符串表示,请使用默认的is()方法。
$resource = array();
TypeConverter::is($resource); // array
最后,您可以转换XML文档为数组(必须具有SimpleXML)。
$array = TypeConverter::xmlToArray($xml, TypeConverter::XML_MERGE);
当使用xmlToArray()时,您可以定义节点属性和值的格式。以下是一些可用常量。
XML_NONE - Disregard XML attributes and only return the value.
XML_MERGE - Merge attributes and the value into a single dimension; the values key will be "value".
XML_GROUP - Group the attributes into a key of "attributes" and the value into a key of "value".
XML_OVERWRITE - Attributes will only be returned.
务必在支持UTF-8时设置内容类型。
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">