diff --git a/src/extension/ApiController.php b/src/extension/ApiController.php index aaaa5d1..d420c73 100644 --- a/src/extension/ApiController.php +++ b/src/extension/ApiController.php @@ -167,13 +167,13 @@ public function create(Request $request) $this->setMaskCreate($jsonApiAttributes); } $this->model->save(); - // set bit mask from model -> response - if(true === $this->configOptions->isBitMask()) { - $this->setFlagsCreate(); - } // jwt if($this->configOptions->getIsJwtAction() === true) { - $this->createJwtUser(); + $this->createJwtUser(); // !!! model is overridden + } + // set bit mask from model -> response + if(true === $this->configOptions->isBitMask()) { + $this->model = $this->setFlagsCreate(); } $this->setRelationships($json, $this->model->id); $resource = Json::getResource($this->middleWare, $this->model, $this->entity, false, $meta); @@ -206,7 +206,7 @@ public function update(Request $request, int $id) $this->setRelationships($json, $model->id, true); // set bit mask if(true === $this->configOptions->isBitMask()) { - $this->setFlagsUpdate(); + $this->setFlagsUpdate($model); } $resource = Json::getResource($this->middleWare, $model, $this->entity, false, $meta); Json::outputSerializedData($resource); @@ -235,12 +235,12 @@ private function processUpdate($model, array $jsonApiAttributes) $model->$k = $jsonApiAttributes[$k]; } } - // set bit mask - if(true === $this->configOptions->isBitMask()) { - $this->setMaskUpdate(); - } } } + // set bit mask + if(true === $this->configOptions->isBitMask()) { + $this->setMaskUpdate($model, $jsonApiAttributes); + } } /** diff --git a/src/extension/BitMask.php b/src/extension/BitMask.php index c3f5e42..4e4bcda 100644 --- a/src/extension/BitMask.php +++ b/src/extension/BitMask.php @@ -40,13 +40,13 @@ public function getField() public function getFlags() { - if(empty($this->entity[$this->field])) { + if(empty($this->entity[$this->field][ConfigInterface::FLAGS])) { throw new AttributesException('Flags should be preset for bit mask.'); } - return $this->entity[$this->field]; + return $this->entity[$this->field][ConfigInterface::FLAGS]; } public function isHidden() { - return empty($this->entity[ConfigInterface::HIDE_MASK]) ? false : true; + return empty($this->entity[$this->field][ConfigInterface::HIDE_MASK]) ? false : true; } } \ No newline at end of file diff --git a/src/extension/BitMaskTrait.php b/src/extension/BitMaskTrait.php index 485b9e1..5ce6507 100644 --- a/src/extension/BitMaskTrait.php +++ b/src/extension/BitMaskTrait.php @@ -66,6 +66,7 @@ protected function setMaskCreate(array $jsonProps) $this->model->$field &= ~$fVal; } } + unset($this->model->$flag); } } @@ -76,47 +77,52 @@ protected function setMaskCreate(array $jsonProps) protected function setFlagsCreate() { $field = $this->bitMask->getField(); - if(isset($this->model[$field])) { + if(isset($this->model->$field)) { $flags = $this->bitMask->getFlags(); - $mask = $this->model[$field]; + $mask = $this->model->$field; foreach($flags as $flag => $fVal) { - $this->model[$flag] = ($fVal & $mask) ? true : false; + $this->model->$flag = ($fVal & $mask) ? true : false; } } + return $this->model; } /** * Updates bit mask based on bit flags and unset those flags to save via model + * @param $model * @param array $jsonProps + * @return mixed * @throws \rjapi\exception\AttributesException */ - protected function setMaskUpdate(array $jsonProps) + protected function setMaskUpdate(&$model, array $jsonProps) { $field = $this->bitMask->getField(); $flags = $this->bitMask->getFlags(); foreach($flags as $flag => $fVal) { if (isset($jsonProps[$flag])) { if (true === (bool) $jsonProps[$flag]) { - $this->model->$field |= $fVal; + $model->$field |= $fVal; } else if (false === (bool) $jsonProps[$flag]) { - $this->model->$field &= ~$fVal; + $model->$field &= ~$fVal; } } } + return $model; } /** * Sets flags on model to pass them through json api processing + * @param $model * @throws \rjapi\exception\AttributesException */ - protected function setFlagsUpdate() + protected function setFlagsUpdate(&$model) { $field = $this->bitMask->getField(); - if(isset($this->model[$field])) { + if(isset($model[$field])) { $flags = $this->bitMask->getFlags(); - $mask = $this->model[$field]; + $mask = $model[$field]; foreach($flags as $flag => $fVal) { - $this->model[$flag] = ($fVal & $mask) ? true : false; + $model[$flag] = ($fVal & $mask) ? true : false; } } }