Skip to content

Commit

Permalink
Merge branch 'issue-79' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jun 26, 2017
2 parents 37aa323 + 067848c commit c1153ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/extension/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/extension/BitMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
26 changes: 16 additions & 10 deletions src/extension/BitMaskTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected function setMaskCreate(array $jsonProps)
$this->model->$field &= ~$fVal;
}
}
unset($this->model->$flag);
}
}

Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit c1153ae

Please sign in to comment.