Esempio n. 1
0
 public function validateSecondaryAbilities($attribute, $params)
 {
     if ($this->hasErrors($attribute)) {
         return;
     }
     $values = $this->{$attribute};
     if (!is_array($values)) {
         $this->addError($attribute, "{$attribute} must be an array of key-string.");
         return;
     }
     if (count($values) < 1 || count($values) > 3) {
         $this->addError($attribute, "{$attribute} must be contain 1-3 values, " . count($values) . " given.");
         return;
     }
     foreach ($values as $i => $value) {
         $value = (string) $value;
         if ($value == '') {
             continue;
         }
         $ability = Ability::findOne(['key' => $value]);
         if (!$ability) {
             $this->addError($attribute, "[{$i}] {$value} is invalid key-string.");
         }
     }
 }
Esempio n. 2
0
 protected function processGear($key)
 {
     if ($this->isTest || !$this->gears instanceof PostGearsForm) {
         return null;
     }
     $gearForm = $this->gears->{$key};
     if (!$gearForm instanceof BaseGearForm) {
         return null;
     }
     $gearModel = $gearForm->getGearModel();
     // may null
     $primaryAbility = $gearForm->primary_ability ? Ability::findOne(['key' => $gearForm->primary_ability]) : null;
     $secondaryAbilityIdList = [];
     if (is_array($gearForm->secondary_abilities)) {
         foreach ($gearForm->secondary_abilities as $aKey) {
             if ($aKey == '') {
                 $secondaryAbilityIdList[] = null;
             } else {
                 if ($a = Ability::findOne(['key' => $aKey])) {
                     $secondaryAbilityIdList[] = $a->id;
                 }
             }
         }
     }
     $fingerPrint = GearConfiguration::generateFingerPrint($gearModel ? $gearModel->id : null, $primaryAbility ? $primaryAbility->id : null, $secondaryAbilityIdList);
     $config = GearConfiguration::findOne(['finger_print' => $fingerPrint]);
     if (!$config) {
         $config = new GearConfiguration();
         $config->finger_print = $fingerPrint;
         $config->gear_id = $gearModel ? $gearModel->id : null;
         $config->primary_ability_id = $primaryAbility ? $primaryAbility->id : null;
         if (!$config->save()) {
             throw new \Exception('Could not save gear_counfiguration');
         }
         foreach ($secondaryAbilityIdList as $aId) {
             $sub = new GearConfigurationSecondary();
             $sub->config_id = $config->id;
             $sub->ability_id = $aId;
             if (!$sub->save()) {
                 throw new \Exception('Could not save gear_configuration_secondary');
             }
         }
     }
     return $config->id;
 }
 /**
  * Finds the Ability model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Ability the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Ability::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }