Skip to content

Commit 176de41

Browse files
committedJul 14, 2020
Support Laravel 6 and 7
1 parent a26ca1b commit 176de41

File tree

3 files changed

+69
-72
lines changed

3 files changed

+69
-72
lines changed
 

‎composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=7.1",
14-
"laravel/framework": "^5.5",
15-
"ext-json": "*",
16-
"guzzlehttp/guzzle": "*"
13+
"php": ">=7.1",
14+
"laravel/framework": "^5.5|^6|^7",
15+
"ext-json": "*",
16+
"guzzlehttp/guzzle": "*"
1717
},
1818
"autoload": {
1919
"psr-4": {

‎src/Builders/Builder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace KgBot\Magento\Builders;
1010

1111
use Illuminate\Support\Collection;
12+
use Illuminate\Support\Str;
1213
use KgBot\Magento\Exceptions\MagentoClientException;
1314
use KgBot\Magento\Exceptions\MagentoRequestException;
1415
use KgBot\Magento\Utils\Model;
@@ -98,7 +99,7 @@ public function find( $id )
9899
public function create( $data )
99100
{
100101
$data = [
101-
str_singular( $this->entity ) => $data,
102+
Str::singular( $this->entity ) => $data,
102103
];
103104

104105
return $this->request->handleWithExceptions( function () use ( $data ) {

‎src/Utils/Model.php

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,96 +9,92 @@
99
namespace KgBot\Magento\Utils;
1010

1111

12+
use Illuminate\Support\Str;
13+
use ReflectionObject;
14+
use ReflectionProperty;
15+
1216
class Model
1317
{
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 = [];
1822

19-
/**
20-
* @var Request
21-
*/
22-
protected $request;
23+
/**
24+
* @var Request
25+
*/
26+
protected $request;
2327

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;
2831

29-
foreach ( $data as $key => $value ) {
32+
foreach ( $data as $key => $value ) {
3033

31-
$customSetterMethod = 'set' . ucfirst( camel_case( $key ) ) . 'Attribute';
34+
$customSetterMethod = 'set' . ucfirst( Str::camel( $key ) ) . 'Attribute';
3235

33-
if ( !method_exists( $this, $customSetterMethod ) ) {
36+
if ( !method_exists( $this, $customSetterMethod ) ) {
3437

35-
$this->setAttribute( $key, $value );
38+
$this->setAttribute( $key, $value );
3639

37-
} else {
40+
} else {
3841

39-
$this->setAttribute( $key, $this->{$customSetterMethod}( $value ) );
40-
}
41-
}
42-
}
42+
$this->setAttribute( $key, $this->{$customSetterMethod}( $value ) );
43+
}
44+
}
45+
}
4346

44-
protected function setAttribute( $attribute, $value )
45-
{
46-
$this->{$attribute} = $value;
47-
}
47+
protected function setAttribute( $attribute, $value ) {
48+
$this->{$attribute} = $value;
49+
}
4850

49-
public function __toString()
50-
{
51-
return json_encode( $this->toArray() );
52-
}
51+
public function __toString() {
52+
return json_encode( $this->toArray() );
53+
}
5354

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 );
5959

60-
/** @var \ReflectionProperty $property */
61-
foreach ( $properties as $property ) {
60+
/** @var ReflectionProperty $property */
61+
foreach ( $properties as $property ) {
6262

63-
$data[ $property->getName() ] = $this->{$property->getName()};
64-
}
63+
$data[ $property->getName() ] = $this->{$property->getName()};
64+
}
6565

66-
return $data;
67-
}
66+
return $data;
67+
}
6868

69-
public function delete()
70-
{
71-
return $this->request->handleWithExceptions( function () {
69+
public function delete() {
70+
return $this->request->handleWithExceptions( function () {
7271

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+
}
7675

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+
];
8280

83-
return $this->request->handleWithExceptions( function () use ( $data ) {
81+
return $this->request->handleWithExceptions( function () use ( $data ) {
8482

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+
] );
8886

89-
$responseData = json_decode( (string) $response->getBody() );
87+
$responseData = json_decode( (string) $response->getBody() );
9088

91-
return new $this->modelClass( $this->request, $responseData );
92-
} );
93-
}
89+
return new $this->modelClass( $this->request, $responseData );
90+
} );
91+
}
9492

95-
public function getEntity()
96-
{
97-
return $this->entity;
98-
}
93+
public function getEntity() {
94+
return $this->entity;
95+
}
9996

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+
}
104100
}

0 commit comments

Comments
 (0)