-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
156 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* PHP Billing Library | ||
* | ||
* @link https://github.com/hiqdev/php-billing | ||
* @package php-billing | ||
* @license BSD-3-Clause | ||
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) | ||
*/ | ||
|
||
namespace hiqdev\php\billing\order; | ||
|
||
use DateTimeImmutable; | ||
use hiqdev\php\billing\action\ActionInterface; | ||
|
||
/** | ||
* Creates order from given source: | ||
* - Order: just passs by. Can be prepared more in other implementations. | ||
* - Action or Action[]: create order from given action(s) | ||
* @author Andrii Vasyliev <sol@hiqdev.com> | ||
*/ | ||
class Collector implements CollectorInterface | ||
{ | ||
public function collect($source, DateTimeImmutable $time = null): OrderInterface | ||
{ | ||
if ($source instanceof OrderInterface) { | ||
return $source; | ||
} | ||
if ($source instanceof ActionInterface) { | ||
return Order::fromAction($source); | ||
} | ||
if (is_array($source)) { | ||
$item = reset($source); | ||
if ($item instanceof OrderInterface) { | ||
return $this->mergeOrders($source); | ||
} | ||
if ($item instanceof ActionInterface) { | ||
return Order::fromActions($source); | ||
} | ||
} | ||
|
||
throw new \Exception('unknown order source'); | ||
} | ||
|
||
protected function mergeOrders(array $orders): OrderInterface | ||
{ | ||
$actions = []; | ||
foreach ($orders as $order) { | ||
$actions = array_merge($actions, $order->getActions()); | ||
} | ||
|
||
return Order::fromActions($actions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* PHP Billing Library | ||
* | ||
* @link https://github.com/hiqdev/php-billing | ||
* @package php-billing | ||
* @license BSD-3-Clause | ||
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) | ||
*/ | ||
|
||
namespace hiqdev\php\billing\order; | ||
|
||
use DateTimeImmutable; | ||
|
||
/** | ||
* Order Collector collects order from given source. | ||
* Actual source can be different, see Collector. | ||
* Also, it can be user shopping for example. | ||
* | ||
* ``` | ||
* $cart = new Cart(); | ||
* /// fill cart | ||
* $bills = $this->billing->calculate($cart) | ||
* ``` | ||
* @author Andrii Vasyliev <sol@hiqdev.com> | ||
*/ | ||
interface CollectorInterface | ||
{ | ||
/** | ||
* @return OrderInterface | ||
*/ | ||
public function collect($source, DateTimeImmutable $time = null): OrderInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters