/** * This method processes the models and returns the status. * * @access public * @param BT\Exchange $exchange the exchange given to process * @return integer the status code */ public function process(BT\Exchange $exchange) { $order = $exchange->getIn()->getBody()->Order; $tax_amount = Trade\Money::make($order->terms->subtotal, $order->currency)->add(Trade\Money::make($order->terms->freight->amount, $order->currency)); $tax_rate = Core\Convert::toDouble($this->policy->getValue('rate')); $tax_amount = $tax_amount->multiply($tax_rate); $order->terms->tax->amount = $tax_amount->getConvertedAmount(); return BT\Task\Status::SUCCESS; }
/** * This method processes the models and returns the status. * * @access public * @param BT\Exchange $exchange the exchange given to process * @return integer the status code */ public function process(BT\Exchange $exchange) { $order = $exchange->getIn()->getBody()->Order; $amount = Core\Convert::toDouble($this->policy->getValue('amount')); if ($order->terms->subtotal < $amount) { return BT\Task\Status::SUCCESS; } return BT\Task\Status::FAILED; }
/** * This method processes the models and returns the status. * * @access public * @param BT\Exchange $exchange the exchange given to process * @return integer the status code */ public function process(BT\Exchange $exchange) { $callable = explode(',', $this->policy->getValue('callable')); $options = Core\Convert::toInteger($this->policy->getValue('options')); $probability = Core\Convert::toDouble($this->policy->hasKey('odds')) * $options; if (call_user_func($callable, array(1, $options)) <= $probability) { $status = BT\Task\Handler::process($this->task, $exchange); return $status; } return BT\Task\Status::ACTIVE; }
/** * This method processes the models and returns the status. * * @access public * @param BT\Exchange $exchange the exchange given to process * @return integer the status code */ public function process(BT\Exchange $exchange) { $order = $exchange->getIn()->getBody()->Order; $weight = 0.0; foreach ($order->lines->items as $line) { $value = $line->item->weightEach->value; $uom = $line->item->weightEach->unit; if (!preg_match('/^kg(s)?$/i', $uom)) { $value = $value * self::KGS_TO_LBS_CONVERSION_RATE; } else { if (preg_match('/^lb(s)?$/i', $uom)) { return BT\Task\Status::ERROR; } } $weight += $line->quantity * $value; } $breakpoint = Core\Convert::toDouble($this->policy->getValue('breakpoint')); $rate = Core\Convert::toDouble($this->policy->getValue('rate')); $surcharge = Trade\Money::make($this->policy->getValue('surcharge'), $order->currency); $weight = max(0.0, ceil($weight) - $breakpoint); $order->terms->freight->amount = Trade\Money::make($weight, $order->currency)->multiply($rate)->add($surcharge)->getConvertedAmount(); return BT\Task\Status::SUCCESS; }
/** * This method attempts to resolve the value as a number in accordance with the schema * definition. * * @access public * @static * @param mixed $value the value to be resolved * @param array $definition the schema definition * @return mixed the resolved value * @throws Throwable\Runtime\Exception indicates that the value failed * to meet a requirement */ public static function resolveNumberValue($value, $definition) { if (MappingService\Data\ToolKit::isUnset($value)) { if (isset($definition['required']) && $definition['required']) { throw new Throwable\Runtime\Exception('Invalid value defined. Expected a value that is a number, but got :type.', array(':type' => Core\DataType::info($value)->type)); } return $value; } $value = Core\Convert::toDouble($value); if (isset($definition['exclusiveMinimum']) && $definition['exclusiveMinimum']) { if ($value <= $definition['minimum']) { throw new Throwable\Runtime\Exception('Invalid value defined. Expected a value that is greater than ":minimum", but got :value.', array(':minimum' => $definition['minimum'], ':value' => $value)); } } else { if (isset($definition['minimum'])) { if ($value < $definition['minimum']) { throw new Throwable\Runtime\Exception('Invalid value defined. Expected a value that is greater than or equal to ":minimum", but got :value.', array(':minimum' => $definition['minimum'], ':value' => $value)); } } } if (isset($definition['exclusiveMaximum']) && $definition['exclusiveMaximum']) { if ($value >= $definition['maximum']) { throw new Throwable\Runtime\Exception('Invalid value defined. Expected a value that is less than ":maximum", but got :value.', array(':maximum' => $definition['maximum'], ':value' => $value)); } } else { if (isset($definition['maximum'])) { if ($value > $definition['maximum']) { throw new Throwable\Runtime\Exception('Invalid value defined. Expected a value that is less than or equal to ":maximum", but got :value.', array(':maximum' => $definition['maximum'], ':value' => $value)); } } } if (isset($definition['divisibleBy'])) { if (fmod($value, $definition['divisibleBy']) == 0.0) { throw new Throwable\Runtime\Exception('Invalid value defined. Expected a value that is divisible by ":divisibleBy", but got :value.', array(':divisibleBy' => $definition['divisibleBy'], ':value' => $value)); } } if (isset($definition['enum']) && count($definition['enum']) > 0) { if (!in_array($value, $definition['enum'])) { throw new Throwable\Runtime\Exception('Invalid value defined. Expected a value that is in enumeration, but got :value.', array(':value' => $value)); } } return $value; }
/** * This method parses a "real" node. * * @access protected * @param \SimpleXMLElement $node a reference to the "real" node * @return double the value as a real number * @throws \Unicity\Throwable\Parse\Exception indicates that problem occurred while * parsing */ protected function parseRealElement(\SimpleXMLElement $node) { $value = dom_import_simplexml($node)->textContent; $value = trim($value); if (!preg_match('/^[+-]?(0|[1-9][0-9]*)((\\.[0-9]+)|([eE][+-]?(0|[1-9][0-9]*)))?$/', $value)) { throw new Throwable\Parse\Exception('Expected a real number, but got :value for the value.', array(':value' => $value)); } return Core\Convert::toDouble($value); }
/** * This method returns a number formatted as a currency string. * * @access public * @static * @param double $number the number to be formatted * @return string the formatted number * * @see http://php.net/money_format */ public static function format($number) { $amount = Core\Convert::toDouble($number); if (function_exists('money_format')) { return money_format('%i', $amount); } return sprintf('%0.2f', $amount); }
/** * This method parses a "number" node. * * @access protected * @param \DOMElement $node a reference to the "number" node * @return number the value as a number * @throws \Unicity\Throwable\Parse\Exception indicates that problem occurred while * parsing */ protected function parseNumberElement(\DOMElement $node) { $value = $node->textContent; $value = trim($value); if (preg_match('/^[+-]?(0|[1-9][0-9]*)((\\.[0-9]+)|([eE][+-]?(0|[1-9][0-9]*)))$/', $value)) { $value = Core\Convert::toDouble($value); } else { if (filter_var($value, FILTER_VALIDATE_INT) !== false) { $value = Core\Convert::toInteger($value); } else { throw new Throwable\Parse\Exception('Expected an integer number, but got :value for the value.', array(':value' => $value)); } } return $value; }
/** * This method parses a child node. * * @access protected * @param \SimpleXMLElement $node a reference to a child node * @return \Unicity\Common\Mutable\ICollection a collection representing the data * in the soap file * @throws \Unicity\Throwable\Parse\Exception indicates that an unrecognized child * node was encountered */ protected function parseChildElement(\SimpleXMLElement $node) { $children = $node->children(); if (count($children) > 0) { $list = new Common\Mutable\ArrayList(); $map = new Common\Mutable\HashMap(); foreach ($children as $child) { $name = $child->getName(); $value = $this->parseChildElement($child); $temp = new Common\Mutable\HashMap(); $temp->putEntry($name, $value); $list->addValue($temp); $map->putEntry($name, $value); } return $list->count() > $map->count() || $this->directives->hasKey('expandableProperties') && $this->directives->getValue('expandableProperties')->hasValue($node->getName()) ? $list : $map; } else { $value = dom_import_simplexml($node)->textContent; $value = trim($value); if ($value == '') { $value = Core\Data\Undefined::instance(); } else { if (preg_match('/^(true|false)$/i', $value)) { $value = Core\Convert::toBoolean($value); } else { if (preg_match('/^[+-]?(0|[1-9][0-9]*)((\\.[0-9]+)|([eE][+-]?(0|[1-9][0-9]*)))$/', $value)) { $value = Core\Convert::toDouble($value); } else { if (filter_var($value, FILTER_VALIDATE_INT) !== false) { $value = Core\Convert::toInteger($value); } else { $value = Core\Convert::toString($value); } } } } return $value; } }
/** * This method parses a custom node. * * @access protected * @param \SimpleXMLElement $node a reference to a custom node * @return \Unicity\Common\Mutable\ICollection a collection representing the data * in the soap file * @throws \Unicity\Throwable\Parse\Exception indicates that an unrecognized child * node was encountered */ protected function parseCustomElement(\SimpleXMLElement $node) { $children = $this->getElementChildren($node, null); if (count($children) > 0) { $list = new Common\Mutable\ArrayList(); $map = new Common\Mutable\HashMap(); foreach ($children as $child) { $name = $child->getName(); $value = $this->parseCustomElement($child); $temp = new Common\Mutable\HashMap(); $temp->putEntry($name, $value); $list->addValue($temp); $map->putEntry($name, $value); } return $list->count() > $map->count() || $this->directives->hasKey('expandableProperties') && $this->directives->getValue('expandableProperties')->hasValue($node->getName()) ? $list : $map; } else { $value = dom_import_simplexml($node)->textContent; $value = trim($value); if ($value == '') { $attributes = $node->attributes('xsi', true); if (isset($attributes['nil'])) { $nil = SOAP\Data\XML::valueOf($attributes['nil']); if (!SOAP\Data\XML\Syntax::isBoolean($nil)) { throw new Throwable\Parse\Exception('Unable to process SOAP XML. Expected a valid boolean token, but got ":token".', array(':token' => $nil)); } $value = strtolower($nil) != 'false' ? null : Core\Data\Undefined::instance(); } else { $value = Core\Data\Undefined::instance(); } } else { if (preg_match('/^(true|false)$/i', $value)) { $value = Core\Convert::toBoolean($value); } else { if (preg_match('/^[+-]?(0|[1-9][0-9]*)((\\.[0-9]+)|([eE][+-]?(0|[1-9][0-9]*)))$/', $value)) { $value = Core\Convert::toDouble($value); } else { if (filter_var($value, FILTER_VALIDATE_INT) !== false) { $value = Core\Convert::toInteger($value); } else { $value = Core\Convert::toString($value); } } } } return $value; } }
/** * This method rounds the value to the nearest 5 cents' place. * * @access public * @param double $value the value to be rounded * @return double the rounded value * * @see http://forums.devshed.com/php-development-5/round-nearest-5-cents-537959.html */ protected function roundToNearest5($value, $currency) { $currency = new Trade\Currency($currency); return round(Core\Convert::toDouble($value) / 5, $currency->getDefaultFractionDigits(), PHP_ROUND_HALF_UP) * 5; }