-
-
Save Danack/7d66f4dea91b21acbc3534ede8390dea to your computer and use it in GitHub Desktop.
ResponseFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface DataResponseFactory | |
{ | |
function createFromData(array $responseData); | |
} | |
interface FooResponseFactory | |
{ | |
function createFromFoo(Foo $foo); | |
} | |
class RequestResponseFactory implements DataResponseFactory, FooResponseFactory | |
{ | |
private $request; | |
function __construct(Request $request) { | |
$this->request = $request; | |
} | |
function createFromData(array $responseData) { | |
$acceptHeader = $this->request->getHeader('Accept'); | |
if (strpos($acceptHeader, "text/xml") === 0) { | |
return new XMLResponse($responseData); | |
} | |
return new JsonResponse($responseData); | |
} | |
function createFrom(Foo $foo) { | |
$acceptHeader = $this->request->getHeader('Accept'); | |
if (strpos($acceptHeader, "text/xml") === 0) { | |
return new XMLResponse($foo->toData()); | |
} | |
return new JsonResponse($foo->toData()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment