Пример #1
0
 /**
  * This method iterates over each record in the file, yielding each item to the procedure function.
  *
  * @access public
  * @param callable $procedure                               the procedure function to be used
  * @throws Throwable\Parse\Exception                        indicates that an invalid record was
  *                                                          encountered
  */
 public function each(callable $procedure)
 {
     $self = $this;
     IO\FileReader::read($this->file, function ($reader, $data, $index) use($self, $procedure) {
         $line = trim($data);
         if (strlen($line) > 0) {
             if ($index == 0 && $self->bom) {
                 $line = preg_replace('/^' . pack('H*', 'EFBBBF') . '/', '', $line);
             }
             if (!preg_match('/^\\s*#.*$/', $line)) {
                 if (!preg_match('/^[^=]+=.+$/', $line)) {
                     throw new Throwable\Parse\Exception('Unable to parse file. File ":uri" contains an invalid pattern on line :line.', array(':uri' => $self->file, ':line' => $index));
                 }
                 $position = strpos($line, '=');
                 $key = trim(substr($line, 0, $position));
                 $value = trim(substr($line, $position + 1));
                 $type = isset($self->schema[$key]) ? $self->schema[$key] : 'string';
                 $value = Core\Convert::changeType($value, $type);
                 $record = new Common\Mutable\HashMap();
                 $record->putEntry($key, $value);
                 $procedure($record);
             }
         }
     });
 }
Пример #2
0
 /**
  * This method loads the specified file for reading.
  *
  * @access public
  * @static
  * @param Config\Reader $reader                             the config reader to use
  * @param boolean $case_sensitive                           whether keys are to be case sensitive
  * @param string $path                                      the path to the value to be returned
  * @return mixed                                            the resource as a collection
  */
 public static function unmarshal(Config\Reader $reader, $case_sensitive = true, $path = null)
 {
     if ($path !== null && !$case_sensitive) {
         $path = strtolower($path);
     }
     return static::useCollections($reader->read($path), Core\Convert::toBoolean($case_sensitive));
 }
Пример #3
0
 /**
  * This method iterates over each record in the file, yielding each item to the procedure function.
  *
  * @access public
  * @param callable $procedure                               the procedure function to be used
  * @throws Throwable\Parse\Exception                        indicates that an invalid record was
  *                                                          encountered
  */
 public function each(callable $procedure)
 {
     $self = $this;
     IO\FileReader::read($this->file, function ($reader, $data, $index) use($self, $procedure) {
         $line = trim($data);
         if (strlen($line) > 0) {
             if ($index == 0 && $self->bom) {
                 $line = preg_replace('/^' . pack('H*', 'EFBBBF') . '/', '', $line);
             }
             $query_string = parse_url($line, PHP_URL_QUERY);
             $properties = array();
             parse_str($query_string, $properties);
             if (!empty($properties)) {
                 $source_encoding = $self->encoder !== null ? call_user_func($self->encoder . "::getEncoding", $properties) : $self->encoding[0];
                 $target_encoding = $self->encoding[1];
                 foreach ($properties as $key => &$value) {
                     $value = urldecode($value);
                     $value = Core\Data\Charset::encode($value, $source_encoding, $target_encoding);
                     $type = isset($self->schema[$key]) ? $self->schema[$key] : 'string';
                     $value = Core\Convert::changeType($value, $type);
                     $record = new Common\Mutable\HashMap();
                     $record->putEntry($key, $value);
                     $procedure($record);
                 }
             }
         }
     });
 }
Пример #4
0
 /**
  * This method returns whether the logger is enabled.
  *
  * @access public
  * @return boolean                                          whether the logger is enabled
  */
 public function isEnabled()
 {
     if ($this->policy->hasKey('enabled')) {
         Core\Convert::toBoolean($this->policy->getValue('enabled'));
     }
     return true;
 }
Пример #5
0
 /**
  * This method resets the task.
  *
  * @access public
  */
 public function reset()
 {
     $id = Core\Convert::toString($this->policy->getValue('id'));
     if ($this->blackboard->hasKey($id)) {
         $this->blackboard->removeKey($id);
     }
 }
