9
9
namespace KgBot \Magento \Utils ;
10
10
11
11
12
+ use Illuminate \Support \Str ;
13
+ use ReflectionObject ;
14
+ use ReflectionProperty ;
15
+
12
16
class Model
13
17
{
14
- protected $ entity ;
15
- protected $ primaryKey ;
16
- protected $ modelClass = self ::class;
17
- protected $ fillable = [];
18
+ protected $ entity ;
19
+ protected $ primaryKey ;
20
+ protected $ modelClass = self ::class;
21
+ protected $ fillable = [];
18
22
19
- /**
20
- * @var Request
21
- */
22
- protected $ request ;
23
+ /**
24
+ * @var Request
25
+ */
26
+ protected $ request ;
23
27
24
- public function __construct ( Request $ request , $ data = [] )
25
- {
26
- $ this ->request = $ request ;
27
- $ data = (array ) $ data ;
28
+ public function __construct ( Request $ request , $ data = [] ) {
29
+ $ this ->request = $ request ;
30
+ $ data = (array ) $ data ;
28
31
29
- foreach ( $ data as $ key => $ value ) {
32
+ foreach ( $ data as $ key => $ value ) {
30
33
31
- $ customSetterMethod = 'set ' . ucfirst ( camel_case ( $ key ) ) . 'Attribute ' ;
34
+ $ customSetterMethod = 'set ' . ucfirst ( Str:: camel ( $ key ) ) . 'Attribute ' ;
32
35
33
- if ( !method_exists ( $ this , $ customSetterMethod ) ) {
36
+ if ( !method_exists ( $ this , $ customSetterMethod ) ) {
34
37
35
- $ this ->setAttribute ( $ key , $ value );
38
+ $ this ->setAttribute ( $ key , $ value );
36
39
37
- } else {
40
+ } else {
38
41
39
- $ this ->setAttribute ( $ key , $ this ->{$ customSetterMethod }( $ value ) );
40
- }
41
- }
42
- }
42
+ $ this ->setAttribute ( $ key , $ this ->{$ customSetterMethod }( $ value ) );
43
+ }
44
+ }
45
+ }
43
46
44
- protected function setAttribute ( $ attribute , $ value )
45
- {
46
- $ this ->{$ attribute } = $ value ;
47
- }
47
+ protected function setAttribute ( $ attribute , $ value ) {
48
+ $ this ->{$ attribute } = $ value ;
49
+ }
48
50
49
- public function __toString ()
50
- {
51
- return json_encode ( $ this ->toArray () );
52
- }
51
+ public function __toString () {
52
+ return json_encode ( $ this ->toArray () );
53
+ }
53
54
54
- public function toArray ()
55
- {
56
- $ data = [];
57
- $ class = new \ReflectionObject ( $ this );
58
- $ properties = $ class ->getProperties ( \ReflectionProperty::IS_PUBLIC );
55
+ public function toArray () {
56
+ $ data = [];
57
+ $ class = new ReflectionObject ( $ this );
58
+ $ properties = $ class ->getProperties ( ReflectionProperty::IS_PUBLIC );
59
59
60
- /** @var \ ReflectionProperty $property */
61
- foreach ( $ properties as $ property ) {
60
+ /** @var ReflectionProperty $property */
61
+ foreach ( $ properties as $ property ) {
62
62
63
- $ data [ $ property ->getName () ] = $ this ->{$ property ->getName ()};
64
- }
63
+ $ data [ $ property ->getName () ] = $ this ->{$ property ->getName ()};
64
+ }
65
65
66
- return $ data ;
67
- }
66
+ return $ data ;
67
+ }
68
68
69
- public function delete ()
70
- {
71
- return $ this ->request ->handleWithExceptions ( function () {
69
+ public function delete () {
70
+ return $ this ->request ->handleWithExceptions ( function () {
72
71
73
- return $ this ->request ->client ->delete ( "{$ this ->entity }/ " . urlencode ( $ this ->{$ this ->primaryKey } ) );
74
- } );
75
- }
72
+ return $ this ->request ->client ->delete ( "{$ this ->entity }/ " . urlencode ( $ this ->{$ this ->primaryKey } ) );
73
+ } );
74
+ }
76
75
77
- public function update ( $ data = [] )
78
- {
79
- $ data = [
80
- str_singular ( $ this ->entity ) => $ data ,
81
- ];
76
+ public function update ( $ data = [] ) {
77
+ $ data = [
78
+ Str::singular ( $ this ->entity ) => $ data ,
79
+ ];
82
80
83
- return $ this ->request ->handleWithExceptions ( function () use ( $ data ) {
81
+ return $ this ->request ->handleWithExceptions ( function () use ( $ data ) {
84
82
85
- $ response = $ this ->request ->client ->put ( "{$ this ->entity }/ " . urlencode ( $ this ->{$ this ->primaryKey } ), [
86
- 'json ' => $ data ,
87
- ] );
83
+ $ response = $ this ->request ->client ->put ( "{$ this ->entity }/ " . urlencode ( $ this ->{$ this ->primaryKey } ), [
84
+ 'json ' => $ data ,
85
+ ] );
88
86
89
- $ responseData = json_decode ( (string ) $ response ->getBody () );
87
+ $ responseData = json_decode ( (string ) $ response ->getBody () );
90
88
91
- return new $ this ->modelClass ( $ this ->request , $ responseData );
92
- } );
93
- }
89
+ return new $ this ->modelClass ( $ this ->request , $ responseData );
90
+ } );
91
+ }
94
92
95
- public function getEntity ()
96
- {
97
- return $ this ->entity ;
98
- }
93
+ public function getEntity () {
94
+ return $ this ->entity ;
95
+ }
99
96
100
- public function setEntity ( $ new_entity )
101
- {
102
- $ this ->entity = $ new_entity ;
103
- }
97
+ public function setEntity ( $ new_entity ) {
98
+ $ this ->entity = $ new_entity ;
99
+ }
104
100
}
0 commit comments