Exemplo n.º 1
0
 /**
  * Merge the given config on to "configurable" (either a GeneratorInterface
  * or a RendererInterface) instance's default config and validate it
  * according to the "configurable" instance's JSON schema.
  *
  * @param ConfigurableInterface $configurable
  * @param array $config
  *
  * @return array
  */
 private function mergeAndValidateConfig(RegistrableInterface $configurable, array $config)
 {
     $config = array_replace_recursive($configurable->getDefaultConfig(), $config);
     // not sure if there is a better way to convert the schema array to objects
     // as expected by the validator.
     $validationConfig = json_decode(json_encode($config));
     $schema = $configurable->getSchema();
     if (!is_array($schema)) {
         throw new \InvalidArgumentException(sprintf('Configurable class "%s" must return the JSON schema as an array', get_class($configurable)));
     }
     $schema['properties'][$this->serviceType] = array('type' => 'string');
     $schema['properties']['_name'] = array('type' => 'string');
     // convert the schema to a \stdClass
     $schema = json_decode(json_encode($schema));
     // json_encode encodes an array instead of an object if the schema
     // is empty. JSON schema requires an object.
     if (empty($schema)) {
         $schema = new \stdClass();
     }
     $this->validator->check($validationConfig, $schema);
     if (!$this->validator->isValid()) {
         $errorString = array();
         foreach ($this->validator->getErrors() as $error) {
             $errorString[] = sprintf('[%s] %s', $error['property'], $error['message']);
         }
         throw new \InvalidArgumentException(sprintf('Invalid JSON: %s%s', PHP_EOL . PHP_EOL . PHP_EOL, implode(PHP_EOL, $errorString)));
     }
     return $config;
 }