public function testNgRestConfigAW()
 {
     $config = new ConfigBuilder('model\\class\\name');
     $config->aw->load(['class' => 'luya\\admin\\aws\\ChangePassword', 'alias' => 'Change Password']);
     $cfg = $config->getConfig();
     $this->assertArrayHasKey('aw', $cfg);
     $aw = $cfg['aw'];
     $this->assertArrayHasKey('00a8a03b008f1b28d968f894d83c2e87c64c046c', $aw);
     $obj = $aw['00a8a03b008f1b28d968f894d83c2e87c64c046c'];
     $this->assertArrayHasKey('objectConfig', $obj);
     $this->assertArrayHasKey('alias', $obj);
     $this->assertArrayHasKey('icon', $obj);
     $ngRestConfig = new Config(['apiEndpoint' => 'api-admin-test', 'primaryKey' => 'id']);
     $ngRestConfig->setConfig($cfg);
 }
示例#2
0
 /**
  * Inject data from the model into the config, usage exmple in ngRestConfig method context:
  *
  * ```php
  * public function ngRestConfig($config)
  * {
  *     // ...
  *     $this->ngRestConfigDefine($config, 'list', ['firstname', 'lastname', 'image_id']);
  *     $this->ngRestConfigDefine($config, 'create', ['firstname', 'lastname', 'description', 'position', 'image_id']);
  *     $this->ngRestConfigDefine($config, 'update', ['firstname', 'lastname', 'description', 'position', 'image_id']);
  *     // ....
  *     return $config;
  * }
  * ```
  *
  * You can also use an array defintion to handle booth types at the same time
  *
  * ```php
  * public function ngRestConfig($config)
  * {
  *     // ...
  *     $this->ngRestConfigDefine($config, ['create', 'update'], ['firstname', 'lastname', 'description', 'position', 'image_id']);
  *     // ....
  *     return $config;
  * }
  * ```
  *
  * @param \luya\admin\ngrest\ConfigBuilder $config The config which the defintion should be append
  * @param string|array $assignedType This can be a string with a type or an array with multiple types
  * @param array $fields An array with fields assign to types type based on the an `ngRestAttributeTypes` defintion.
  * @throws \yii\base\InvalidConfigException
  * @since 1.0.0-beta4
  */
 public function ngRestConfigDefine(ConfigBuilder $config, $assignedType, array $fields)
 {
     $types = $this->ngRestAttributeTypes();
     $extraTypes = $this->ngRestExtraAttributeTypes();
     $scenarios = $this->scenarios();
     $assignedType = (array) $assignedType;
     foreach ($assignedType as $type) {
         $scenario = false;
         $scenarioFields = [];
         if ($type == 'create' || $type == 'update') {
             $scenario = 'rest' . $type;
             if (!isset($scenarios[$scenario])) {
                 throw new InvalidConfigException("The scenario '{$scenario}' does not exists in your scenarios list, have you forgote to defined the '{$scenario}' in the scenarios() method?");
             } else {
                 $scenarioFields = $scenarios[$scenario];
             }
         }
         foreach ($fields as $field) {
             if (!isset($types[$field]) && !isset($extraTypes[$field])) {
                 throw new InvalidConfigException("The ngrest attribue '{$field}' does not exists in ngRestAttributeTypes() nor in ngRestExtraAttributeTypes() method.");
             }
             if ($scenario && !in_array($field, $scenarioFields)) {
                 throw new InvalidConfigException("The field '{$field}' does not exists in the scenario '{$scenario}'. You have to define them in the scenarios() method.");
             }
             if (isset($extraTypes[$field])) {
                 $typeField = 'extraField';
                 $definition = $extraTypes[$field];
             } else {
                 $typeField = 'field';
                 $definition = $types[$field];
             }
             $args = [];
             if (is_array($definition)) {
                 if (array_key_exists('class', $definition)) {
                     $method = $definition['class'];
                     unset($definition['class']);
                 } else {
                     $method = $config->prepandAdminPlugin($definition[0]);
                     $args = array_slice($definition, 1);
                 }
             } else {
                 $method = $config->prepandAdminPlugin($definition);
             }
             $config->{$type}->{$typeField}($field, $this->getAttributeLabel($field))->addPlugin($method, $args);
         }
     }
 }