| @@ 18-127 (lines=110) @@ | ||
| 15 | * @author Flipbox Factory <[email protected]> |
|
| 16 | * @since 2.1.0 |
|
| 17 | */ |
|
| 18 | abstract class ModelByIdOrString extends ModelById |
|
| 19 | { |
|
| 20 | ||
| 21 | use traits\ModelByString; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @inheritdoc |
|
| 25 | */ |
|
| 26 | public function find($identifier, string $toScenario = null) |
|
| 27 | { |
|
| 28 | ||
| 29 | if ($model = parent::find($identifier, $toScenario)) { |
|
| 30 | return $model; |
|
| 31 | } |
|
| 32 | ||
| 33 | if (!is_string($identifier)) { |
|
| 34 | return null; |
|
| 35 | } |
|
| 36 | ||
| 37 | return $this->findByString($identifier, $toScenario); |
|
| 38 | ||
| 39 | } |
|
| 40 | ||
| 41 | /******************************************* |
|
| 42 | * CACHE |
|
| 43 | *******************************************/ |
|
| 44 | ||
| 45 | /** |
|
| 46 | * @inheritdoc |
|
| 47 | */ |
|
| 48 | public function findCache($identifier) |
|
| 49 | { |
|
| 50 | ||
| 51 | if ($model = parent::findCache($identifier)) { |
|
| 52 | return $model; |
|
| 53 | } |
|
| 54 | ||
| 55 | if (!is_string($identifier)) { |
|
| 56 | return null; |
|
| 57 | } |
|
| 58 | ||
| 59 | return $this->findCacheByString($identifier); |
|
| 60 | ||
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * @inheritdoc |
|
| 65 | */ |
|
| 66 | public function addToCache(BaseModel $model) |
|
| 67 | { |
|
| 68 | ||
| 69 | parent::addToCache($model); |
|
| 70 | ||
| 71 | $this->cacheByString($model); |
|
| 72 | ||
| 73 | return $this; |
|
| 74 | ||
| 75 | } |
|
| 76 | ||
| 77 | ||
| 78 | /** |
|
| 79 | * @inheritdoc |
|
| 80 | */ |
|
| 81 | public function findCacheByRecord(Record $record) |
|
| 82 | { |
|
| 83 | ||
| 84 | if ($model = parent::findCacheByRecord($record)) { |
|
| 85 | return $model; |
|
| 86 | } |
|
| 87 | ||
| 88 | return $this->findCacheByRecordByString($record); |
|
| 89 | ||
| 90 | } |
|
| 91 | ||
| 92 | /******************************************* |
|
| 93 | * RECORD |
|
| 94 | *******************************************/ |
|
| 95 | ||
| 96 | /** |
|
| 97 | * @param BaseModel $model |
|
| 98 | * @return Record|null |
|
| 99 | */ |
|
| 100 | public function findRecordByModel(BaseModel $model) |
|
| 101 | { |
|
| 102 | ||
| 103 | $stringValue = $this->stringValue($model); |
|
| 104 | ||
| 105 | if ($stringValue === null) { |
|
| 106 | return null; |
|
| 107 | } |
|
| 108 | ||
| 109 | return $this->findRecordByString($stringValue); |
|
| 110 | ||
| 111 | } |
|
| 112 | ||
| 113 | /** |
|
| 114 | * @inheritdoc |
|
| 115 | */ |
|
| 116 | public function toRecord(BaseModel $model, bool $mirrorScenario = true): Record |
|
| 117 | { |
|
| 118 | ||
| 119 | if ($record = $this->toRecordByString($model, $mirrorScenario)) { |
|
| 120 | return $record; |
|
| 121 | } |
|
| 122 | ||
| 123 | return parent::toRecord($model, $mirrorScenario); |
|
| 124 | ||
| 125 | } |
|
| 126 | ||
| 127 | } |
|
| 128 | ||
| @@ 18-127 (lines=110) @@ | ||
| 15 | * @author Flipbox Factory <[email protected]> |
|
| 16 | * @since 2.0.0 |
|
| 17 | */ |
|
| 18 | abstract class ModelByString extends Model |
|
| 19 | { |
|
| 20 | ||
| 21 | use traits\ModelByString; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @inheritdoc |
|
| 25 | */ |
|
| 26 | public function find($identifier, string $toScenario = null) |
|
| 27 | { |
|
| 28 | ||
| 29 | if ($model = parent::find($identifier, $toScenario)) { |
|
| 30 | return $model; |
|
| 31 | } |
|
| 32 | ||
| 33 | if (!is_string($identifier)) { |
|
| 34 | return null; |
|
| 35 | } |
|
| 36 | ||
| 37 | return $this->findByString($identifier, $toScenario); |
|
| 38 | ||
| 39 | } |
|
| 40 | ||
| 41 | /******************************************* |
|
| 42 | * CACHE |
|
| 43 | *******************************************/ |
|
| 44 | ||
| 45 | /** |
|
| 46 | * @inheritdoc |
|
| 47 | */ |
|
| 48 | public function findCache($identifier) |
|
| 49 | { |
|
| 50 | ||
| 51 | if ($model = parent::findCache($identifier)) { |
|
| 52 | return $model; |
|
| 53 | } |
|
| 54 | ||
| 55 | if (!is_string($identifier)) { |
|
| 56 | return null; |
|
| 57 | } |
|
| 58 | ||
| 59 | return $this->findCacheByString($identifier); |
|
| 60 | ||
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * @inheritdoc |
|
| 65 | */ |
|
| 66 | public function addToCache(BaseModel $model) |
|
| 67 | { |
|
| 68 | ||
| 69 | parent::addToCache($model); |
|
| 70 | ||
| 71 | $this->cacheByString($model); |
|
| 72 | ||
| 73 | return $this; |
|
| 74 | ||
| 75 | } |
|
| 76 | ||
| 77 | ||
| 78 | /** |
|
| 79 | * @inheritdoc |
|
| 80 | */ |
|
| 81 | public function findCacheByRecord(Record $record) |
|
| 82 | { |
|
| 83 | ||
| 84 | if ($model = parent::findCacheByRecord($record)) { |
|
| 85 | return $model; |
|
| 86 | } |
|
| 87 | ||
| 88 | return $this->findCacheByRecordByString($record); |
|
| 89 | ||
| 90 | } |
|
| 91 | ||
| 92 | /******************************************* |
|
| 93 | * RECORD |
|
| 94 | *******************************************/ |
|
| 95 | ||
| 96 | /** |
|
| 97 | * @param BaseModel $model |
|
| 98 | * @return Record|null |
|
| 99 | */ |
|
| 100 | public function findRecordByModel(BaseModel $model) |
|
| 101 | { |
|
| 102 | ||
| 103 | $stringValue = $this->stringValue($model); |
|
| 104 | ||
| 105 | if ($stringValue === null) { |
|
| 106 | return null; |
|
| 107 | } |
|
| 108 | ||
| 109 | return $this->findRecordByString($stringValue); |
|
| 110 | ||
| 111 | } |
|
| 112 | ||
| 113 | /** |
|
| 114 | * @inheritdoc |
|
| 115 | */ |
|
| 116 | public function toRecord(BaseModel $model, bool $mirrorScenario = true): Record |
|
| 117 | { |
|
| 118 | ||
| 119 | if ($record = $this->toRecordByString($model, $mirrorScenario)) { |
|
| 120 | return $record; |
|
| 121 | } |
|
| 122 | ||
| 123 | return parent::toRecord($model, $mirrorScenario); |
|
| 124 | ||
| 125 | } |
|
| 126 | ||
| 127 | } |
|
| 128 | ||