/** * Ensures that the visitor for a given location is added to the array * * This will create the visitor if it's not already in $visitors, and then * run its "before()" method, before adding it as $visitors[$location]. */ private function addVisitor(array &$visitors, $location, EmbeddedCommand $command, array &$result) { if ($location && !isset($visitors[$location])) { $visitors[$location] = $this->visitors->getResponseVisitor($location); $visitors[$location]->before($command, $result); } }
public function testAllowsAddingVisitors() { $f = new VisitorFlyweight(); $j1 = new JsonRequestVisitor(); $j2 = new JsonResponseVisitor(); $f->addRequestVisitor('json', $j1); $f->addResponseVisitor('json', $j2); $this->assertSame($j1, $f->getRequestVisitor('json')); $this->assertSame($j2, $f->getResponseVisitor('json')); }
/** * Processes model data according to a parameter schema * * @param Desk\Relationship\Resource\EmbeddedCommand $command * @param Guzzle\Service\Description\Parameter $schema * @param array $data * * @return array */ public function process(EmbeddedCommand $command, Parameter $schema, array $data) { $result = array(); $visitors = array(); $properties = $schema->getProperties(); foreach ($properties as $property) { $location = $property->getLocation(); if ($location && !isset($visitors[$location])) { // add visitor for this location and trigger before() $visitor = $this->visitors->getResponseVisitor($location); $visitor->before($command, $result); $visitors[$location] = $visitor; } } $response = $command->getResponse(); // Visit additional properties when it is an actual schema $additional = $schema->getAdditionalProperties(); if ($additional instanceof Parameter) { // Only visit when a location is specified $location = $additional->getLocation(); if ($location) { if (!isset($visitors[$location])) { $visitors[$location] = $this->visitors->getResponseVisitor($location); $visitors[$location]->before($command, $result); } // Only traverse if an array was parsed from the before() visitors if (is_array($result)) { // Find each additional property foreach (array_keys($result) as $key) { // Check if the model actually knows this property. If so, then it is not additional if (!$schema->getProperty($key)) { // Set the name to the key so that we can parse it with each visitor $additional->setName($key); $visitors[$location]->visit($command, $response, $additional, $result); } } // Reset the additionalProperties name to null $additional->setName(null); } } } // Apply the parameter value with the location visitor foreach ($properties as $property) { $location = $property->getLocation(); if ($location) { $visitors[$location]->visit($command, $response, $property, $result); } } // Call the after() method of each found visitor foreach ($visitors as $visitor) { $visitor->after($command); } return $result; }
/** * Perform transformations on the result array * * @param Parameter $model Model that defines the structure * @param CommandInterface $command Command that performed the operation * @param Response $response Response received * * @return array Returns the array of result data */ protected function visitResult(Parameter $model, CommandInterface $command, Response $response) { $foundVisitors = $result = array(); $props = $model->getProperties(); foreach ($props as $schema) { if ($location = $schema->getLocation()) { // Trigger the before method on the first found visitor of this type if (!isset($foundVisitors[$location])) { $foundVisitors[$location] = $this->factory->getResponseVisitor($location); $foundVisitors[$location]->before($command, $result); } } } // Visit additional properties when it is an actual schema if ($additional = $model->getAdditionalProperties()) { if ($additional instanceof Parameter) { // Only visit when a location is specified if ($location = $additional->getLocation()) { if (!isset($foundVisitors[$location])) { $foundVisitors[$location] = $this->factory->getResponseVisitor($location); $foundVisitors[$location]->before($command, $result); } // Only traverse if an array was parsed from the before() visitors if (is_array($result)) { // Find each additional property foreach (array_keys($result) as $key) { // Check if the model actually knows this property. If so, then it is not additional if (!$model->getProperty($key)) { // Set the name to the key so that we can parse it with each visitor $additional->setName($key); $foundVisitors[$location]->visit($command, $response, $additional, $result); } } // Reset the additionalProperties name to null $additional->setName(null); } } } } // Apply the parameter value with the location visitor foreach ($props as $schema) { if ($location = $schema->getLocation()) { $foundVisitors[$location]->visit($command, $response, $schema, $result); } } // Call the after() method of each found visitor foreach ($foundVisitors as $visitor) { $visitor->after($command); } return $result; }
/** * Perform transformations on the result array * * @param Parameter $model Model that defines the structure * @param CommandInterface $command Command that performed the operation * @param Response $response Response received * * @return array Returns the array of result data */ protected function visitResult(Parameter $model, CommandInterface $command, Response $response) { // Determine what visitors are associated with the model $foundVisitors = $result = array(); foreach ($model->getProperties() as $schema) { if ($location = $schema->getLocation()) { $foundVisitors[$location] = $this->factory->getResponseVisitor($location); $foundVisitors[$location]->before($command, $result); } } foreach ($model->getProperties() as $schema) { /** @var $arg Parameter */ if ($location = $schema->getLocation()) { // Apply the parameter value with the location visitor $foundVisitors[$location]->visit($command, $response, $schema, $result); } } foreach ($foundVisitors as $visitor) { $visitor->after($command); } return $result; }
protected function visitAdditionalProperties(Parameter $model, CommandInterface $command, Response $response, Parameter $additional, &$result, array &$foundVisitors) { // Only visit when a location is specified if ($location = $additional->getLocation()) { if (!isset($foundVisitors[$location])) { $foundVisitors[$location] = $this->factory->getResponseVisitor($location); $foundVisitors[$location]->before($command, $result); } // Only traverse if an array was parsed from the before() visitors if (is_array($result)) { // Find each additional property foreach (array_keys($result) as $key) { // Check if the model actually knows this property. If so, then it is not additional if (!$model->getProperty($key)) { // Set the name to the key so that we can parse it with each visitor $additional->setName($key); $foundVisitors[$location]->visit($command, $response, $additional, $result); } } // Reset the additionalProperties name to null $additional->setName(null); } } }