/** * Get either the predefined (subset of) collection/model, or. * @return Collection|mixed|BaseModel */ private function getPredefinedOrMocks() { if ($this->entityCount > 1) { $collection = new Collection(); if ($this->predefinedEntities) { if ($this->predefinedEntities instanceof Collection) { $collection = $collection->merge($this->predefinedEntities->random($this->entityCount)); } else { $collection->push($this->predefinedEntities); } } if ($collection->count() < $this->entityCount) { /** @var Collection $collection */ $collection = $collection->merge($this->getModelMock($this->entityCount - $collection->count())); } return $collection; } else { if ($this->predefinedEntities) { if ($this->predefinedEntities instanceof Collection) { return $this->predefinedEntities->random(); } else { return $this->predefinedEntities; } } return $this->getModelMock(); } }
/** * @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])); }
/** * @param array $attributes */ public function __construct(array $attributes = array()) { parent::__construct($attributes); }
/** * @param Request $request * @param $id * @param BaseModel $model * @param bool|true $requireEntityKey * @return bool */ protected function checkEntityIdMatchesRoute(Request $request, $id, BaseModel $model, $requireEntityKey = true) { $keyName = $model->getKeyName(); if (!$request->has($keyName)) { if (!$requireEntityKey) { return true; //it is ok if the key is not set (for patch requests etc) } else { throw new BadRequestException("Request entity must include entity id ({$keyName}) for " . get_class($model)); } } if ((string) $request->json($keyName) !== (string) $id) { throw new BadRequestException('Provided entity body does not match route parameter. The entity key cannot be updated'); } return true; }
/** * Override for custom functionality. * * @param $baseModel $model * @param Collection|BaseModel[] $existingModels * @param Collection|array $requestCollection * @return Collection|array */ protected function fillModels(BaseModel $baseModel, $existingModels, $requestCollection) { return $baseModel->hydrateRequestCollection($requestCollection, $existingModels); }
protected static function boot() { parent::boot(); //register the parent event handlers first static::created(function (IndexedModel $model) { $model->addToIndex(); return true; }, PHP_INT_MAX); static::deleted(function (IndexedModel $model) { $model->removeFromIndex(); return true; }, PHP_INT_MAX); static::updated(function (IndexedModel $model) { $model->addToIndex(); return true; }, PHP_INT_MAX); }
/** * Attempt to find localizations in cache and replace the attributes with the items. * @param $object */ protected function applyCreated(BaseModel $object) { $object->setVisible(['']); }