/** * This constructor initializes the class with the specified value. * * @access public * @param string $country the country to be encapsulated */ public function __construct($country) { $country = Core\Convert::toString($country); $info = Core\DataType::info($country); $hash = $info->hash; if (!isset(static::$countries[$hash])) { static::$countries[$hash] = $this->getCountryData($country); } $this->country = static::$countries[$hash]; }
/** * This constructor initializes the class with the specified value. * * @access public * @param string $name the name identifying the value * @param mixed $value the value to be wrapped */ public function __construct($name, $value) { $info = Core\DataType::info($value); if ($info->class == 'object' && $info->type != 'string') { throw new Throwable\InvalidArgument\Exception('Invalid argument specified. Expected a primitive or unknown, but got ":type"', array(':type', $info->type)); } $this->info = $info; $this->name = $name; $this->value = $value; }
/** * 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) { $object = new Common\Mutable\ArrayList(); $children = $parser->getElementChildren($element, null); if (!empty($children)) { foreach ($children as $child) { $task = $parser->getObjectFromElement($child); if (!$task instanceof BT\Task) { throw new Throwable\Parse\Exception('Invalid type defined. Expected a task, but got an element of type ":type" instead.', array(':type' => Core\DataType::info($object)->type)); } $object->addValue($task); } } return $object; }
/** * This method generates the hash key for the specified value. * * @access protected * @static * @param mixed $value the value to be hashed * @return string the hash key for the specified value */ protected static function hashKey($value) { return Core\DataType::info($value)->hash; }
/** * This method returns the last index of the specified value in the list. * * @access public * @param mixed $value the value to be located * @return integer the last index of the specified value */ public function lastIndexOf($value) { for ($i = $this->count(); $i >= 0; $i--) { if (Core\DataType::info($value)->hash == Core\DataType::info($this->getValue($i))->hash) { return $i; } } return -1; }
/** * 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 processes the key for use by the hash map. * * @access protected * @param mixed $key the key to be processed * @return string the key to be used * @throws \Unicity\Throwable\InvalidArgument\Exception indicates an invalid key was specified */ protected function getKey($key) { if (is_object($key) && $key instanceof Common\String) { return $key->__toString(); } else { if (!(is_integer($key) || is_string($key))) { throw new Throwable\InvalidArgument\Exception('Invalid key specified. Expected integer or string, but got ":type" instead.', array(':type' => Core\DataType::info($key)->type)); } } return $key; }
/** * This method logs any changes between the source and target values. * * @access protected * @param mixed $source the source value to be evaluated * @param mixed $target the target value to be evaluated * @param string $path the current path * @param Common\Mutable\IList $log a reference to the log */ protected function compareValues($source, $target, $path, Common\Mutable\IList $log) { $source_info = Core\DataType::info($source); $target_info = Core\DataType::info($target); if ($source_info->hash != $target_info->hash) { if ($source_info->type != $target_info->type) { $log->addValue(array('body' => strtr('Target value is of ":target" type, but source value is of ":source" type.', array(':source' => $source_info->type, ':target' => $target_info->type)), 'level' => Log\Level::warning()->__name(), 'path' => $path, 'time' => date('c'))); } else { $log->addValue(array('body' => strtr('Target value ":target" is different from source value ":source".', array(':source' => Core\Convert::toString($source), ':target' => Core\Convert::toString($target))), 'level' => Log\Level::warning()->__name(), 'path' => $path, 'time' => date('c'))); } } }
/** * This method replaces the value at the specified index. * * @access public * @param integer $index the index of the element to be set * @param mixed $value the value to be set * @return boolean whether the value was set * @throws Throwable\InvalidArgument\Exception indicates that index must be an integer */ public function setValue($index, $value) { if (is_integer($index)) { if (array_key_exists($index, $this->elements)) { if (isset($this->schema['items'][0])) { $definition = $this->schema['items'][0]; if (isset($definition['type'])) { switch ($definition['type']) { case 'array': $value = MappingService\Data\Model\JSON\Helper::resolveArrayValue($value, $definition); break; case 'boolean': $value = MappingService\Data\Model\JSON\Helper::resolveBooleanValue($value, $definition); break; case 'integer': $value = MappingService\Data\Model\JSON\Helper::resolveIntegerValue($value, $definition); break; case 'number': $value = MappingService\Data\Model\JSON\Helper::resolveNumberValue($value, $definition); break; case 'null': $value = MappingService\Data\Model\JSON\Helper::resolveNullValue($value, $definition); break; case 'object': $value = MappingService\Data\Model\JSON\Helper::resolveObjectValue($value, $definition); break; case 'string': $value = MappingService\Data\Model\JSON\Helper::resolveStringValue($value, $definition); break; } } } $this->elements[$index] = $value; return true; } else { if ($index == $this->count()) { $this->addValue($value); return true; } } return false; } throw new Throwable\InvalidArgument\Exception('Unable to set element. Expected an integer, but got ":index" of type ":type".', array(':index' => $index, ':type' => Core\DataType::info($index)->type)); }
/** * This method is called object's properties have been set. * * @access public */ public function afterPropertiesSet() { if ($this->targetClass !== null) { if (!method_exists($this->targetClass, $this->targetMethod)) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid callable, but got ":class" and ":method".', array(':class' => $this->targetClass, ':method' => $this->targetMethod)); } $this->callable = array($this->targetClass, $this->targetMethod); } else { if ($this->targetClass !== null) { if (!method_exists($this->targetObject, $this->targetMethod)) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid callable, but got ":class" and ":method".', array(':class' => Core\DataType::info($this->targetObject)->type, ':method' => $this->targetMethod)); } $this->callable = array($this->targetObject, $this->targetMethod); } else { if (in_array($this->targetMethod, array('eval'))) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid callable, but got ":method".', array(':method' => $this->targetMethod)); } $this->callable = $this->targetMethod; } } }
/** * This method returns the element's children. * * @access protected * @param \SimpleXMLElement $element the element to be parsed * @param string $namespace the namespace associated with * the children * @return array the element's children * * @see http://php.net/manual/en/class.simplexmlelement.php */ protected function getElementChildren(\SimpleXMLElement $element, $namespace = '') { if (is_string($namespace)) { if ($namespace != '') { return $element->children($namespace); } return $element->children(); } $children = array(); $namespaces = $element->getNamespaces(true); foreach ($namespaces as $namespace) { $elements = $element->children($namespace); foreach ($elements as $child) { $key = Core\DataType::info($child)->hash; $children[$key] = $child; } } /* $elements = $element->children(); foreach ($elements as $child) { $key = $child->getName(); $children[$key] = $child; } */ $children = array_values($children); return $children; }
/** * This method inserts a value at the specified index. * * @access public * @param integer $index the index where the value will be inserted at * @param mixed $value the value to be inserted * @return boolean whether the value was inserted * @throws Throwable\InvalidArgument\Exception indicates that index must be an integer * @throws Throwable\OutOfBounds\Exception indicates that no value exists at the * specified index * * @see http://www.justin-cook.com/wp/2006/08/02/php-insert-into-an-array-at-a-specific-position/ */ public function insertValue($index, $value) { if (!is_integer($index)) { throw new Throwable\InvalidArgument\Exception('Unable to insert value. :type is of the wrong data type.', array(':type' => Core\DataType::info($index)->type, ':value' => $value)); } $count = $this->count(); if ($index > $count) { throw new Throwable\OutOfBounds\Exception('Unable to insert value. Invalid index specified', array(':index' => $index, ':value' => $value)); } else { if ($index == $count) { $this->elements[$index] = $value; return true; } else { if ($index == 0) { array_unshift($this->elements, $value); return true; } else { array_splice($this->elements, $index, 0, array($value)); return true; } } } }