/** * 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); } }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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); }
/** * 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; }
/** * 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); }
/** * 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; }
/** * 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; } } }
/** * This method returns ISO data for the specified currency. * * @access protected * @param string $currency the currency to be queried * @return array the data */ protected function getCurrencyData($currency) { if (is_string($currency) || is_integer($currency)) { $currency = Core\Convert::toString($currency); $currency = preg_replace('/\\s+/', ' ', trim($currency)); $length = strlen($currency); if ($length == 3) { $records = DB\SQL::select('locale')->from('Currencies')->where('CurrencyNumeric3', '=', $currency)->where('CurrencyAlpha3', '=', strtoupper($currency), 'OR')->limit(1)->query(); if ($records->is_loaded()) { return $records->current(); } } else { $records = DB\SQL::select('locale')->before(function (DB\Connection\Driver $driver) { $driver->get_resource()->createFunction('TRANSLITERATE', function ($string) { return Common\String::transliterate($string)->__toString(); }, 1); })->from('Currencies')->where(DB\SQL::expr('LOWER([CurrencyName])'), '=', strtolower($currency))->where(DB\SQL::expr("LOWER(TRANSLITERATE([CurrencyName]))"), '=', Common\String::transliterate($currency)->toLowerCase()->__toString(), 'OR')->where(DB\SQL::expr('LOWER([CurrencySymbol])'), '=', strtolower($currency))->limit(1)->query(); if ($records->is_loaded()) { return $records->current(); } $records = DB\SQL::select('locale')->before(function (DB\Connection\Driver $driver) { $driver->get_resource()->createFunction('PREG_REPLACE', 'preg_replace', 3); })->from('Currencies')->where(DB\SQL::expr("LOWER(PREG_REPLACE('/[^a-z]/i', '', [CurrencyName]))"), '=', strtolower(preg_replace('/[^a-z]/i', '', $currency)))->limit(1)->query(); if ($records->is_loaded()) { return $records->current(); } $records = DB\SQL::select('locale')->before(function (DB\Connection\Driver $driver) { $driver->get_resource()->createFunction('TRANSLITERATE', function ($string) { return Common\String::transliterate($string)->__toString(); }, 1); })->from('Currencies')->where(DB\SQL::expr('LOWER([CurrencyName])'), 'LIKE', '%' . strtolower($currency) . '%')->where(DB\SQL::expr("LOWER(TRANSLITERATE([CurrencyName]))"), 'LIKE', '%' . Common\String::transliterate($currency)->toLowerCase() . '%', 'OR')->limit(1)->query(); if ($records->is_loaded()) { return $records->current(); } $records = DB\SQL::select('locale')->before(function (DB\Connection\Driver $driver) { $driver->get_resource()->createFunction('SOUNDEX', 'soundex', 1); })->from('Currencies')->where(DB\SQL::expr("SOUNDEX([CurrencyName])"), '=', DB\SQL::expr("SOUNDEX('{$currency}')"))->limit(1)->query(); if ($records->is_loaded()) { return $records->current(); } } } return array('CurrencyID' => 0, 'CurrencyName' => null, 'CurrencyNumeric3' => null, 'CurrencyAlpha3' => null, 'CurrencySymbol' => null, 'CurrencyDecimals' => -1); }
/** * 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); } } }
/** * This method sets the message's id. * * @access public * @param string $id * @throws Throwable\Parse\Exception the message id */ public function setMessageId($id) { if ($this->id !== null) { $this->id = Core\Convert::toString($id); } else { $this->id = $this->__hashCode(); } }
/** * This method adds a dictionary of nodes to the DOM structure. * * @access protected * @param \SimpleXMLElement $root a reference to the root node * @param \SimpleXMLElement $node a reference to the current node * @param mixed $data the data to be added to the node * @param string $xmlns the xml namespace to be used */ protected function addDictionary(&$root, &$node, &$data, $xmlns) { foreach ($data as $name => $value) { $type = $value !== null ? gettype($value) : 'NULL'; switch ($type) { case 'array': $child = $node->addChild($name, null, $xmlns); if (static::isDictionary($value)) { $this->addDictionary($root, $child, $value, $xmlns); } else { $this->addArray($root, $child, $value, $xmlns); } break; case 'object': if ($value instanceof Core\Data\Undefined) { // do nothing } break; case 'NULL': $child = $node->addChild($name, null, $xmlns); $child->addAttribute('xsi:nil', 'true', 'http://www.w3.org/2001/XMLSchema-instance'); break; default: $datum = Core\Convert::toString($value); $datum = Core\Data\Charset::encode($datum, $this->metadata['encoding'][0], $this->metadata['encoding'][1]); $datum = SOAP\Data\XML::entities($datum); $node->addChild($name, $datum, $xmlns); break; } } }
/** * This method attempts to resolve the value as a string 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 resolveStringValue($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 string, but got :type.', array(':type' => Core\DataType::info($value)->type)); } return $value; } $value = Core\Convert::toString($value); if (isset($definition['filters'])) { foreach ($definition['filters'] as $filter) { $value = Core\Convert::toString(call_user_func($filter, $value)); } } if (isset($definition['pattern'])) { if (!preg_match($definition['pattern'], $value)) { throw new Throwable\Runtime\Exception('Invalid value defined. Expected a value matching pattern ":pattern", but got :value.', array(':pattern' => $definition['pattern'], ':value' => $value)); } } if (isset($definition['minLength'])) { if (strlen($value) < $definition['minLength']) { $value = str_pad($value, $definition['minLength'], ' ', STR_PAD_RIGHT); } } if (isset($definition['maxLength'])) { if (strlen($value) > $definition['maxLength']) { $value = substr($value, 0, $definition['maxLength']); // TODO log string was truncated } } 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 creates a temporary buffer for the source data. * * @access protected * @static * @param string $data the data to be buffered * @return string the URI of the temp buffer */ protected static function buffer($data) { $uri = tempnam(IO\TempBuffer::directory(), 'mio-'); file_put_contents($uri, Core\Convert::toString($data)); return $uri; }
/** * This method returns the barcode represented as a string. * * @access public * @return string the barcode as a string */ public function __toString() { return Core\Convert::toString($this->render()); }
/** * This method serializes a value. * * @access protected * @param mixed $value the value to be serialized * @return string the serialized key */ protected function getSerializedValue($value) { $type = gettype($value); switch ($type) { case 'array': $array = 'array(' . $this->metadata['eol']; foreach ($value as $k => $v) { $array .= $this->getSerializedKey($k) . ' => ' . $this->getSerializedValue($v) . ',' . $this->metadata['eol']; } $array .= ')'; return $array; case 'NULL': return 'null'; case 'object': if ($value instanceof Core\Data\Undefined) { return '\\Unicity\\Core\\Data\\Undefined::instance()'; } return 'null'; case 'boolean': case 'double': case 'integer': return Core\Convert::toString($value); case 'string': case 'resource': case 'unknown type': default: $value = Core\Convert::toString($value); $value = Core\Data\Charset::encode($value, $this->metadata['encoding'][0], $this->metadata['encoding'][1]); return "'" . addslashes($value) . "'"; } }
/** * This method formats an array of values using CSV conventions. * * @access public * @static * @param array $fields the values to be formatted * @param string $delimiter the delimiter to be used * @param string $enclosure the enclosure to be used * @param string $escape the escape character to be used * @param string $eol the end-of-line character to be used * @param array $encoding the character set encoding to be used * @return string the formatted string * * @see http://php.net/manual/en/function.fputcsv.php#77866 */ public static function format(array $fields, $delimiter = ',', $enclosure = '"', $escape = '\\', $eol = "\n", array $encoding = null) { $buffer = ''; if (empty($encoding)) { $encoding = array(Core\Data\Charset::UTF_8_ENCODING, Core\Data\Charset::UTF_8_ENCODING); } foreach ($fields as $field) { $value = Core\Data\Charset::encode($field, $encoding[0], $encoding[1]); if ($enclosure != '' && (strpos($value, $delimiter) !== false || strpos($value, $enclosure) !== false || strpos($value, "\n") !== false || strpos($value, "\r") !== false || strpos($value, "\t") !== false || strpos($value, ' ') !== false)) { $literal = $enclosure; $escaped = 0; $length = strlen($value); for ($i = 0; $i < $length; $i++) { if ($value[$i] == $escape) { $escaped = 1; } else { if (!$escaped && $value[$i] == $enclosure) { $literal .= $enclosure; } else { $escaped = 0; } } $literal .= $value[$i]; } $literal .= $enclosure; $buffer .= $literal . $delimiter; } else { $buffer .= Core\Convert::toString($value) . $delimiter; } } $buffer = substr($buffer, 0, -1) . $eol; return $buffer; }
/** * This method returns the value represented as a string. * * @access public * @return string the value represented as a string */ public function __toString() { return Core\Convert::toString($this->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 parses a "string" node. * * @access protected * @param \DOMElement $node a reference to the "string" node * @return \Unicity\Common\String the value as a string */ protected function parseStringElement(\DOMElement $node) { $value = $node->textContent; $value = Core\Convert::toString($value); $value = Core\Data\XML::toUnicodeString($value); $value = Core\Data\Charset::encode($value, $this->metadata['encoding'][0], $this->metadata['encoding'][1]); $string = new Common\String($value); return $string; }
/** * This method sets the title to be associated with this task. * * @access public * @param string $title the title to be associated * with this task */ public function setTitle($title) { $this->title = Core\Convert::toString($title); }
/** * This method appends the path with the specified name. * * @access protected * @static * @param string $path the path to be appended * @param mixed $name the name of the next level * @return string a new path with name of next level * appended */ protected static function buildPath($path, $name) { if (empty($path)) { return Core\Convert::toString($name); } return $path . '.' . Core\Convert::toString($name); }
/** * This method returns the data as an XML string. * * @access protected * @param \DOMDocument $document the XML DOM document * @param \DOMElement $element the XML DOM element * @param mixed $data the data as an XML string */ protected function toXML($document, $element, $data) { if (is_array($data)) { if (Common\Collection::isDictionary($data)) { foreach ($data as $node => $value) { $child = $document->createElement($node); $element->appendChild($child); $this->toXML($document, $child, $value); } } else { foreach ($data as $value) { $this->toXML($document, $element, $value); } } } else { if (is_string($data) && preg_match('/^<!CDATA\\[.*\\]\\]>$/', $data)) { $data = substr($data, 8, strlen($data) - 11); $child = $document->createCDATASection($data); $element->appendChild($child); } else { if ($data !== null) { $child = $document->createTextNode(Core\Convert::toString($data)); $element->appendChild($child); } } } }
/** * 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; } }