Пример #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) . "'.");
 }
Пример #2
0
 /**
  * Create configuration for models.
  *
  * @param array $configuration
  * @param Factory $modelFactory
  * @throws \InvalidArgumentException
  */
 public function __construct(array $configuration, Factory $modelFactory)
 {
     if (empty($configuration)) {
         throw new \InvalidArgumentException('Configurations of index are empty.');
     }
     $this->modelFactory = $modelFactory;
     foreach ($configuration as $className => $options) {
         $fields = array_get($options, 'fields', []);
         if (count($fields) == 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, 'primary_key' => array_get($options, 'primary_key', self::FIELD_LABEL_DEFAULT_DB_PK)];
     }
 }