Пример #6
0
 /**
  * Initializes this bar code creator.
  *
  * @access public
  * @param string $data                                      the data to be encoded (up to 2K in size)
  * @param string $charset                                   the character set to be used
  * @param integer $width                                    the width of the barcode
  * @param integer $height                                   the height of the barcode
  * @param integer $margin                                   the margin around the barcode
  * @throws Throwable\InvalidArgument\Exception              indicates that the data could not
  *                                                          be encoded
  */
 public function __construct($data, $charset = 'UTF-8', $width = 150, $height = 150, $margin = 4)
 {
     $this->data = Core\Convert::toString($data);
     $this->encoding = $charset;
     $this->size = min($width, $height);
     $this->margin = $margin;
 }
Пример #7
0
 /**
  * This constructor initializes the property to an undefined value.
  *
  * @access public
  * @param mixed $data                                       any data to pre-set
  */
 public function __construct($data = array())
 {
     $data = Core\Convert::toDictionary($data);
     $data = array_change_key_case($data, CASE_LOWER);
     foreach (get_object_vars($this) as $name => $value) {
         $this->{$name} = array_key_exists($name, $data) ? $data[$name] : Core\Data\Undefined::instance();
     }
 }
Пример #8
0
 /**
  * This constructor instantiates the class.
  *
  * @access public
  * @param string $string                                    the data source to be used
  * @throws Throwable\InvalidArgument\Exception              indicates a data type mismatch
  */
 public function __construct($string)
 {
     $this->buffer = Core\Convert::toString($string);
     // TODO preg_replace('/\R/u', "\n", $string);
     $this->length = strlen($this->buffer);
     $this->mark = 0;
     $this->position = 0;
 }
Пример #9
0
 /**
  * This constructor initializes the property to an undefined value.
  *
  * @access public
  * @param mixed $data                                       any data to pre-set
  */
 public function __construct($data = array())
 {
     $properties = get_object_vars($this);
     $data = Core\Convert::toDictionary($data);
     foreach ($properties as $name => $value) {
         $this->{$name} = array_key_exists($name, $data) ? $data[$name] : Core\Data\Undefined::instance();
     }
 }
 /**
  * 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;
 }
Пример #11
0
 /**
  * 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;
 }
Пример #12
0
 /**
  * Initializes this barcode creator.
  *
  * @access public
  * @param string $data                                      the data string to be encoded
  * @param char $charset                                     the character to be used
  * @throws Throwable\InvalidArgument\Exception              indicates that the data could not
  *                                                          be encoded
  */
 public function __construct($data, $charset = 'C')
 {
     $data = Core\Convert::toString($data);
     if (!preg_match('/^[abc]$/i', $charset)) {
         throw new Throwable\InvalidArgument\Exception('Invalid character set declared. Expected either an "A", "B", or "C", but got ":charset".', array(':charset' => $charset));
     }
     $this->data = strtoupper($data);
     $this->charset = strtoupper($charset);
 }
Пример #13
0
 /**
  * Initializes this barcode creator.
  *
  * @access public
  * @param string $data                                      the data string to be encoded
  * @param array $properties                                 the properties used when rendering
  *                                                          the barcode
  * @throws Throwable\InvalidArgument\Exception              indicates that the data could not
  *                                                          be encoded
  */
 public function __construct($data, array $properties = array())
 {
     $data = Core\Convert::toString($data);
     if (!preg_match('/^[- *$%.\\/+a-z0-9]+$/i', $data)) {
         throw new Throwable\InvalidArgument\Exception('Invalid character in data string. Expected an data in the Code 39 subset, but got ":data" value.', array(':data' => $data));
     }
     $this->data = strtoupper($data);
     $this->properties = $properties;
 }
