26 private $schemaInModels;
34 if (!static::$instance) {
38 return static::$instance;
48 $this->schemaInModels = $schemaInModels;
59 public function addVisitor($location, ResponseVisitorInterface $visitor)
61 $this->factory->addResponseVisitor($location, $visitor);
69 $type = $operation->getResponseType();
73 $model = $operation->getServiceDescription()->getModel($operation->getResponseClass());
80 return parent::handleParsing($command, $response, $contentType);
83 return new Model(parent::handleParsing($command, $response, $contentType));
86 return new Model($this->
visitResult($model, $command, $response), $this->schemaInModels ? $model :
null);
98 protected function parseClass(CommandInterface $command)
101 $event =
new CreateResponseClassEvent(array(
'command' => $command));
102 $command->getClient()->getEventDispatcher()->dispatch(
'command.parse_response', $event);
103 if ($result = $event->getResult()) {
107 $className = $command->getOperation()->getResponseClass();
108 if (!method_exists($className,
'fromCommand')) {
112 return $className::fromCommand($command);
124 protected function visitResult(Parameter $model, CommandInterface $command, Response $response)
126 $foundVisitors = $result = $knownProps = array();
127 $props = $model->getProperties();
129 foreach ($props as $schema) {
130 if ($location = $schema->getLocation()) {
132 if (!isset($foundVisitors[$location])) {
133 $foundVisitors[$location] = $this->factory->getResponseVisitor($location);
134 $foundVisitors[$location]->before($command, $result);
140 if (($additional = $model->getAdditionalProperties()) instanceof Parameter) {
145 foreach ($props as $schema) {
146 $knownProps[$schema->getName()] = 1;
147 if ($location = $schema->getLocation()) {
148 $foundVisitors[$location]->visit($command, $response, $schema, $result);
153 if ($additional ===
false) {
154 $result = array_intersect_key($result, $knownProps);
158 foreach ($foundVisitors as $visitor) {
159 $visitor->after($command);
167 CommandInterface $command,
169 Parameter $additional,
171 array &$foundVisitors
174 if ($location = $additional->getLocation()) {
175 if (!isset($foundVisitors[$location])) {
176 $foundVisitors[$location] = $this->factory->getResponseVisitor($location);
177 $foundVisitors[$location]->before($command, $result);
180 if (is_array($result)) {
182 foreach (array_keys($result) as $key) {
184 if (!$model->getProperty($key)) {
186 $additional->setName($key);
187 $foundVisitors[$location]->visit($command, $response, $additional, $result);
191 $additional->setName(
null);