diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88e99d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +composer.lock \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..c4f28c0 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2016 Lasse Rafn + +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a705e9f --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "lasserafn/laravel-economic", + "description": "Economic REST wrapper for Laravel", + "keywords": ["laravel", "wrapper", "economic", "api", "integration", "e-conomic"], + "license": "MIT", + "authors": [ + { + "name": "Lasse Rafn", + "email": "lasserafn@gmail.com" + } + ], + "require": { + "php": ">=5.4.0", + "illuminate/support": "5.1.*|5.2.*|5.3.*", + "guzzlehttp/guzzle": "^6.2" + }, + "require-dev": { + }, + "autoload": { + "psr-4": { + "LasseRafn\\Economic\\": "src" + } + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..c182a08 --- /dev/null +++ b/readme.md @@ -0,0 +1,15 @@ +## Laravel Economic REST wrapper + +#Installation + +1. Require using composer +```` +composer require lasserafn/laravel-economic +```` + +2. Add the EconomicServiceProvider to your ````config/app.php```` providers array. +```` +'providers' => [ + \LasseRafn\Economic\EconomicServiceProvider::class, +] +```` \ No newline at end of file diff --git a/src/Builders/Builder.php b/src/Builders/Builder.php new file mode 100644 index 0000000..beb82bd --- /dev/null +++ b/src/Builders/Builder.php @@ -0,0 +1,123 @@ +request = $request; + } + + /** + * @param $id + * + * @return mixed|Model + */ + public function find($id) + {//todo test + $response = $this->request->curl->get( "/{$this->entity}/{$id}" ); + + // todo check for errors and such + + $responseData = json_decode( $response->getBody()->getContents() ); + + return new $this->model($this->request, $responseData); + } + + public function first() + { + $response = $this->request->curl->get( "/{$this->entity}?skippages=0&pagesize=1" ); + + // todo check for errors and such + + $responseData = json_decode( $response->getBody()->getContents() ); + $fetchedItems = $responseData->collection; + + return new $this->model($this->request, $fetchedItems[0]); + } + + /** + * @return \Illuminate\Support\Collection|Model[] + */ + public function get() + { + $response = $this->request->curl->get( "/{$this->entity}" ); + + // todo check for errors and such + + $responseData = json_decode( $response->getBody()->getContents() ); + $fetchedItems = $responseData->collection; + + $items = collect( [] ); + foreach ( $fetchedItems as $item ) + { + /** @var Model $model */ + $model = new $this->model( $this->request, $item ); + + $items->push( $model ); + } + + return $items; + } + + /** + * @return \Illuminate\Support\Collection|Model[] + */ + public function all() + { + $page = 0; + $pagesize = 500; // Yes, we could move this to 1000, but honestly I'd rather send two requests than stall their servers. + $hasMore = true; + $items = collect( [] ); + + while($hasMore) + { + $response = $this->request->curl->get( "/{$this->entity}?skippages={$page}&pagesize={$pagesize}" ); + + // todo check for errors and such + + $responseData = json_decode( $response->getBody()->getContents() ); + $fetchedItems = $responseData->collection; + + if( count($fetchedItems) == 0) + { + $hasMore = false; + + break; + } + + foreach ( $fetchedItems as $item ) + { + /** @var Model $model */ + $model = new $this->model( $this->request, $item ); + + $items->push( $model ); + } + + $page++; + } + + return $items; + } + + public function create( $data) + { + $response = $this->request->curl->post( "/{$this->entity}", [ + 'json' => $data + ] ); + + $responseData = json_decode( $response->getBody()->getContents() ); + + return new $this->model( $this->request, $responseData ); + } + + +} \ No newline at end of file diff --git a/src/Builders/ContactBuilder.php b/src/Builders/ContactBuilder.php new file mode 100644 index 0000000..b7b6232 --- /dev/null +++ b/src/Builders/ContactBuilder.php @@ -0,0 +1,17 @@ +entity = str_replace(':customerNumber', $customerNumber, $this->entity); + + parent::__construct( $request ); + } +} \ No newline at end of file diff --git a/src/Builders/CustomerBuilder.php b/src/Builders/CustomerBuilder.php new file mode 100644 index 0000000..1bf6724 --- /dev/null +++ b/src/Builders/CustomerBuilder.php @@ -0,0 +1,9 @@ +request = new Request($agreement); + } + + public function getAuthUrl() + { + return config('economic.auth_endpoint') . config('economic.public_token'); + } + + /** + * @return CustomerBuilder|Builder + */ + public function customers() + { + return new CustomerBuilder($this->request); + } + + /** + * @return CustomerGroupBuilder|Builder + */ + public function customersGroups() + { + return new CustomerGroupBuilder($this->request); + } + + /** + * @return LayoutBuilder|Builder + */ + public function layouts() + { + return new LayoutBuilder($this->request); + } + + /** + * @return VatZoneBuilder|Builder + */ + public function vatZones() + { + return new VatZoneBuilder($this->request); + } + + /** + * @return VatZoneBuilder|Builder + */ + public function paymentTerms() + { + return new PaymentTermBuilder($this->request); + } +} \ No newline at end of file diff --git a/src/EconomicServiceProvider.php b/src/EconomicServiceProvider.php new file mode 100644 index 0000000..c044240 --- /dev/null +++ b/src/EconomicServiceProvider.php @@ -0,0 +1,25 @@ +mergeConfigFrom($configPath, 'economic'); + + $configPath = __DIR__ . '/config/economic.php'; + + if (function_exists('config_path')) { + $publishPath = config_path('economic.php'); + } else { + $publishPath = base_path('config/economic.php'); + } + + $this->publishes([$configPath => $publishPath], 'config'); + } +} \ No newline at end of file diff --git a/src/Models/Contact.php b/src/Models/Contact.php new file mode 100644 index 0000000..5db1e0f --- /dev/null +++ b/src/Models/Contact.php @@ -0,0 +1,35 @@ +entity = str_replace(':customerNumber', $data['customer']->customerNumber, $this->entity); + + parent::__construct( $request, $data ); + } +} \ No newline at end of file diff --git a/src/Models/Customer.php b/src/Models/Customer.php new file mode 100644 index 0000000..c6313e1 --- /dev/null +++ b/src/Models/Customer.php @@ -0,0 +1,75 @@ +request, $this->customerNumber); + } +} \ No newline at end of file diff --git a/src/Models/CustomerGroup.php b/src/Models/CustomerGroup.php new file mode 100644 index 0000000..3596322 --- /dev/null +++ b/src/Models/CustomerGroup.php @@ -0,0 +1,14 @@ +request = $request; + + foreach ( $this->fillable as $fillable ) + { + if ( is_array($data) && isset( $data[ $fillable ] ) ) + { + if(!method_exists($this, 'set' . ucfirst(camel_case($fillable)) . 'Attribute' )) + { + $this->setAttribute($fillable, $data[$fillable]); + } + else + { + $this->setAttribute($fillable, $this->{'set' . ucfirst(camel_case($fillable)) . 'Attribute'}($data[$fillable])); + } + } + elseif(is_object($data) && isset( $data->{$fillable} )) + { + if(!method_exists($this, 'set' . ucfirst(camel_case($fillable)) . 'Attribute' )) + { + $this->setAttribute($fillable, $data->{$fillable}); + } + else + { + $this->setAttribute($fillable, $this->{'set' . ucfirst(camel_case($fillable)) . 'Attribute'}($data->{$fillable})); + } + } + } + } + + function __toString() + { + return json_encode($this->toArray()); + } + + function toArray() + { + $data = []; + + foreach($this->fillable as $fillable) + { + if( isset($this->{$fillable})) + { + $data[$fillable] = $this->{$fillable}; + } + } + + return $data; + } + + protected function setAttribute( $attribute, $value ) + { + $this->{$attribute} = $value; + } + + public function delete() + { + return $this->request->curl->delete("/{$this->entity}/{$this->{$this->primaryKey}}"); + } +} \ No newline at end of file diff --git a/src/Utils/Request.php b/src/Utils/Request.php new file mode 100644 index 0000000..03f26d8 --- /dev/null +++ b/src/Utils/Request.php @@ -0,0 +1,37 @@ +curl = new Client( [ + 'base_uri' => config('economic.request_endpoint'), + 'headers' => [ + 'X-AppSecretToken' => config('economic.secret_token'), + 'X-AgreementGrantToken' => $agreementToken, + 'Content-Type' => 'application/json' + ] + ] ); + } + + + function lol() + { + // fixme! + try + { + $url = config( 'pipedrive.endpoint' ) . $this->buildEntity( $entity, $id, $fields ) . '?api_token=' . $this-$this->api_token . '&start=' . $start . '&limit=' . $limit; + $response = $this->curl->get( $url ); + + return $this->getData( $response->getBody() ); + } catch ( \Exception $exception ) + { + throw new \Exception( $exception->getMessage(), $exception->getCode() ); + } + } +} \ No newline at end of file diff --git a/src/config/economic.php b/src/config/economic.php new file mode 100644 index 0000000..e86ea6b --- /dev/null +++ b/src/config/economic.php @@ -0,0 +1,7 @@ + 'https://secure.e-conomic.com/secure/api1/requestaccess.aspx?appPublicToken=', + 'request_endpoint' => 'https://restapi.e-conomic.com', + 'public_token' => env( 'ECONOMIC_APP_PUBLIC' ), + 'secret_token' => env( 'ECONOMIC_APP_SECRET' ) +]; \ No newline at end of file