Пример #14
0
 /**
  * This constructor instantiates the class.
  *
  * @access public
  * @param string $string                                    the data source to be used
  * @throws Throwable\InvalidArgument\Exception              indicates a data type mismatch
  */
 public function __construct($string, $encoding = 'UTF-8')
 {
     $this->buffer = Core\Convert::toString($string);
     // TODO preg_replace('/\R/u', "\n", $string);
     $this->encoding = $encoding;
     $this->length = mb_strlen($this->buffer, $this->encoding);
     $this->mark = 0;
     $this->position = 0;
 }
Пример #15
0
 /**
  * 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)
 {
     $limit = Core\Convert::toInteger($this->policy->getValue('limit'));
     if ($this->calls < $limit) {
         $status = BT\Task\Handler::process($this->task, $exchange);
         $this->calls++;
         return $status;
     }
     return BT\Task\Status::FAILED;
 }
Пример #16
0
 /**
  * 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;
 }
Пример #17
0
 /**
  * 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;
     $zip = Core\Convert::toInteger($order->shipToAddress->zip);
     $begin = Core\Convert::toInteger($this->policy->getValue('begin'));
     $end = Core\Convert::toInteger($this->policy->getValue('end'));
     if ($zip >= $begin && $zip <= $end) {
         return BT\Task\Status::SUCCESS;
     }
     return BT\Task\Status::FAILED;
 }
Пример #18
0
 /**
  * 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)
 {
     $max_count = Core\Convert::toInteger($this->policy->getValue('max_count'));
     if ($this->counter < $max_count) {
         $this->counter++;
         return BT\Task\Status::ACTIVE;
     }
     $this->counter = 0;
     $status = BT\Task\Handler::process($this->task, $exchange);
     return $status;
 }
Пример #19
0
 /**
  * Initializes this barcode creator.
  *
  * @access public
  * @param string $data                                      the data string to be encoded
  * @throws Throwable\InvalidArgument\Exception              indicates that the data could not
  *                                                          be encoded
  */
 public function __construct($data)
 {
     $data = Core\Convert::toString($data);
     if (!preg_match('/^[0-9]{11}[0-9]?$/i', $data)) {
         throw new Throwable\InvalidArgument\Exception('Invalid character in data string. Expected an data in the UPC-A subset, but got ":data" value.', array(':data' => $data));
     }
     if (strlen($data) == 12) {
         $data = substr($data, 0, -1);
     }
     $this->data = strtoupper($data);
 }
Пример #20
0
 /**
  * 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)
 {
     $shuffle = Core\Convert::toBoolean($this->policy->getValue('shuffle'));
     if ($shuffle) {
         $this->tasks->shuffle();
     }
     $index = Core\Convert::toInteger($this->policy->getValue('index'));
     if ($this->tasks->hasIndex($index)) {
         return BT\Task\Handler::process($this->tasks->getValue($index), $exchange);
     }
     return BT\Task\Status::ERROR;
 }
Пример #21
0
 /**
  * This constructor initializes the class with the specified parameters.
  *
  * @access public
  * @param Common\Mutable\IMap $blackboard                   the blackboard to be used
  * @param Common\Mutable\IMap $policy                       the policy associated with the task
  */
 public function __construct(Common\Mutable\IMap $blackboard = null, Common\Mutable\IMap $policy = null)
 {
     parent::__construct($blackboard, $policy);
     if ($this->policy->hasKey('status')) {
         $status = $this->policy->getValue('status');
         if (is_string($status)) {
             $status = BT\Task\Status::valueOf($status);
         }
         $this->policy->putEntry('status', Core\Convert::toInteger($status));
     } else {
         $this->policy->putEntry('status', BT\Task\Status::SUCCESS);
     }
 }
Пример #22
0
 /**
  * This method adds a value to the query string.
  *
  * @access public
  * @param string $key                                       the key to be used
  * @param mixed $value                                      the value to be added
  * @return string                                           the query string segment
  */
 public function addValue($key, $value)
 {
     if (!Core\Data\Undefined::instance()->__equals($value)) {
         $field = preg_replace('/\\[[^\\]]*\\]/', '[]', $key);
         $type = isset($this->metadata['schema'][$field]) ? $this->metadata['schema'][$field] : 'string';
         $value = Core\Convert::changeType($value, $type);
         $value = Core\Convert::toString($value);
         $value = Core\Data\Charset::encode($value, $this->metadata['encoding'][0], $this->metadata['encoding'][1]);
         $value = urlencode($value);
         return $key . '=' . $value;
     }
     return null;
 }
