Permalink
Browse files

inital commit

  • Loading branch information...
1 parent aa08c17 commit 164b481ab1777617b83181aa7dff1010f028fba9 @iampersistent iampersistent committed Jan 22, 2013
View
@@ -0,0 +1,3 @@
+Pricing
+=======
+
View
@@ -17,3 +17,4 @@
}
}
}
+
@@ -0,0 +1,65 @@
+<?php
+/**
+ * (c) 2013 Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+namespace Vespolina\Entity\Pricing\Element;
+
+use Vespolina\Entity\Pricing\PricingElement;
+
+class RecurringElement extends PricingElement
+{
+ public function __construct()
+ {
+ $attribute['cycles'] = '';
+ $attribute['interval'] = '';
+ $attribute['startsIn'] = null;
+
+ parent::__construct();
+ }
+
+ public function setCycles($cycles)
+ {
+ $this->attributes['cycles'] = $cycles;
+
+ return $this;
+ }
+
+ public function getCycles()
+ {
+ return $this->attributes['cycles'];
+ }
+
+ public function setInterval($interval)
+ {
+ $this->attributes['interval'] = $interval;
+
+ return $this;
+ }
+
+ public function getInterval()
+ {
+ return $this->attributes['interval'];
+ }
+
+ public function setStartsIn($startsIn)
+ {
+ $this->attributes['startsIn'] = $startsIn;
+
+ return $this;
+ }
+
+ public function getStartsIn()
+ {
+ return $this->attributes['startsIn'];
+ }
+
+ protected function doProcess($context, $processed)
+ {
+ $processed['netValue'] = $this->attribute['netValue'];
+
+ return $processed;
+ }
+}
@@ -0,0 +1,79 @@
+<?php
+/**
+ * (c) 2011-2012 Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Pricing\Entity;
+
+/**
+ * PricingContext implements a data container holding price variables needed for calculation
+ *
+ * @author Daniel Kucharski <daniel@xerias.be>
+ */
+
+use Vespolina\Entity\Pricing\PricingContextInterface;
+
+class PricingContext implements PricingContextInterface
+{
+ protected $data;
+ protected $entities;
+
+ public function __construct($data = array())
+ {
+ $this->data = $data;
+ $this->entities = array();
+
+ if ($this->getQuantity() === null) {
+ $this->setQuantity(1);
+ }
+ }
+
+ public function addEntity($entity)
+ {
+ $this->entities[] = $entity;
+ }
+
+ public function getEntities()
+ {
+ return $this->entities;
+ }
+
+ public function setEntities($entities)
+ {
+ $this->entities = $entities;
+ }
+
+ public function get($key, $default = null)
+ {
+ if (array_key_exists($key, $this->data)) {
+ return $this->data[$key];
+ } elseif ($default) {
+ return $default;
+ } else {
+ return null;
+ }
+ }
+
+ public function set($key, $value)
+ {
+ $this->data[$key] = $value;
+ }
+
+ public function getData()
+ {
+ return $this->data;
+ }
+
+ public function getQuantity()
+ {
+ return $this->get('quantity');
+ }
+
+ public function setQuantity($quantity)
+ {
+ $this->set('quantity', $quantity);
+ }
+}
@@ -0,0 +1,54 @@
+<?php
+/**
+ * (c) 2012 Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+namespace Pricing\Entity;
+
+use Vespolina\Entity\Pricing\PricingElementInterface;
+use Vespolina\Exception\FunctionNotSupportedException;
+
+class PricingElement implements PricingElementInterface
+{
+ protected $attributes;
+ protected $order;
+ protected $type;
+
+ public function __construct()
+ {
+ $attribute['netValue'] = '';
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setOrder($order)
+ {
+ $this->order = $order;
+
+ return $this;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getOrder()
+ {
+ return $this->order;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function process($context, $processed)
+ {
+ return $this->doProcess($context, $processed);
+ }
+
+ protected function doProcess($context, $processed)
+ {
+ throw new FunctionNotSupportedException('process() has not been implemented in ' . get_class($this));
+ }
+}
@@ -0,0 +1,86 @@
+<?php
+/**
+ * (c) Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Pricing\Entity;
+
+use Vespolina\Entity\Pricing\PricingSetInterface;
+
+class PricingSet implements PricingSetInterface
+{
+ protected $context;
+ protected $elements;
+ protected $processed;
+ protected $processingState = self::PROCESSING_UNPROCESSED;
+ protected $returns;
+
+ const PROCESSING_UNPROCESSED = 0;
+ const PROCESSING_FINISHED = 1;
+
+ public function __construct(array $customReturns = array())
+ {
+ $defaultReturns = array(
+ 'discounts', 'netValue', 'surcharge', 'taxes', 'totalValue'
+ );
+ $this->returns = array_merge($defaultReturns, $customReturns);
+
+ foreach ($this->returns as $return) {
+ $this->processed[$return] = null;
+ }
+ }
+
+ public function getDiscounts()
+ {
+ return $this->get('discounts');
+ }
+
+ public function getNetValue()
+ {
+ return $this->get('netValue');
+ }
+
+ public function getSurcharge()
+ {
+ return $this->get('surcharge');
+ }
+
+ public function getTaxes()
+ {
+ return $this->get('taxes');
+ }
+
+ public function getTotalValue()
+ {
+ return $this->get('totalValue');
+ }
+
+ public function __get($name)
+ {
+ return $this->get($name);
+ }
+
+ public function get($name)
+ {
+ if ($this->processingState != self::PROCESSING_FINISHED) {
+ throw new \Exception();
+ }
+
+ return $this->processed[$name];
+ }
+
+ public function process($context = null)
+ {
+ // create empty array with keys from $this->processed
+ $processed = array();
+ foreach ($this->elements as $element) {
+ $processed = array_merge($this->processed, $element->process($context, $processed));
+ }
+
+ $this->processed = $processed;
+ $this->processingState = self::PROCESSING_FINISHED;
+ }
+}
View
@@ -12,4 +12,5 @@
<directory suffix=".php">./lib</directory>
</whitelist>
</filter>
-</phpunit>
+</phpunit>
+
@@ -0,0 +1,45 @@
+<?php
+
+use Vespolina\Entity\Pricing\PricingElement;
+use Vespolina\Entity\Pricing\PricingSet;
+
+class PricingSetTest extends \PHPUnit_Framework_TestCase
+{
+ public function testAddElements()
+ {
+ $pricingSet = new PricingSet();
+
+ $this->assertNull($pricingSet->getElements(), 'the elements should start empty');
+
+ $element1 = new PricingElement();
+ $pricingSet->addElement($element1);
+ $this->assertCount(1, $pricingSet->getElements());
+
+ $element2 = new PricingElement();
+ $pricingSet->addElement($element2);
+ $this->assertCount(2, $pricingSet->getElements());
+ }
+
+ public function testProcess()
+ {
+ $pricingSet = new PricingSet();
+
+ $elementNetValue = $this->getMock('Vespolina\Entity\Pricing\PricingElement', array('process'));
+ $elementNetValue->expects($this->any())
+ ->method('process')
+ ->will($this->returnValue(array('netValue' => '9.99')));
+ $elementDiscount = $this->getMock('Vespolina\Entity\Pricing\PricingElement', array('process'));
+ $elementDiscount->expects($this->any())
+ ->method('process')
+ ->will($this->returnValue(array('discount' => '.99')));
+
+ $pricingSet->process();
+
+ $this->assertEqual('9.99', $pricingSet->getNetValue(), 'the final value should be 9.99');
+ $this->assertEqual('.99', $pricingSet->getDiscount(), 'the final value should be .99');
+ $this->assertEqual('9.00', $pricingSet->getValue(), 'the final value should be 9.00');
+
+ $this->markTestIncomplete('test all default returns');
+ $this->markTestIncomplete('test custom returns');
+ }
+}

0 comments on commit 164b481

Please sign in to comment.