0
隨着CsvReader一種類型的對象:尋找設計模式 - 不同來源
class CsvReader {
public static function createFromString($csvString){
/.../
return new static($something)
}
}
我打算用csvString來自不同來源的操作:讀取文件時,從HTTP響應等的身體所以即時通訊尋找設計模式,輕鬆創建此csvReaders對象。現在,我已經結束了這樣的事情:
use SplFileInfo as UploadedFile;
use CsvReader as Reader;
use GuzzleHttp\Client as HttpClient;
class CsvFactory {
public function createFromUploadedFile(UploadedFile $uploadedFile){
return Reader::createFromString($uploadedFile->toString());
}
public function createFromHttpEndpoint(HttpClient $client, $url, $options = array()){
$response = $client->request('GET', $url, $options);
if($response->getStatusCode() != 200){
throw new Exception('Http Code Not Ok', $response->getStatusCode());
}
return Reader::createFromString($response->getBody());
}
}
我覺得它可以在更好的方式完成。但是如何?
*返回新的靜態($ something)*? – CKing