From 176de4111c40afbff3bbad346d497c493f43c731 Mon Sep 17 00:00:00 2001 From: Stefan Ninic Date: Tue, 14 Jul 2020 14:02:36 +0200 Subject: [PATCH] Support Laravel 6 and 7 --- composer.json | 8 +-- src/Builders/Builder.php | 3 +- src/Utils/Model.php | 130 +++++++++++++++++++-------------------- 3 files changed, 69 insertions(+), 72 deletions(-) diff --git a/composer.json b/composer.json index cff2c6e..dce1a41 100644 --- a/composer.json +++ b/composer.json @@ -10,10 +10,10 @@ } ], "require": { - "php": ">=7.1", - "laravel/framework": "^5.5", - "ext-json": "*", - "guzzlehttp/guzzle": "*" + "php": ">=7.1", + "laravel/framework": "^5.5|^6|^7", + "ext-json": "*", + "guzzlehttp/guzzle": "*" }, "autoload": { "psr-4": { diff --git a/src/Builders/Builder.php b/src/Builders/Builder.php index 1150c39..1dc201d 100644 --- a/src/Builders/Builder.php +++ b/src/Builders/Builder.php @@ -9,6 +9,7 @@ namespace KgBot\Magento\Builders; use Illuminate\Support\Collection; +use Illuminate\Support\Str; use KgBot\Magento\Exceptions\MagentoClientException; use KgBot\Magento\Exceptions\MagentoRequestException; use KgBot\Magento\Utils\Model; @@ -98,7 +99,7 @@ public function find( $id ) public function create( $data ) { $data = [ - str_singular( $this->entity ) => $data, + Str::singular( $this->entity ) => $data, ]; return $this->request->handleWithExceptions( function () use ( $data ) { diff --git a/src/Utils/Model.php b/src/Utils/Model.php index 90c0aba..b717bd9 100644 --- a/src/Utils/Model.php +++ b/src/Utils/Model.php @@ -9,96 +9,92 @@ namespace KgBot\Magento\Utils; +use Illuminate\Support\Str; +use ReflectionObject; +use ReflectionProperty; + class Model { - protected $entity; - protected $primaryKey; - protected $modelClass = self::class; - protected $fillable = []; + protected $entity; + protected $primaryKey; + protected $modelClass = self::class; + protected $fillable = []; - /** - * @var Request - */ - protected $request; + /** + * @var Request + */ + protected $request; - public function __construct( Request $request, $data = [] ) - { - $this->request = $request; - $data = (array) $data; + public function __construct( Request $request, $data = [] ) { + $this->request = $request; + $data = (array) $data; - foreach ( $data as $key => $value ) { + foreach ( $data as $key => $value ) { - $customSetterMethod = 'set' . ucfirst( camel_case( $key ) ) . 'Attribute'; + $customSetterMethod = 'set' . ucfirst( Str::camel( $key ) ) . 'Attribute'; - if ( !method_exists( $this, $customSetterMethod ) ) { + if ( !method_exists( $this, $customSetterMethod ) ) { - $this->setAttribute( $key, $value ); + $this->setAttribute( $key, $value ); - } else { + } else { - $this->setAttribute( $key, $this->{$customSetterMethod}( $value ) ); - } - } - } + $this->setAttribute( $key, $this->{$customSetterMethod}( $value ) ); + } + } + } - protected function setAttribute( $attribute, $value ) - { - $this->{$attribute} = $value; - } + protected function setAttribute( $attribute, $value ) { + $this->{$attribute} = $value; + } - public function __toString() - { - return json_encode( $this->toArray() ); - } + public function __toString() { + return json_encode( $this->toArray() ); + } - public function toArray() - { - $data = []; - $class = new \ReflectionObject( $this ); - $properties = $class->getProperties( \ReflectionProperty::IS_PUBLIC ); + public function toArray() { + $data = []; + $class = new ReflectionObject( $this ); + $properties = $class->getProperties( ReflectionProperty::IS_PUBLIC ); - /** @var \ReflectionProperty $property */ - foreach ( $properties as $property ) { + /** @var ReflectionProperty $property */ + foreach ( $properties as $property ) { - $data[ $property->getName() ] = $this->{$property->getName()}; - } + $data[ $property->getName() ] = $this->{$property->getName()}; + } - return $data; - } + return $data; + } - public function delete() - { - return $this->request->handleWithExceptions( function () { + public function delete() { + return $this->request->handleWithExceptions( function () { - return $this->request->client->delete( "{$this->entity}/" . urlencode( $this->{$this->primaryKey} ) ); - } ); - } + return $this->request->client->delete( "{$this->entity}/" . urlencode( $this->{$this->primaryKey} ) ); + } ); + } - public function update( $data = [] ) - { - $data = [ - str_singular( $this->entity ) => $data, - ]; + public function update( $data = [] ) { + $data = [ + Str::singular( $this->entity ) => $data, + ]; - return $this->request->handleWithExceptions( function () use ( $data ) { + return $this->request->handleWithExceptions( function () use ( $data ) { - $response = $this->request->client->put( "{$this->entity}/" . urlencode( $this->{$this->primaryKey} ), [ - 'json' => $data, - ] ); + $response = $this->request->client->put( "{$this->entity}/" . urlencode( $this->{$this->primaryKey} ), [ + 'json' => $data, + ] ); - $responseData = json_decode( (string) $response->getBody() ); + $responseData = json_decode( (string) $response->getBody() ); - return new $this->modelClass( $this->request, $responseData ); - } ); - } + return new $this->modelClass( $this->request, $responseData ); + } ); + } - public function getEntity() - { - return $this->entity; - } + public function getEntity() { + return $this->entity; + } - public function setEntity( $new_entity ) - { - $this->entity = $new_entity; - } + public function setEntity( $new_entity ) { + $this->entity = $new_entity; + } } \ No newline at end of file