5
5
*/
6
6
namespace Magento \Framework \Model ;
7
7
8
+ use Exception ;
8
9
use Laminas \Validator \ValidatorChain ;
9
10
use Laminas \Validator \ValidatorInterface ;
11
+ use Magento \Framework \App \CacheInterface ;
12
+ use Magento \Framework \App \ObjectManager ;
13
+ use Magento \Framework \App \State ;
10
14
use Magento \Framework \DataObject ;
15
+ use Magento \Framework \Event \ManagerInterface ;
11
16
use Magento \Framework \Exception \LocalizedException ;
17
+ use Magento \Framework \Message \Error ;
18
+ use Magento \Framework \Model \ActionValidator \RemoveAction ;
19
+ use Magento \Framework \Model \ResourceModel \AbstractResource ;
20
+ use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
21
+ use Magento \Framework \Data \Collection \AbstractDb as AbstractDbCollection ;
22
+ use Magento \Framework \Model \ResourceModel \Db \Collection \AbstractCollection ;
12
23
use Magento \Framework \Phrase ;
24
+ use Magento \Framework \Registry ;
25
+ use Psr \Log \LoggerInterface ;
13
26
14
27
/**
15
28
* Abstract model class
@@ -71,12 +84,12 @@ abstract class AbstractModel extends DataObject
71
84
/**
72
85
* Resource model instance
73
86
*
74
- * @var \Magento\Framework\Model\ResourceModel\Db\ AbstractDb
87
+ * @var AbstractDb
75
88
*/
76
89
protected $ _resource ;
77
90
78
91
/**
79
- * @var \Magento\Framework\Model\ResourceModel\Db\Collection\ AbstractCollection
92
+ * @var AbstractCollection
80
93
*/
81
94
protected $ _resourceCollection ;
82
95
@@ -129,34 +142,34 @@ abstract class AbstractModel extends DataObject
129
142
/**
130
143
* Application Event Dispatcher
131
144
*
132
- * @var \Magento\Framework\Event\ ManagerInterface
145
+ * @var ManagerInterface
133
146
*/
134
147
protected $ _eventManager ;
135
148
136
149
/**
137
150
* Application Cache Manager
138
151
*
139
- * @var \Magento\Framework\App\ CacheInterface
152
+ * @var CacheInterface
140
153
*/
141
154
protected $ _cacheManager ;
142
155
143
156
/**
144
- * @var \Magento\Framework\ Registry
157
+ * @var Registry
145
158
*/
146
159
protected $ _registry ;
147
160
148
161
/**
149
- * @var \Psr\Log\ LoggerInterface
162
+ * @var LoggerInterface
150
163
*/
151
164
protected $ _logger ;
152
165
153
166
/**
154
- * @var \Magento\Framework\App\ State
167
+ * @var State
155
168
*/
156
169
protected $ _appState ;
157
170
158
171
/**
159
- * @var \Magento\Framework\Model\ActionValidator\ RemoveAction
172
+ * @var RemoveAction
160
173
*/
161
174
protected $ _actionValidator ;
162
175
@@ -168,18 +181,19 @@ abstract class AbstractModel extends DataObject
168
181
protected $ storedData = [];
169
182
170
183
/**
171
- * @param \Magento\Framework\Model\ Context $context
172
- * @param \Magento\Framework\ Registry $registry
173
- * @param \Magento\Framework\Model\ResourceModel\ AbstractResource $resource
174
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
184
+ * @param Context $context
185
+ * @param Registry $registry
186
+ * @param AbstractResource|null $resource
187
+ * @param AbstractDbCollection|null $resourceCollection
175
188
* @param array $data
189
+ * @throws LocalizedException
176
190
*/
177
191
public function __construct (
178
- \ Magento \ Framework \ Model \ Context $ context ,
179
- \ Magento \ Framework \ Registry $ registry ,
180
- \ Magento \ Framework \ Model \ ResourceModel \ AbstractResource $ resource = null ,
181
- \ Magento \ Framework \ Data \ Collection \ AbstractDb $ resourceCollection = null ,
182
- array $ data = []
192
+ Context $ context ,
193
+ Registry $ registry ,
194
+ AbstractResource $ resource = null ,
195
+ AbstractDbCollection $ resourceCollection = null ,
196
+ array $ data = []
183
197
) {
184
198
$ this ->_registry = $ registry ;
185
199
$ this ->_appState = $ context ->getAppState ();
@@ -214,6 +228,7 @@ protected function _construct() //phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
214
228
*
215
229
* @param string $resourceModel
216
230
* @return void
231
+ * @throws LocalizedException
217
232
*/
218
233
protected function _init ($ resourceModel )
219
234
{
@@ -252,11 +267,11 @@ public function __sleep()
252
267
*/
253
268
public function __wakeup ()
254
269
{
255
- $ objectManager = \ Magento \ Framework \ App \ ObjectManager::getInstance ();
256
- $ this ->_registry = $ objectManager ->get (\ Magento \ Framework \ Registry::class);
270
+ $ objectManager = ObjectManager::getInstance ();
271
+ $ this ->_registry = $ objectManager ->get (Registry::class);
257
272
258
- $ context = $ objectManager ->get (\ Magento \ Framework \ Model \ Context::class);
259
- if ($ context instanceof \ Magento \ Framework \ Model \ Context) {
273
+ $ context = $ objectManager ->get (Context::class);
274
+ if ($ context instanceof Context) {
260
275
$ this ->_appState = $ context ->getAppState ();
261
276
$ this ->_eventManager = $ context ->getEventDispatcher ();
262
277
$ this ->_cacheManager = $ context ->getCacheManager ();
@@ -357,6 +372,7 @@ public function setData($key, $value = null)
357
372
}
358
373
$ this ->_data = $ key ;
359
374
} else {
375
+ $ this ->checkAndConvertNumericValue ($ key , $ value );
360
376
if (!array_key_exists ($ key , $ this ->_data ) || $ this ->_data [$ key ] !== $ value ) {
361
377
$ this ->_hasDataChanges = true ;
362
378
}
@@ -470,19 +486,19 @@ protected function _setResourceModel($resourceName, $collectionName = null)
470
486
/**
471
487
* Get resource instance
472
488
*
473
- * @throws \Magento\Framework\Exception\LocalizedException
474
- * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
489
+ * @return AbstractDb
490
+ * @throws LocalizedException
475
491
* @deprecated 101.0.0 because resource models should be used directly
476
492
* @see we don't recommend this approach anymore
477
493
*/
478
494
protected function _getResource ()
479
495
{
480
496
if (empty ($ this ->_resourceName ) && empty ($ this ->_resource )) {
481
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
482
- new \ Magento \ Framework \ Phrase ('The resource isn \'t set. ' )
497
+ throw new LocalizedException (
498
+ new Phrase ('The resource isn \'t set. ' )
483
499
);
484
500
}
485
- return $ this ->_resource ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()->get ($ this ->_resourceName );
501
+ return $ this ->_resource ?: ObjectManager::getInstance ()->get ($ this ->_resourceName );
486
502
}
487
503
488
504
/**
@@ -499,20 +515,20 @@ public function getResourceName()
499
515
* Get collection instance
500
516
*
501
517
* @TODO MAGETWO-23541: Incorrect dependencies between Model\AbstractModel and Data\Collection\Db from Framework
502
- * @throws \Magento\Framework\Exception\ LocalizedException
503
- * @return \Magento\Framework\Model\ResourceModel\Db\Collection\ AbstractCollection
518
+ * @throws LocalizedException
519
+ * @return AbstractCollection
504
520
* @deprecated 101.0.0 because collections should be used directly via factory
505
521
* @see we don't recommend this approach anymore
506
522
*/
507
523
public function getResourceCollection ()
508
524
{
509
525
if (empty ($ this ->_resourceCollection ) && empty ($ this ->_collectionName )) {
510
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
511
- new \ Magento \ Framework \ Phrase ('Model collection resource name is not defined. ' )
526
+ throw new LocalizedException (
527
+ new Phrase ('Model collection resource name is not defined. ' )
512
528
);
513
529
}
514
530
return !$ this ->_collectionName ? clone $ this
515
- ->_resourceCollection : \ Magento \ Framework \ App \ ObjectManager::getInstance ()
531
+ ->_resourceCollection : ObjectManager::getInstance ()
516
532
->create (
517
533
$ this ->_collectionName
518
534
);
@@ -522,9 +538,10 @@ public function getResourceCollection()
522
538
* Retrieve collection instance
523
539
*
524
540
* @TODO MAGETWO-23541: Incorrect dependencies between Model\AbstractModel and Data\Collection\Db from Framework
525
- * @return \Magento\Framework\Model\ResourceModel\Db\Collection\ AbstractCollection
526
- * @deprecated 101.0.0 because collections should be used directly via factory
541
+ * @return AbstractCollection
542
+ * @throws LocalizedException
527
543
* @see we don't recommend this approach anymore
544
+ * @deprecated 101.0.0 because collections should be used directly via factory
528
545
*/
529
546
public function getCollection ()
530
547
{
@@ -537,10 +554,11 @@ public function getCollection()
537
554
* @param integer $modelId
538
555
* @param null|string $field
539
556
* @return $this
557
+ * @throws LocalizedException
558
+ * @see we don't recommend this approach anymore
540
559
* @deprecated 100.1.0 because entities must not be responsible for their own loading.
541
560
* Service contracts should persist entities. Use resource model "load" or collections to implement
542
561
* service contract model loading operations.
543
- * @see we don't recommend this approach anymore
544
562
*/
545
563
public function load ($ modelId , $ field = null )
546
564
{
@@ -651,7 +669,7 @@ public function setHasDataChanges($flag)
651
669
* Save object data
652
670
*
653
671
* @return $this
654
- * @throws \ Exception
672
+ * @throws Exception
655
673
*
656
674
* @deprecated 100.1.0 because entities must not be responsible for their own persistence.
657
675
* Service contracts should persist entities. Use resource model "save" to implement
@@ -726,7 +744,7 @@ public function validateBeforeSave()
726
744
new Phrase (implode (PHP_EOL , $ errors ))
727
745
);
728
746
foreach ($ errors as $ errorMessage ) {
729
- $ exception ->addMessage (new \ Magento \ Framework \ Message \ Error ($ errorMessage ));
747
+ $ exception ->addMessage (new Error ($ errorMessage ));
730
748
}
731
749
throw $ exception ;
732
750
}
@@ -739,6 +757,7 @@ public function validateBeforeSave()
739
757
* Returns FALSE, if no validation rules exist.
740
758
*
741
759
* @return ValidatorInterface|false
760
+ * @throws LocalizedException
742
761
*/
743
762
protected function _getValidatorBeforeSave ()
744
763
{
@@ -782,7 +801,7 @@ protected function _createValidatorBeforeSave()
782
801
*/
783
802
private function getValidator (): ValidatorChain
784
803
{
785
- return \ Magento \ Framework \ App \ ObjectManager::getInstance ()->create (ValidatorChain::class);
804
+ return ObjectManager::getInstance ()->create (ValidatorChain::class);
786
805
}
787
806
788
807
/**
@@ -852,7 +871,7 @@ public function afterSave()
852
871
* Delete object from database
853
872
*
854
873
* @return $this
855
- * @throws \ Exception
874
+ * @throws Exception
856
875
* @deprecated 100.1.0 because entities must not be responsible for their own deletion.
857
876
* Service contracts should delete entities. Use resource model "delete" method to implement
858
877
* service contract persistence operations.
@@ -868,13 +887,13 @@ public function delete()
868
887
* Processing object before delete data
869
888
*
870
889
* @return $this
871
- * @throws \Magento\Framework\Exception\ LocalizedException
890
+ * @throws LocalizedException
872
891
*/
873
892
public function beforeDelete ()
874
893
{
875
894
if (!$ this ->_actionValidator ->isAllowed ($ this )) {
876
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
877
- new \ Magento \ Framework \ Phrase ('Delete operation is forbidden for current area ' )
895
+ throw new LocalizedException (
896
+ new Phrase ('Delete operation is forbidden for current area ' )
878
897
);
879
898
}
880
899
@@ -913,7 +932,7 @@ public function afterDeleteCommit()
913
932
/**
914
933
* Retrieve model resource
915
934
*
916
- * @return \Magento\Framework\Model\ResourceModel\Db\ AbstractDb
935
+ * @return AbstractDb
917
936
* @deprecated 101.0.0 because resource models should be used directly
918
937
* @see we don't recommend this approach anymore
919
938
*/
@@ -1010,4 +1029,24 @@ public function getEventPrefix()
1010
1029
{
1011
1030
return $ this ->_eventPrefix ;
1012
1031
}
1032
+
1033
+ /**
1034
+ * Check and Convert Numeric Value for Proper Type Matching
1035
+ *
1036
+ * @param mixed $key
1037
+ * @param mixed $value
1038
+ * @return void
1039
+ */
1040
+ private function checkAndConvertNumericValue (mixed $ key , mixed $ value ): void
1041
+ {
1042
+ if (array_key_exists ($ key , $ this ->_data ) && is_numeric ($ this ->_data [$ key ])
1043
+ && $ value != null
1044
+ ) {
1045
+ if (is_int ($ value )) {
1046
+ $ this ->_data [$ key ] = (int ) $ this ->_data [$ key ];
1047
+ } elseif (is_float ($ value )) {
1048
+ $ this ->_data [$ key ] = (float ) $ this ->_data [$ key ];
1049
+ }
1050
+ }
1051
+ }
1013
1052
}
0 commit comments