unisharp / pricing
A modularized pricing package for Buyable.
Installs: 1 659
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 11
Forks: 2
Open Issues: 1
Requires
- php: >=5.6
- illuminate/support: ~5
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.6.0
- mockery/mockery: ^0.9.4
- orchestra/database: ~3.6.0
- orchestra/testbench: ~3.6.0
- phpunit/phpunit: ^7.0
- sebastian/diff: ^3.0.0
- squizlabs/php_codesniffer: ^3.3
This package is auto-updated.
Last update: 2024-10-13 22:27:22 UTC
README
A modularized pricing package for buyalbe.
Installation
composer require unisharp/pricing dev-master
Configuration
php artisan vendor:publish --tag pricing
Set available pricing modules in config/pricing.php
return [ 'modules' => [ UniSharp\Pricing\Tests\Fixtures\TestModule::class, ] ];
Module Principles
- Module must implement
UniSharp\Pricing\ModuleContract
which needshandle
andfinish
functions. - Modules will be processed by the sequence in
config/pricing.php
, the first module will handle the pricing logic and pass pricing instance to the next module (Pipeline Pattern). - There are some APIs a pricing module can call in the handle function:
- $pricing->addFee(int $fee);
- $pricing->addDeduction(int $deduction);
- $pricing->writeModuleLog(mix $log);
- $pricing->getModuleInfo();
addFee
,addDeduction
,writeModuleLog
will only change pricing instance's properties.getModuleInfo
can get extra info of that module.
- Finish functions will be called after pricing execute. The logic after module successfully applied can be implemented here.
namespace UniSharp\Pricing\Tests\Fixtures; use Closure; use UniSharp\Pricing\Pricing; use UniSharp\Pricing\ModuleContract; use Illuminate\Contracts\Pipeline\Pipeline; class TestModule implements ModuleContract { const FEE = 99; const DEDUCTION = 88; const LOG = 'log'; public function handle(Pricing $pricing, Closure $next) { $pricing->addFee(static::FEE); $pricing->addDeduction(static::DEDUCTION); $pricing->writeModuleLog(static::LOG); $info = $pricing->getModuleInfo(); return $next($pricing); } public function finish(Pricing $pricing) { // } }
Pricing Usages
use UniSharp\Pricing\Facades\Pricing; class Foo { // set items and get original price Pricing::setItems(UniSharp\Cart\CartItemCollection $items) ->getOriginalTotal(); // apply some modules Pricing::apply(ModuleA::class) ->apply(ModuleB::class); // apply some modules with some extra info Pricing::apply(ModuleA::class) ->apply(ModuleB::class) ->with([ ModuleA::class => 'extra info A', ModuleB::class => 'extra info B', ]); // apply modules and get final total Pricing::apply(ModuleA::class) ->apply(ModuleB::class) ->getTotal(); // apply modules and execute them Pricing::apply(ModuleA::class) ->apply(ModuleB::class) ->execute(); // get all applied modules Pricing::getAppliedModules(); // get all fees Pricing::getFees(); // get fee of a specific module Pricing::getFee(ModuleA::class); // get all deductions Pricing::getDeductions(); // get deduction of a specific module Pricing::getDeduction(ModuleA::class); // get items Pricing::getItems(); }