从我的意见继:
"I am not aware of any such library to do this, but as a rough guide I think you probably want to create some kind of adapters which all use a common interface. These adapters can then be written to deal with the conversion you are trying to achieve via some open-source library. Manipulating the output might be a good excuse to use the decorator pattern :) Sorry I could not be of much more help."
你以后什么,我想一个例子如下:
的接口为您的适配器
interface DataConvertor
{
public function convert(DataInterface $data);
}
的接口为你传递的数据(数据将是一个对象也有一个通用的接口来工作)。
interface DataInterface
{
/**
* returns a json string
*/
public function asJson();
}
然后你可以创建你的适配器用于某些第三方库。
class SomeThirdPartyNameAdapter implements DataConvertor
{
public function convert($data)
{
//some logic here to make my data object with a known asJon method
//suitable for use for some 3rd party library, and use that library.
$rawJson = $data->asJson();
//manipulate this as needed ($compatibleData)
$thirdPartyLib = new ThirdPartyLib();
return $thirdPartyLib->thirdPartyMethod($compatibleData);
}
}
显然,这只是一个粗略的指南,有可能是这个其他部分可以使抽象的(例如具有适配器实现DataConvertor接口,同时也延长了一些抽象类继承某些功能或其他方法添加到你的界面)。
希望这有助于
我不知道有任何这样的库要做到这一点,但作为一个粗略的指南,我认为你可能想创建某种适配器所有使用通用接口。然后可以编写这些适配器来处理您尝试通过某个开源库实现的转换。操作输出可能是使用装饰器模式的好借口:)对不起,我无法提供更多帮助。 – 2013-04-11 13:37:34
谢谢卡尔。你是否有这种类型的适配器的例子? – robjmills 2013-04-12 07:57:36
我假设你指的是适配器模式btw? – robjmills 2013-04-12 13:46:50