Open Journal Systems  3.3.0
DefaultRequestSerializer.php
1 <?php
2 
3 namespace Guzzle\Service\Command;
4 
10 
15 {
17  protected $factory;
18 
20  protected static $instance;
21 
26  public static function getInstance()
27  {
28  if (!self::$instance) {
29  self::$instance = new self(VisitorFlyweight::getInstance());
30  }
31 
32  return self::$instance;
33  }
34 
38  public function __construct(VisitorFlyweight $factory)
39  {
40  $this->factory = $factory;
41  }
42 
51  public function addVisitor($location, RequestVisitorInterface $visitor)
52  {
53  $this->factory->addRequestVisitor($location, $visitor);
54 
55  return $this;
56  }
57 
58  public function prepare(CommandInterface $command)
59  {
60  $request = $this->createRequest($command);
61  // Keep an array of visitors found in the operation
62  $foundVisitors = array();
63  $operation = $command->getOperation();
64 
65  // Add arguments to the request using the location attribute
66  foreach ($operation->getParams() as $name => $arg) {
68  $location = $arg->getLocation();
69  // Skip 'uri' locations because they've already been processed
70  if ($location && $location != 'uri') {
71  // Instantiate visitors as they are detected in the properties
72  if (!isset($foundVisitors[$location])) {
73  $foundVisitors[$location] = $this->factory->getRequestVisitor($location);
74  }
75  // Ensure that a value has been set for this parameter
76  $value = $command[$name];
77  if ($value !== null) {
78  // Apply the parameter value with the location visitor
79  $foundVisitors[$location]->visit($command, $request, $arg, $value);
80  }
81  }
82  }
83 
84  // Serialize additional parameters
85  if ($additional = $operation->getAdditionalParameters()) {
86  if ($visitor = $this->prepareAdditionalParameters($operation, $command, $request, $additional)) {
87  $foundVisitors[$additional->getLocation()] = $visitor;
88  }
89  }
90 
91  // Call the after method on each visitor found in the operation
92  foreach ($foundVisitors as $visitor) {
93  $visitor->after($command, $request);
94  }
95 
96  return $request;
97  }
98 
109  protected function prepareAdditionalParameters(
110  OperationInterface $operation,
111  CommandInterface $command,
113  Parameter $additional
114  ) {
115  if (!($location = $additional->getLocation())) {
116  return;
117  }
118 
119  $visitor = $this->factory->getRequestVisitor($location);
120  $hidden = $command[$command::HIDDEN_PARAMS];
121 
122  foreach ($command->toArray() as $key => $value) {
123  // Ignore values that are null or built-in command options
124  if ($value !== null
125  && !in_array($key, $hidden)
126  && !$operation->hasParam($key)
127  ) {
128  $additional->setName($key);
129  $visitor->visit($command, $request, $additional, $value);
130  }
131  }
132 
133  return $visitor;
134  }
135 
143  protected function createRequest(CommandInterface $command)
144  {
145  $operation = $command->getOperation();
146  $client = $command->getClient();
147  $options = $command[AbstractCommand::REQUEST_OPTIONS] ?: array();
148 
149  // If the command does not specify a template, then assume the base URL of the client
150  if (!($uri = $operation->getUri())) {
151  return $client->createRequest($operation->getHttpMethod(), $client->getBaseUrl(), null, null, $options);
152  }
153 
154  // Get the path values and use the client config settings
155  $variables = array();
156  foreach ($operation->getParams() as $name => $arg) {
157  if ($arg->getLocation() == 'uri') {
158  if (isset($command[$name])) {
159  $variables[$name] = $arg->filter($command[$name]);
160  if (!is_array($variables[$name])) {
161  $variables[$name] = (string) $variables[$name];
162  }
163  }
164  }
165  }
166 
167  return $client->createRequest($operation->getHttpMethod(), array($uri, $variables), null, null, $options);
168  }
169 }
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight
Definition: VisitorFlyweight.php:12
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Service\Description\OperationInterface\hasParam
hasParam($name)
Guzzle\Service\Command
Definition: AbstractCommand.php:3
Guzzle\Service\Command\DefaultRequestSerializer\prepareAdditionalParameters
prepareAdditionalParameters(OperationInterface $operation, CommandInterface $command, RequestInterface $request, Parameter $additional)
Definition: DefaultRequestSerializer.php:112
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Service\Command\DefaultRequestSerializer\createRequest
createRequest(CommandInterface $command)
Definition: DefaultRequestSerializer.php:146
Guzzle\Service\Command\DefaultRequestSerializer
Definition: DefaultRequestSerializer.php:14
Guzzle\Service\Command\RequestSerializerInterface
Definition: RequestSerializerInterface.php:11
Guzzle\Service\Command\DefaultRequestSerializer\addVisitor
addVisitor($location, RequestVisitorInterface $visitor)
Definition: DefaultRequestSerializer.php:54
Guzzle\Service\Description\OperationInterface
Definition: OperationInterface.php:10
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\getInstance
static getInstance()
Definition: VisitorFlyweight.php:52
Guzzle\Service\Command\DefaultRequestSerializer\getInstance
static getInstance()
Definition: DefaultRequestSerializer.php:29
Guzzle\Service\Command\CommandInterface
Definition: CommandInterface.php:17
Guzzle\Service\Description\Parameter\setName
setName($name)
Definition: Parameter.php:262
Guzzle\Service\Command\DefaultRequestSerializer\$instance
static $instance
Definition: DefaultRequestSerializer.php:23
Guzzle\Service\Command\CommandInterface\getClient
getClient()
Guzzle\Service\Command\AbstractCommand\REQUEST_OPTIONS
const REQUEST_OPTIONS
Definition: AbstractCommand.php:31
Guzzle\Service\Command\LocationVisitor\Request\RequestVisitorInterface
Definition: RequestVisitorInterface.php:12
Guzzle\Service\Command\DefaultRequestSerializer\__construct
__construct(VisitorFlyweight $factory)
Definition: DefaultRequestSerializer.php:41
Guzzle\Service\Command\CommandInterface\getOperation
getOperation()
Guzzle\Service\Command\DefaultRequestSerializer\$factory
$factory
Definition: DefaultRequestSerializer.php:20
Guzzle\Service\Command\DefaultRequestSerializer\prepare
prepare(CommandInterface $command)
Definition: DefaultRequestSerializer.php:61
Guzzle\Service\Description\Parameter\getLocation
getLocation()
Definition: Parameter.php:514
Guzzle\Common\ToArrayInterface\toArray
toArray()