Skip to content

Commit

Permalink
Merge pull request #7 from blesta/style-formatting
Browse files Browse the repository at this point in the history
Formatted the code to psr-2
  • Loading branch information
clphillips committed May 27, 2016
2 parents 702b10c + 5d4fb27 commit 8e188d9
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 325 deletions.
39 changes: 0 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,45 +178,6 @@ foreach ($item_collection as $item) {
}
```

### ItemComparator

The ItemComparator merges two ItemPrice objects into a single ItemPrice
object containing the taxes and discounts of the second ItemPrice being merged.

The resulting ItemPrice is given a price and description determined by the
given callbacks.

```php
$price_callback = function($old_price, $new_price, $old_meta, $new_meta) {
return ($old_price - $new_price);
}
$description_callback = function($old_meta, $new_meta) {
return '1x Apples';
}

$comparator = new ItemComparator($price_callback, $description_callback);

$old_item = new ItemPrice(1, 10);
$new_item = new ItemPrice(15, 2);

// Set custom meta data on the ItemPrice
// this is passed to the price and description callbacks
// as a Blesta\Items\Collection\ItemCollection containing the items attached
$old_meta = new Blesta\Items\Item\Item();
$new_meta = new Blesta\Items\Item\Item();
$old_item->attach($old_meta);
$new_item->attach($new_meta);

// Merge from $old_item to $new_item
$item = $comparator->merge($old_item, $new_item);

