Skip to content
This repository was archived by the owner on Jan 29, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Contracts/ShippingMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Vanilo\Shipping\Contracts;

interface ShippingMethod
{
}
50 changes: 50 additions & 0 deletions src/Models/ShippingRate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Vanilo\Shipping\Models;

use Illuminate\Contracts\Support\Arrayable;

class ShippingRate implements Arrayable
{
protected $price;
protected $currency;

public function setPrice($value, $currency = null)
{
$this->price = $value;
if (isset($currency)) {
$this->currency = $currency;
}
}

public function setCurrency($value)
{
$this->currency = $value;
}

public function getPrice()
{
return $this->price;
}

public function getConvertedPrice()
{
return currency($this->price, $this->currency, null, false);
}

public function getCurrency()
{
return $this->currency;
}

public function toArray()
{
return [
'price' => $this->price,
'currency' => $this->currency,
'converted_price' => $this->getConvertedPrice(),
'converted_currency' => currency()->getUserCurrency(),
'display_price' => currency_format($this->getConvertedPrice(), currency()->getUserCurrency()),
];
}
}
49 changes: 49 additions & 0 deletions src/ShippingMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Vanilo\Shipping;

use Vanilo\Shipping\Contracts\ShippingMethod;

class ShippingMethods
{
/** @var array */
private static $registry = [];

public static function register(string $id, string $class)
{
if (array_key_exists($id, self::$registry)) {
return;
}

if (!class_implements($class, ShippingMethod::class)) {
throw new \InvalidArgumentException(
sprintf(
'The class you are trying to register (%s) as property type, ' .
'must implement the %s interface.',
$class,
ShippingMethod::class
)
);
}

self::$registry[$id] = $class;
}

public static function make(string $id)
{
$className = self::getClass($id);

if (null === $className) {
throw new \Exception(
"No shipping method is registered with the id `$id`."
);
}

return app()->make($className);
}

public static function getClass(string $id): ?string
{
return self::$registry[$id] ?? null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateShippingMethods extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shipping_methods', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('class_name');
$table->text('options');
$table->smallInteger('status');
$table->smallInteger('order');
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('shipping_methods');
}
}