コード例 #1
0
ファイル: LocalizableTrait.php プロジェクト: spira/api-core
 /**
  * @param BaseModel $model
  * @param $localizations
  * @param $region
  * @return
  * @throws ValidationException
  */
 protected function validateAndSaveLocalizations(BaseModel $model, $localizations, $region)
 {
     // Check to see if parameters exist in model
     foreach ($localizations as $parameter => $localization) {
         if (!$model->isFillable($parameter)) {
             throw new ValidationException(new MessageBag([$parameter => 'Localization for this parameter is not allowed.']));
         }
     }
     // Validate the region
     $regionCode = ['region_code' => $region];
     parent::validateRequest($regionCode, Localization::getValidationRules(null, $regionCode));
     // Localizations are partial updates so only validate the fields which were sent with the request
     $this->validateRequest($localizations, $this->getValidationRules($model->getKey(), $localizations), null, true);
     return $model->localizations()->updateOrCreate($regionCode, array_merge($regionCode, ['localizations' => $localizations]));
 }
コード例 #2
0
 /**
  * Attempt to find localizations in cache and replace the attributes with the items.
  * @param $object
  */
 protected function applyLocalizations(BaseModel $object)
 {
     if (!isset($this->options['region']) || !$object instanceof LocalizableModelInterface) {
         return;
     }
     if ($localizations = Localization::getFromCache($object->getKey(), $this->options['region'])) {
         foreach ($localizations as $attribute => $localization) {
             if (is_array($localization) && is_array($object->{$attribute})) {
                 $object->{$attribute} = $this->mergeRecursive($object->{$attribute}, $localization);
             } else {
                 $object->{$attribute} = $localization;
             }
         }
     }
 }