/** * 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 return the same object, but first checks if the value has been set. * * @access publiv * @return MappingService\Data\Field\Item the same object */ public function isRequired() { if ($this->info->type == 'string') { if (MappingService\Data\ToolKit::isEmpty($this->value)) { // TODO add logging } return $this; } else { if (MappingService\Data\ToolKit::isUnset($this->value)) { // TODO add logging } } return $this; }