Example #1
0
 /**
  * @param  string $name
  * @param  array  $arguments
  * @return array|object
  */
 public function __call($name, $arguments)
 {
     $args = array();
     $count = count($arguments);
     $name = substr($name, 3);
     if (!isset($this->resources[$name])) {
         throw new Exception\BadMethodCallException(sprintf(self::ERROR_RESOURCE_NOT_FOUND, $name));
     }
     if ($count > 2) {
         throw new Exception\LengthException(sprintf('Too many arguments passed. 2 or less expected, %i given.', $count));
     }
     foreach ($arguments as $num => $arg) {
         if (is_array($arg)) {
             $args = array_merge($args, $arg);
         } elseif (is_string($arg)) {
             $args['url'] = $arg;
         } elseif ($arg instanceof ConsumeInterface) {
             foreach ($arg->consume() as $key => $value) {
                 if (!isset($args[$key])) {
                     $args[$key] = $value;
                 }
             }
         } else {
             $errType = gettype($arg);
             $errType = $errType === 'object' ? get_class($closure) : $errType;
             throw new Exception\InvalidArgumentException(sprintf('Argument %i must be an array or implement ConsumeInterface, %s given.', $num, $errType));
         }
     }
     $shortName = constant(get_class($this) . '::SHORT_NAME');
     $config = $this->serviceLocator->get(sprintf('%s.config.%s', $shortName, strtolower($name)), true);
     $type = $config->getResourceType();
     $lastModified = isset($args['lastmodified']) ? $args['lastmodified'] : null;
     $returnType = isset($args['return']) ? $args['return'] : $this->returnType;
     /**
      * Note: DATE_RFC1123 my not be RFC 1123 compliant, depending on your platform.
      * @link http://www.php.net/manual/de/function.gmdate.php#25031
      */
     $format = 'D, d M Y H:i:s \\G\\M\\T';
     $lastModified = is_numeric($lastModified) ? gmdate($format, $lastModified) : $lastModified;
     $lastModified = $lastModified instanceof \DateTime ? $lastModified->format($format) : $lastModified;
     if ($returnType !== self::RETURN_PLAIN && $returnType !== self::RETURN_OBJECT) {
         throw new Exception\DomainException('Invalid return type specified.');
     }
     if ($type !== ConfigurationInterface::TYPE_STATIC_URL) {
         if (!isset($args['region'])) {
             $defaultRegion = $this->connection->getDefaultRegion();
             if ($defaultRegion !== null) {
                 $args['region'] = $defaultRegion;
             }
         }
         if (!isset($args['locale']) && isset($args['region'])) {
             $defaultLocale = $this->connection->getDefaultLocale($args['region']);
             if ($defaultLocale !== null) {
                 $args['locale'] = $defaultLocale;
             }
         }
     }
     switch ($type) {
         case ConfigurationInterface::TYPE_STATIC_URL:
             if (!isset($args['url'])) {
                 throw new Exception\BadMethodCallException(sprintf(self::ERROR_MSSING_ARGUMENT, 'url'));
             }
             $url = $args['url'];
             /**
              * Necessary, because the URL returned from /api/wow/auction/data/$realm
              * will use http as scheme, instead of https, when requested via https.
              * Note: I'm unable to test this behavior for authenticated requests.
              */
             if ($config->isAuthenticationPossible() && $this->connection->doSecureRequest()) {
                 $url = preg_replace('/^http:/', 'https:', $url);
             }
             break;
         case ConfigurationInterface::TYPE_STATIC_PATH:
         case ConfigurationInterface::TYPE_DYNAMIC_URL:
         case ConfigurationInterface::TYPE_DYNAMIC_PATH:
             $url = $this->buildUrl($config, $args, $name);
             break;
         default:
             throw new Exception\DomainException(sprintf('%s Resource type is not valid.', sprintf(self::ERROR_INVALID_CONFIG, $name)));
             break;
     }
     $response = $this->connection->request(array('url' => $url, 'config' => $config, 'lastmodified' => $lastModified));
     switch ($returnType) {
         case self::RETURN_OBJECT:
             $class = $this->serviceLocator->get($this->resources[$name]);
             if (!$class instanceof EntityInterface) {
                 throw new Exception\DomainException(sprintf('Resource %s must implement EntityInterface', $name));
             }
             $class->populate($response['content']);
             if (isset($response['headers'])) {
                 $class->setResponseHeaders($response['headers']);
             }
             return $class;
         case self::RETURN_PLAIN:
             return $response;
     }
 }