Example #1
0
 /**
  * Get configuration for model.
  *
  * @param Model $model
  * @return array
  * @throws \InvalidArgumentException
  */
 private function config(Model $model)
 {
     $classUid = $this->modelFactory->classUid($model);
     foreach ($this->configuration as $config) {
         if ($config['class_uid'] === $classUid) {
             return $config;
         }
     }
     throw new \InvalidArgumentException("Configuration doesn't exist for model of class '" . get_class($model) . "'.");
 }
 /**
  * Create configuration for models.
  *
  * @param array $configuration
  * @param Factory $modelFactory
  * @throws \InvalidArgumentException
  */
 public function __construct(array $configuration, Factory $modelFactory)
 {
     $this->modelFactory = $modelFactory;
     foreach ($configuration as $className => $options) {
         $fields = array_get($options, 'fields', []);
         $optionalAttributes = array_get($options, 'optional_attributes', []);
         if (count($fields) == 0 && count($optionalAttributes) == 0) {
             throw new \InvalidArgumentException("Parameter 'fields' and/or 'optional_attributes ' for '{$className}' class must be specified.");
         }
         $modelRepository = $modelFactory->newInstance($className);
         $classUid = $modelFactory->classUid($className);
         $this->configuration[] = ['repository' => $modelRepository, 'class_uid' => $classUid, 'fields' => $fields, 'optional_attributes' => $optionalAttributes, 'private_key' => array_get($options, 'private_key', 'id')];
     }
 }