From a753fbc738812b277cbe742eb9f38932ae74859c Mon Sep 17 00:00:00 2001 From: Stefan Ninic Date: Wed, 12 Feb 2020 11:27:38 +0100 Subject: [PATCH] url_friendly_id --- src/Models/Customer.php | 2 +- src/Models/CustomerInvoice.php | 2 +- src/Models/DraftOrder.php | 4 ++-- src/Models/DraftPurchaseOrder.php | 4 ++-- src/Models/Lot.php | 4 ++-- src/Models/Order.php | 12 ++++++------ src/Models/OrderShipment.php | 4 ++-- src/Models/Product.php | 10 +++++----- src/Models/ProductionOrder.php | 4 ++-- src/Models/PurchaseOrder.php | 10 +++++----- src/Models/Variation/Type.php | 4 ++-- src/Utils/Model.php | 9 +++++---- 12 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/Models/Customer.php b/src/Models/Customer.php index 077ba5f..6d76a08 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -22,7 +22,7 @@ public function contacts() { return $this->request->handleWithExceptions(function () { - $builder = new CustomerContactBuilder($this->request, $this->{$this->primaryKey}); + $builder = new CustomerContactBuilder($this->request, $this->{$this->url_friendly_id}); return $builder->get(); }); diff --git a/src/Models/CustomerInvoice.php b/src/Models/CustomerInvoice.php index f37817c..f79e2b4 100644 --- a/src/Models/CustomerInvoice.php +++ b/src/Models/CustomerInvoice.php @@ -57,7 +57,7 @@ public function book($send_email = false) ]; } - return $this->request->client->post("{$this->entity}/{$this->{$this->primaryKey}}/book" . $query, [ + return $this->request->client->post("{$this->entity}/{$this->{$this->url_friendly_id}}/book" . $query, [ 'json' => $body, ]) ->getBody() diff --git a/src/Models/DraftOrder.php b/src/Models/DraftOrder.php index 41111bd..19e6ca0 100644 --- a/src/Models/DraftOrder.php +++ b/src/Models/DraftOrder.php @@ -19,8 +19,8 @@ class DraftOrder extends Model public function getPDF() { return $this->request->handleWithExceptions( function () { - return $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}.pdf" )->getBody() - ->getContents(); + return $this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}.pdf")->getBody() + ->getContents(); } ); } } diff --git a/src/Models/DraftPurchaseOrder.php b/src/Models/DraftPurchaseOrder.php index f622ea3..f0da2cb 100644 --- a/src/Models/DraftPurchaseOrder.php +++ b/src/Models/DraftPurchaseOrder.php @@ -13,8 +13,8 @@ class DraftPurchaseOrder extends Model public function getPDF() { return $this->request->handleWithExceptions( function () { - return $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}.pdf" )->getBody() - ->getContents(); + return $this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}.pdf")->getBody() + ->getContents(); } ); } } diff --git a/src/Models/Lot.php b/src/Models/Lot.php index b00d1cd..e5f6e10 100644 --- a/src/Models/Lot.php +++ b/src/Models/Lot.php @@ -21,7 +21,7 @@ public function variations( $variation_id = 1001 ) { return $this->request->handleWithExceptions( function () use ( $variation_id ) { - $response = $this->request->client->get( "variations/{$this->{$this->primaryKey}}/variation-matrix" ); + $response = $this->request->client->get("variations/{$this->{$this->url_friendly_id}}/variation-matrix"); $html = (string) $response->getBody(); @@ -33,7 +33,7 @@ public function location() { return $this->request->handleWithExceptions( function () { - $response = $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}/locations" ); + $response = $this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}/locations"); return collect( json_decode( (string) $response->getBody() )->lot_locations ); diff --git a/src/Models/Order.php b/src/Models/Order.php index 70ff012..7ea4510 100644 --- a/src/Models/Order.php +++ b/src/Models/Order.php @@ -22,7 +22,7 @@ class Order extends Model public function getPDF() { return $this->request->handleWithExceptions( function () { - return $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}.pdf" )->getBody() + return $this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}.pdf")->getBody() ->getContents(); } ); } @@ -32,7 +32,7 @@ public function reopen() return $this->request->handleWithExceptions( function () { - return $this->request->client->post( "{$this->entity}/{$this->{$this->primaryKey}}/reopen" ) + return $this->request->client->post("{$this->entity}/{$this->{$this->url_friendly_id}}/reopen") ->getBody() ->getContents(); } ); @@ -42,7 +42,7 @@ public function createShipment() { return $this->request->handleWithExceptions(function () { - $response = json_decode((string)$this->request->client->post("{$this->entity}/{$this->{$this->primaryKey}}/create-shipment") + $response = json_decode((string)$this->request->client->post("{$this->entity}/{$this->{$this->url_friendly_id}}/create-shipment") ->getBody()); return new OrderShipment($this->request, $response->order_shipment); @@ -56,7 +56,7 @@ public function shipments() $builder = new OrderShipmentBuilder($this->request); return $builder->get([ - ['order_number', '=', $this->{$this->primaryKey}], + ['order_number', '=', $this->{$this->url_friendly_id}], ]); }); } @@ -71,7 +71,7 @@ public function convertToInvoice($book = false) { return $this->request->handleWithExceptions(function () use ($book) { - $response = json_decode((string)$this->request->client->post("{$this->entity}/{$this->{$this->primaryKey}}/convert-to-invoice?book=" . (($book === true) ? 'true' : 'false')) + $response = json_decode((string)$this->request->client->post("{$this->entity}/{$this->{$this->url_friendly_id}}/convert-to-invoice?book=" . (($book === true) ? 'true' : 'false')) ->getBody()); $invoice = (new CustomerInvoiceBuilder($this->request))->find($response->invoice_id); @@ -85,7 +85,7 @@ public function notes() return $this->request->handleWithExceptions(function () { $builder = new OrderNoteBuilder($this->request); - $builder->setEntity(str_replace(':number', $this->{$this->primaryKey}, $builder->getEntity())); + $builder->setEntity(str_replace(':number', $this->{$this->url_friendly_id}, $builder->getEntity())); return $builder->get(); }); diff --git a/src/Models/OrderShipment.php b/src/Models/OrderShipment.php index 8631e1b..e15c5ca 100644 --- a/src/Models/OrderShipment.php +++ b/src/Models/OrderShipment.php @@ -20,12 +20,12 @@ public function markShipped() { return $this->request->handleWithExceptions(function () { - return $this->request->client->post("orders/shipments/{$this->{$this->primaryKey}}/mark-shipped", [ + return $this->request->client->post("orders/shipments/{$this->{$this->url_friendly_id}}/mark-shipped", [ 'json' => [ 'pick' => true, - ] + ], ]) ->getBody() ->getContents(); diff --git a/src/Models/Product.php b/src/Models/Product.php index 08ef497..0637af2 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -44,8 +44,8 @@ public function inventoryMatrix( $location_id = null, array $filter = null ) } } - $response = $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}/variation-matrix" . - $query ); + $response = $this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}/variation-matrix" . + $query); $html = (string) $response->getBody(); @@ -58,7 +58,7 @@ public function variations( $variation_id = 1001 ) { return $this->request->handleWithExceptions( function () use ( $variation_id ) { - $response = $this->request->client->get( "variations/{$this->{$this->primaryKey}}/variation-matrix" ); + $response = $this->request->client->get("variations/{$this->{$this->url_friendly_id}}/variation-matrix"); $html = (string) $response->getBody(); @@ -70,7 +70,7 @@ public function location($number = null) { return $this->request->handleWithExceptions(function () use ($number) { - $response = json_decode((string)$this->request->client->get("{$this->entity}/{$this->{$this->primaryKey}}/locations" . (($number !== null) ? '/' . $number : ''))->getBody()); + $response = json_decode((string)$this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}/locations" . (($number !== null) ? '/' . $number : ''))->getBody()); if (isset($response->product_locations)) { @@ -109,7 +109,7 @@ public function fields() return $this->request->handleWithExceptions( function () { $response = - $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}/fields" ); + $this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}/fields"); return collect( json_decode( (string) $response->getBody() )->field_values ); diff --git a/src/Models/ProductionOrder.php b/src/Models/ProductionOrder.php index c714759..a71e9c9 100644 --- a/src/Models/ProductionOrder.php +++ b/src/Models/ProductionOrder.php @@ -19,8 +19,8 @@ class ProductionOrder extends Model public function getPDF() { return $this->request->handleWithExceptions( function () { - return $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}.pdf" )->getBody() - ->getContents(); + return $this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}.pdf")->getBody() + ->getContents(); } ); } } diff --git a/src/Models/PurchaseOrder.php b/src/Models/PurchaseOrder.php index 371ee6d..6edfb67 100644 --- a/src/Models/PurchaseOrder.php +++ b/src/Models/PurchaseOrder.php @@ -13,8 +13,8 @@ class PurchaseOrder extends Model public function getPDF() { return $this->request->handleWithExceptions( function () { - return $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}.pdf" )->getBody() - ->getContents(); + return $this->request->client->get("{$this->entity}/{$this->{$this->url_friendly_id}}.pdf")->getBody() + ->getContents(); } ); } @@ -23,9 +23,9 @@ public function reopen() return $this->request->handleWithExceptions( function () { - return $this->request->client->post( "{$this->entity}/{$this->{$this->primaryKey}}/reopen" ) - ->getBody() - ->getContents(); + return $this->request->client->post("{$this->entity}/{$this->{$this->url_friendly_id}}/reopen") + ->getBody() + ->getContents(); } ); } } diff --git a/src/Models/Variation/Type.php b/src/Models/Variation/Type.php index bafb996..2af0db1 100644 --- a/src/Models/Variation/Type.php +++ b/src/Models/Variation/Type.php @@ -31,8 +31,8 @@ public function options() { $options = new TypeOptionBuilder( $this->request ); $old_entity = $options->getEntity(); - $new_entity = $options->setEntity( str_replace( ':variation_number', $this->variation_id, $old_entity ) ); - $options->setEntity( str_replace( ':type_number', $this->{$this->primaryKey}, $new_entity ) ); + $new_entity = $options->setEntity(str_replace(':variation_number', $this->variation_id, $old_entity)); + $options->setEntity(str_replace(':type_number', $this->{$this->url_friendly_id}, $new_entity)); return $options; } diff --git a/src/Utils/Model.php b/src/Utils/Model.php index 94083a4..0340019 100644 --- a/src/Utils/Model.php +++ b/src/Utils/Model.php @@ -13,6 +13,7 @@ class Model { protected $entity; protected $primaryKey; + protected $url_friendly_id; protected $modelClass = self::class; protected $fillable = []; @@ -45,7 +46,7 @@ protected function setAttribute( $attribute, $value ) { if ($attribute === $this->primaryKey) { - $value = rawurlencode(rawurlencode($value)); + $this->url_friendly_id = rawurldecode(rawurldecode($value)); } $this->{$attribute} = $value; @@ -76,7 +77,7 @@ public function delete() { return $this->request->handleWithExceptions( function () { - return $this->request->client->delete( "{$this->entity}/{$this->{$this->primaryKey}}" ); + return $this->request->client->delete("{$this->entity}/{$this->{$this->url_friendly_id}}"); } ); } @@ -85,9 +86,9 @@ public function update( $data = [] ) return $this->request->handleWithExceptions( function () use ( $data ) { - $response = $this->request->client->put( "{$this->entity}/{$this->{$this->primaryKey}}", [ + $response = $this->request->client->put("{$this->entity}/{$this->{$this->url_friendly_id}}", [ 'json' => $data, - ] ); + ]); $responseData = collect( json_decode( (string) $response->getBody() ) );