Пример #23
0
 /**
  * This method recursively flattens each key/value pair.
  *
  * @access private
  * @static
  * @param array &$buffer                                    the array buffer
  * @param string $key                                       the key to be used
  * @param mixed $value                                      the value to be added
  * @param boolean $stringify                                whether to stringify the value
  */
 private static function _flatten(&$buffer, $key, $value, $stringify)
 {
     if (is_array($value)) {
         foreach ($value as $k => $v) {
             static::_flatten($buffer, $key . '.' . Core\Convert::toString($k), $v, $stringify);
         }
     } else {
         if ($stringify) {
             $buffer[$key] = $value == null || is_object($value) && $value instanceof Core\Data\Undefined ? '' : Core\Convert::toString($value);
         } else {
             $buffer[$key] = $value;
         }
     }
 }
Пример #24
0
 /**
  * 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)
 {
     $delay = Core\Convert::toInteger($this->policy->getValue('delay')) / 1000;
     // milliseconds => seconds
     $deltaT = microtime(true) - $this->start_time;
     if ($deltaT >= $delay) {
         $duration = Core\Convert::toInteger($this->policy->getValue('duration')) / 1000;
         // milliseconds => seconds
         if ($deltaT < $delay + $duration) {
             return BT\Task\Handler::process($this->task, $exchange);
         }
     }
     return BT\Task\Status::INACTIVE;
 }
Пример #25
0
 /**
  * This method returns an object matching the description specified by the element.
  *
  * @access public
  * @param Spring\Object\Parser $parser                      a reference to the parser
  * @param \SimpleXMLElement $element                        the element to be parsed
  * @return mixed                                            an object matching the description
  *                                                          specified by the element
  * @throws Throwable\Parse\Exception                        indicates that a problem occurred
  *                                                          when parsing
  */
 public function getObject(Spring\Object\Parser $parser, \SimpleXMLElement $element)
 {
     $children = $parser->getElementChildren($element, null);
     if (!empty($children)) {
         $value = '';
         foreach ($children as $child) {
             $name = $parser->getElementPrefixedName($child);
             switch ($name) {
                 case 'spring:null':
                     $value = $parser->getObjectFromElement($child);
                     break;
                 default:
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" has invalid child node ":child"', array(':tag' => 'value', ':child' => $name));
                     break;
             }
         }
         if (is_string($value)) {
             return Core\Data\Charset::encode($value, $parser->getEncoding($parser->getResource()), Core\Data\Charset::UTF_8_ENCODING);
         }
         return $value;
     } else {
         $attributes = $element->attributes();
         $value = dom_import_simplexml($element)->textContent;
         if (isset($attributes['type'])) {
             $type = $parser->valueOf($attributes['type']);
             if (!$parser->isPrimitiveType($type)) {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type));
             }
             $value = Core\Convert::changeType($value, $type);
         }
         if (is_string($value)) {
             $attributes = $element->attributes('xml', true);
             if (isset($attributes['space'])) {
                 $space = $parser->valueOf($attributes['space']);
                 if (!$parser->isSpacePreserved($space)) {
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid space token, but got ":token".', array(':token' => $space));
                 }
             } else {
                 $value = trim($value);
             }
         }
         if (is_string($value)) {
             return Core\Data\Charset::encode($value, $parser->getEncoding($parser->getResource()), Core\Data\Charset::UTF_8_ENCODING);
         }
         return $value;
     }
 }
