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