1: <?php
2:
3: namespace Guzzle\Service\Command;
4:
5: use Guzzle\Http\Message\Response;
6:
7: /**
8: * Default HTTP response parser used to marshal JSON responses into arrays and XML responses into SimpleXMLElement
9: */
10: class DefaultResponseParser implements ResponseParserInterface
11: {
12: /**
13: * @var self
14: */
15: protected static $instance;
16:
17: /**
18: * Get a cached instance of the default response parser
19: * @return self
20: * @codeCoverageIgnore
21: */
22: public static function getInstance()
23: {
24: if (!self::$instance) {
25: self::$instance = new self;
26: }
27:
28: return self::$instance;
29: }
30:
31: /**
32: * {@inheritdoc}
33: */
34: public function parse(CommandInterface $command)
35: {
36: $response = $command->getRequest()->getResponse();
37:
38: // Account for hard coded content-type values specified in service descriptions
39: if ($contentType = $command->get('command.expects')) {
40: $response->setHeader('Content-Type', $contentType);
41: } else {
42: $contentType = (string) $response->getHeader('Content-Type');
43: }
44:
45: return $this->handleParsing($command, $response, $contentType);
46: }
47:
48: /**
49: * {@inheritdoc}
50: */
51: protected function handleParsing(AbstractCommand $command, Response $response, $contentType)
52: {
53: $result = $response;
54: if ($result->getBody()) {
55: if (stripos($contentType, 'json') !== false) {
56: $result = $result->json();
57: } if (stripos($contentType, 'xml') !== false) {
58: $result = $result->xml();
59: }
60: }
61:
62: return $result;
63: }
64: }
65: