/** * This method puts the key/value mapping to the collection. * * @access public * @override * @param mixed $key the key to be mapped * @param mixed $value the value to be mapped * @return boolean whether the key/value pair was set */ public function putEntry($key, $value) { try { if (Core\Data\Undefined::instance()->__equals($value)) { return $this->removeKey($key); } else { $key = $this->getKey($key); if (isset($this->schema['properties'][$key])) { $definition = $this->schema['properties'][$key]; 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[$key] = $value; return true; } return false; } } catch (\Exception $ex) { return false; } }
/** * This method returns the JSON schema for the specified input. * * @access public * @static * @param mixed $schema the JSON schema to be loaded * @return array the JSON schema */ public static function resolveJSONSchema($schema) { if (is_string($schema)) { $components = preg_split('/(\\\\|_)+/', trim($schema, '\\')); $file = Bootstrap::rootPath() . implode(DIRECTORY_SEPARATOR, $components) . '.json'; $schema = Config\JSON\Reader::load(new IO\File($file))->read(); } $schema = Core\Convert::toDictionary($schema); if (isset($schema['$ref'])) { $result = MappingService\Data\Model\JSON\Helper::resolveJSONSchema($schema['$ref']); if (isset($schema['properties'])) { $result = array_merge($result, $schema['properties']); } return $result; } return $schema; }
/** * 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)); }