继我的评论之后:
"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();
}
然后,您可以创建适配器以与某些 3rd 方库一起使用。
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 接口,但也扩展一些抽象类以继承某些功能,或添加到您的接口的其他方法)。
希望这可以帮助