$item->price(); // -20
$item->qty(); // 1
$item->description() // 1x Apples
$item->discounts(); // same as $new_item
$item->taxes(); // same as $new_item
```

### PricingFactory

Using the PricingFactory can streamline usage. Assume you have the following:
Expand Down
4 changes: 2 additions & 2 deletions src/AutoloadPricing.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function load($class)
$totalDir = $baseDir . 'Total' . DIRECTORY_SEPARATOR;
$typeDir = $baseDir . 'Type' . DIRECTORY_SEPARATOR;

$classes = array(
$classes = [
'AbstractPriceDescription' => $descriptionDir . 'AbstractPriceDescription.php',
'AbstractPriceModifier' => $modifierDir . 'AbstractPriceModifier.php',
'DiscountPrice' => $modifierDir . 'DiscountPrice.php',
Expand All @@ -32,7 +32,7 @@ public static function load($class)
'PriceInterface' => $typeDir . 'PriceInterface.php',
'TaxPrice' => $modifierDir . 'TaxPrice.php',
'UnitPrice' => $typeDir . 'UnitPrice.php'
);
];

if (isset($classes[$class])) {
include $classes[$class];
Expand Down
8 changes: 4 additions & 4 deletions src/Collection/ItemPriceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ItemPriceCollection implements PriceTotalInterface, Iterator
/**
* @var array A collection of ItemPrice objects
*/
private $collection = array();
private $collection = [];

/**
* @var int The current index position within the collection
Expand Down Expand Up @@ -175,7 +175,7 @@ public function discountAmount(DiscountPrice $discount = null)
public function taxes()
{
// Include unique instances of TaxPrice
$taxes = array();
$taxes = [];
foreach ($this->collection as $item_price) {
foreach ($item_price->taxes() as $tax_price) {
if (!in_array($tax_price, $taxes, true)) {
Expand All @@ -195,7 +195,7 @@ public function taxes()
public function discounts()
{
// Include unique instances of DiscountPrice
$discounts = array();
$discounts = [];
foreach ($this->collection as $item_price) {
foreach ($item_price->discounts() as $discount_price) {
if (!in_array($discount_price, $discounts, true)) {
Expand Down Expand Up @@ -314,7 +314,7 @@ public function rewind()
/**
* Determines whether the current pointer references a valid item in the collection
*
* @return boolean True if the pointer references a valid item in the collection, false otherwise
* @return bool True if the pointer references a valid item in the collection, false otherwise
*/
public function valid()
{
Expand Down
14 changes: 7 additions & 7 deletions src/Type/ItemPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class ItemPrice extends UnitPrice implements PriceTotalInterface
/**
* @var array A cached value of discount subtotals
*/
private $discount_amounts = array();
private $discount_amounts = [];
/**
* @var boolean Whether or not to cache discount subtotals
* @var bool Whether or not to cache discount subtotals
*/
private $cache_discount_amounts = false;
/**
Expand All @@ -21,11 +21,11 @@ class ItemPrice extends UnitPrice implements PriceTotalInterface
/**
* @var array A numerically-indexed array of DiscountPrice objects
*/
protected $discounts = array();
protected $discounts = [];
/**
* @var array A numerically-indexed array containing an array of TaxPrice objects
*/
protected $taxes = array();
protected $taxes = [];

/**
* Initialize the item price
Expand Down Expand Up @@ -137,14 +137,14 @@ public function total()
{
// discountAmount() is called twice: once by totalAfterDiscount, and once by taxAmount
// The discount must be removed only once, so flag it to be ignored the second time
$this->discount_amounts = array();
$this->discount_amounts = [];
$this->cache_discount_amounts = true;
$total = $this->totalAfterDiscount();

// Include tax without taking the discount off again, and reset the flag
$this->cache_discount_amounts = false;
$total += $this->taxAmount();
$this->discount_amounts = array();
$this->discount_amounts = [];

return $total;
}
Expand Down Expand Up @@ -337,7 +337,7 @@ public function taxes($unique = true)
}

// Retrieve all unique taxes
$all_taxes = array();
$all_taxes = [];
foreach ($this->taxes as $taxes) {
$all_taxes = array_merge($all_taxes, array_values($taxes));
}
Expand Down
2 changes: 1 addition & 1 deletion src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'AutoloadPricing.php';

spl_autoload_register(array('AutoloadPricing', 'load'));
spl_autoload_register(['AutoloadPricing', 'load']);
39 changes: 19 additions & 20 deletions tests/Collection/ItemPriceCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
class ItemPriceCollectionTest extends PHPUnit_Framework_TestCase
{

/**
* @covers ::append
* @covers ::count
Expand Down Expand Up @@ -155,7 +154,7 @@ public function testDiscountAmount(ItemPriceCollection $collection, array $expec
*/
public function testTaxes(ItemPriceCollection $collection, array $expected_totals)
{
$this->assertContainsOnlyInstancesOf("TaxPrice", $collection->taxes());
$this->assertContainsOnlyInstancesOf('TaxPrice', $collection->taxes());

// Exactly each expected tax should exist
foreach ($expected_totals['taxes'] as $tax_price) {
Expand All @@ -175,7 +174,7 @@ public function testTaxes(ItemPriceCollection $collection, array $expected_total
*/
public function testDiscounts(ItemPriceCollection $collection, array $expected_totals)
{
$this->assertContainsOnlyInstancesOf("DiscountPrice", $collection->discounts());
$this->assertContainsOnlyInstancesOf('DiscountPrice', $collection->discounts());

// Exactly each expected discount should exist
foreach ($expected_totals['discounts'] as $discount_price) {
Expand Down Expand Up @@ -259,11 +258,11 @@ public function totalProvider()
$collection3 = new ItemPriceCollection();
$collection3->append($item4)->append($item5)->append($item6);

return array(
array($collection1, $this->getItemTotals($item1)),
array($collection2, $this->getItemTotals($item2, $item3)),
array($collection3, $this->getItemTotals($item4, $item5, $item6)),
);
return [
[$collection1, $this->getItemTotals($item1)],
[$collection2, $this->getItemTotals($item2, $item3)],
[$collection3, $this->getItemTotals($item4, $item5, $item6)],
];
}

/**
Expand All @@ -277,16 +276,16 @@ private function getItemTotals()
{
// NOTE: 'total', 'total_with_discount', and 'discount' may be INCORRECT
// if a DiscountPrice of type 'amount' applies to multiple items!
$totals = array(
$totals = [
'subtotal' => 0,
'total' => 0,
'total_with_tax' => 0,
'total_with_discount' => 0,
'tax' => 0,
'discount' => 0,
'taxes' => array(),
'discounts' => array()
);
'taxes' => [],
'discounts' => []
];

$args = func_get_args();
foreach ($args as $item) {
Expand Down Expand Up @@ -319,7 +318,7 @@ private function getUnique($arr1, $arr2)
{
foreach ($arr2 as $obj) {
if (!in_array($obj, $arr1, true)) {
$arr1 = array_merge($arr1, array($obj));
$arr1 = array_merge($arr1, [$obj]);
}
}

Expand Down Expand Up @@ -440,13 +439,13 @@ public function mergeProvider()
$collection3->append($item3);
$collection4->append($item2);

return array(
array($collection1, $collection2, 2),
array($collection1, $collection3, 1),
array($collection2, $collection3, 1),
array($collection3, $collection1, 1),
array($collection3, $collection4, 0)
);
return [
[$collection1, $collection2, 2],
[$collection1, $collection3, 1],
[$collection2, $collection3, 1],
[$collection3, $collection1, 1],
[$collection3, $collection4, 0]
];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Description/AbstractPriceDescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public function testGetDescription()
{
// Initially null
$description = null;
$stub = $this->getMockForAbstractClass("AbstractPriceDescription");
$stub = $this->getMockForAbstractClass('AbstractPriceDescription');
$this->assertSame($description, $stub->getDescription());

// Set my own description
$description = '100x Product 1 - Limited Time Offer';
$stub = $this->getMockForAbstractClass("AbstractPriceDescription");
$stub = $this->getMockForAbstractClass('AbstractPriceDescription');
$stub->setDescription($description);
$this->assertSame($description, $stub->getDescription());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Modifier/AbstractPriceModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AbstractPriceModifierTest extends PHPUnit_Framework_TestCase
public function testAmount()
{
$price = 5.00;
$stub = $this->getMockForAbstractClass("AbstractPriceModifier", array($price, 'inclusive'));
$stub = $this->getMockForAbstractClass('AbstractPriceModifier', [$price, 'inclusive']);
$this->assertSame($price, $stub->amount());
}

Expand All @@ -23,7 +23,7 @@ public function testAmount()
public function testType()
{
$type = 'inclusive';
$stub = $this->getMockForAbstractClass("AbstractPriceModifier", array(10.00, $type));
$stub = $this->getMockForAbstractClass('AbstractPriceModifier', [10.00, $type]);
$this->assertSame($type, $stub->type());
}

Expand All @@ -33,7 +33,7 @@ public function testType()
*/
public function testReset()
{
$stub = $this->getMockForAbstractClass("AbstractPriceModifier", array(10.00, 'inclusive'));
$stub = $this->getMockForAbstractClass('AbstractPriceModifier', [10.00, 'inclusive']);
$this->assertNull($stub->reset());
}
}
Loading

0 comments on commit 8e188d9

Please sign in to comment.