コード例 #1
0
 /**
  * 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();
     }
 }
コード例 #2
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]));
 }
コード例 #3
0
ファイル: Revision.php プロジェクト: spira/revisionable
 /**
  * @param array $attributes
  */
 public function __construct(array $attributes = array())
 {
     parent::__construct($attributes);
 }
コード例 #4
0
 /**
  * @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;
 }
コード例 #5
0
ファイル: ApiController.php プロジェクト: spira/api-core
 /**
  *  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);
 }
コード例 #6
0
ファイル: IndexedModel.php プロジェクト: spira/api-core
 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);
 }
コード例 #7
0
 /**
  * Attempt to find localizations in cache and replace the attributes with the items.
  * @param $object
  */
 protected function applyCreated(BaseModel $object)
 {
     $object->setVisible(['']);
 }