Пример #26
0
 /**
  * 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)
 {
     $count = $this->tasks->count();
     if ($count > 0) {
         $shuffle = Core\Convert::toBoolean($this->policy->getValue('shuffle'));
         if ($shuffle) {
             $this->tasks->shuffle();
         }
         $inactivesCt = 0;
         $successesCt = 0;
         $successesMax = min(Core\Convert::toInteger($this->policy->getValue('successes')), $count);
         $failuresCt = 0;
         $failuresMax = min(Core\Convert::toInteger($this->policy->getValue('failures')), $count);
         foreach ($this->tasks as $task) {
             $status = BT\Task\Handler::process($task, $exchange);
             switch ($status) {
                 case BT\Task\Status::INACTIVE:
                     $inactivesCt++;
                     break;
                 case BT\Task\Status::ACTIVE:
                     break;
                 case BT\Task\Status::SUCCESS:
                     $successesCt++;
                     if ($successesCt >= $successesMax) {
                         return BT\Task\Status::SUCCESS;
                     }
                     break;
                 case BT\Task\Status::FAILED:
                     $failuresCt++;
                     if ($failuresCt >= $failuresMax) {
                         return BT\Task\Status::FAILED;
                     }
                     break;
                 case BT\Task\Status::ERROR:
                 case BT\Task\Status::QUIT:
                     return $status;
             }
         }
         if ($inactivesCt != $count) {
             return BT\Task\Status::ACTIVE;
         }
     }
     return BT\Task\Status::INACTIVE;
 }
Пример #27
0
 /**
  * This method recursively adds each entry as a property.
  *
  * @access protected
  * @param string &$buffer                                   the string buffer
  * @param string $key                                       the key to be used
  * @param mixed $value                                      the value to be added
  */
 protected function addProperty(&$buffer, $key, $value)
 {
     if (!is_array($value)) {
         if ($value == null || is_object($value) && $value instanceof Core\Data\Undefined) {
             $buffer .= $key . '=' . '';
         } else {
             $type = isset($this->metadata['schema'][$key]) ? $this->metadata['schema'][$key] : 'string';
             $datum = Core\Convert::changeType($value, $type);
             $datum = Core\Convert::toString($datum);
             $datum = Core\Data\Charset::encode($datum, $this->metadata['encoding'][0], $this->metadata['encoding'][1]);
             $buffer .= $key . '=' . $datum;
         }
         $buffer .= $this->metadata['eol'];
     } else {
         foreach ($value as $k => $v) {
             $this->addProperty($buffer, $key . '.' . Core\Convert::toString($k), $v);
         }
     }
 }
Пример #28
0
 /**
  * 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)
 {
     $shuffle = Core\Convert::toBoolean($this->policy->getValue('shuffle'));
     if ($shuffle) {
         $this->tasks->shuffle();
     }
     $inactives = 0;
     foreach ($this->tasks as $task) {
         $status = BT\Task\Handler::process($task, $exchange);
         if ($status == BT\Task\Status::INACTIVE) {
             $inactives++;
         } else {
             if ($status != BT\Task\Status::SUCCESS) {
                 return $status;
             }
         }
     }
     return $inactives < $this->tasks->count() ? BT\Task\Status::SUCCESS : BT\Task\Status::INACTIVE;
 }
Пример #29
0
 /**
  * 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)
 {
     $steps = Core\Convert::toInteger($this->policy->getValue('steps'));
     if ($this->policy->getValue('reverse')) {
         // direction
         for ($i = $steps - 1; $i >= 0; $i--) {
             $status = BT\Task\Handler::process($this->task, $exchange);
             if (!in_array($status, array(BT\Task\Status::SUCCESS, BT\Task\Status::FAILED, BT\Task\Status::ERROR, BT\Task\Status::QUIT))) {
                 return $status;
             }
         }
     } else {
         for ($i = 0; $i < $steps; $i++) {
             $status = BT\Task\Handler::process($this->task, $exchange);
             if (!in_array($status, array(BT\Task\Status::SUCCESS, BT\Task\Status::FAILED, BT\Task\Status::ERROR, BT\Task\Status::QUIT))) {
                 return $status;
             }
         }
     }
     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;
     $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;
 }