從我的意見繼:
"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