예제 #1
0
 /**
  * @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
 public function testGetQualifiedColumnName()
 {
     $localization = new Localization();
     $qualifiedColumnName = $localization->getQualifiedColumnName('region_code');
     $this->assertEquals($qualifiedColumnName, 'localizations.region_code');
 }
예제 #3
0
 /**
  * @group localizations
  */
 public function testPutOneChildLocalization()
 {
     $entity = factory(TestEntity::class)->create();
     $this->addRelatedEntities($entity);
     $childEntity = $entity->testMany->first();
     $supportedRegions = array_pluck(config('regions.supported'), 'code');
     $region = array_pop($supportedRegions);
     $localization = ['value' => 'foobar'];
     // Give entity a localization
     $this->withAuthorization()->putJson('/test/entities/' . $entity->entity_id . '/child/' . $childEntity->entity_id . '/localizations/' . $region, $localization);
     $this->assertResponseStatus(201);
     // Get the saved localization
     $localizationModel = $childEntity->localizations->where('region_code', $region)->first();
     $savedLocalization = $localizationModel->localizations;
     // Ensure localization was saved correctly
     $this->assertEquals($localization['value'], $savedLocalization['value']);
     // Assert the cache
     $cachedLocalization = Localization::getFromCache($localizationModel->localizable_id, $localizationModel->region_code);
     $this->assertEquals($localization, $cachedLocalization);
     $localizationCount = Localization::count();
     // Give entity an empty localization
     $this->withAuthorization()->putJson('/test/entities/' . $entity->entity_id . '/child/' . $childEntity->entity_id . '/localizations/' . $region, []);
     $this->assertResponseStatus(204);
     // Ensure the localization has been deleted (and only one localization)
     $this->assertEquals($entity->localizations()->where('region_code', $region)->count(), 0);
     $this->assertEquals(Localization::count(), $localizationCount - 1);
     // Check that the cached localization has been removed
     $this->assertEquals(Localization::getFromCache($localizationModel->localizable_id, $localizationModel->region_code), null);
 }
예제 #4
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;
             }
         }
     }
 }