| @@ 20-81 (lines=62) @@ | ||
| 17 | * @author Flipbox Factory <[email protected]> |
|
| 18 | * @since 1.1.0 |
|
| 19 | */ |
|
| 20 | trait ElementTrait |
|
| 21 | { |
|
| 22 | ||
| 23 | use ObjectTrait; |
|
| 24 | ||
| 25 | /******************************************* |
|
| 26 | * ELEMENT -to- RECORD |
|
| 27 | *******************************************/ |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @param ElementInterface $element |
|
| 31 | * @param Record $record |
|
| 32 | * @param bool $mirrorScenario |
|
| 33 | */ |
|
| 34 | public function transferToRecord(ElementInterface $element, Record $record, $mirrorScenario = true) |
|
| 35 | { |
|
| 36 | ||
| 37 | /** @var $element Element */ |
|
| 38 | ||
| 39 | if ($mirrorScenario === true) { |
|
| 40 | ||
| 41 | // Mirror scenarios |
|
| 42 | $record->setScenario($element->getScenario()); |
|
| 43 | ||
| 44 | } |
|
| 45 | ||
| 46 | // Transfer attributes |
|
| 47 | $record->setAttributes($element->toArray()); |
|
| 48 | ||
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * @param ElementInterface $element |
|
| 53 | * @param bool $mirrorScenario |
|
| 54 | * @return Record |
|
| 55 | */ |
|
| 56 | public function toRecord(ElementInterface $element, $mirrorScenario = true) |
|
| 57 | { |
|
| 58 | ||
| 59 | if ($id = $element->getId()) { |
|
| 60 | ||
| 61 | $record = $this->findRecordByCondition( |
|
| 62 | ['id' => $id] |
|
| 63 | ); |
|
| 64 | ||
| 65 | } |
|
| 66 | ||
| 67 | if (empty($record)) { |
|
| 68 | ||
| 69 | // Create new record |
|
| 70 | $record = $this->createRecord(); |
|
| 71 | ||
| 72 | } |
|
| 73 | ||
| 74 | // Populate the record attributes |
|
| 75 | $this->transferToRecord($element, $record, $mirrorScenario); |
|
| 76 | ||
| 77 | return $record; |
|
| 78 | ||
| 79 | } |
|
| 80 | ||
| 81 | } |
|
| 82 | ||
| @@ 24-82 (lines=59) @@ | ||
| 21 | * @author Flipbox Factory <[email protected]> |
|
| 22 | * @since 1.1.0 |
|
| 23 | */ |
|
| 24 | trait ModelTrait |
|
| 25 | { |
|
| 26 | ||
| 27 | use ObjectTrait; |
|
| 28 | ||
| 29 | /******************************************* |
|
| 30 | * Model -to- Record |
|
| 31 | *******************************************/ |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @param Model $model |
|
| 35 | * @param Record $record |
|
| 36 | * @param bool $mirrorScenario |
|
| 37 | * @return void |
|
| 38 | */ |
|
| 39 | public function transferToRecord(Model $model, Record $record, bool $mirrorScenario = true) |
|
| 40 | { |
|
| 41 | ||
| 42 | if ($mirrorScenario === true) { |
|
| 43 | ||
| 44 | // Mirror scenarios |
|
| 45 | $record->setScenario($model->getScenario()); |
|
| 46 | ||
| 47 | } |
|
| 48 | ||
| 49 | // Transfer attributes |
|
| 50 | $record->setAttributes($model->toArray()); |
|
| 51 | ||
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * @param Model $model |
|
| 56 | * @param bool $mirrorScenario |
|
| 57 | * @return Record |
|
| 58 | */ |
|
| 59 | public function toRecord(Model $model, bool $mirrorScenario = true): Record |
|
| 60 | { |
|
| 61 | ||
| 62 | if ($id = $model->id) { |
|
| 63 | $record = $this->findRecordByCondition( |
|
| 64 | ['id' => $id] |
|
| 65 | ); |
|
| 66 | } |
|
| 67 | ||
| 68 | if (empty($record)) { |
|
| 69 | ||
| 70 | // Create new record |
|
| 71 | $record = $this->createRecord(); |
|
| 72 | ||
| 73 | } |
|
| 74 | ||
| 75 | // Populate the record attributes |
|
| 76 | $this->transferToRecord($model, $record, $mirrorScenario); |
|
| 77 | ||
| 78 | return $record; |
|
| 79 | ||
| 80 | } |
|
| 81 | ||
| 82 | } |
|
| 83 | ||