Example #1
0
	/**
	 * Stores the specified field with the current instance.
	 *
	 * @param mixed $field the field data to store
	 */
	private function storeField($field) {
		try {
			// =================================================================
			// Source Data: SimpleXMLElement
			// =================================================================
			if($field instanceof \SimpleXMLElement) {
				if(!isset($field->attributes()->id) || $field->attributes()->id == '') {
					$field->addAttribute('id', $this->_properties->id . '_' . (string) $field->attributes()->name);
				}
				
				$type = '';
				list($type) = explode(':', (string) $field->attributes()->type);
				$fieldClass = \Bedrock\Common\Form::getMapping($type);
				
				if($fieldClass) {
					$this->_fields[(string) $field->attributes()->name] =  new $fieldClass($field);
				}
				else {
					$this->_fields[(string) $field->attributes()->name] = new \Bedrock\Common\Form\Field($field);
				}
			}
			
			// =================================================================
			// Source Data: Config
			// =================================================================
			elseif($field instanceof \Bedrock\Common\Config) {
				if(!isset($field->id) || $field->id == '') {
					$field->id = $this->_properties->id . '_' . $field->name;
				}
				
				$type = '';
				list($type) = explode(':', (string) $field->type);
				$fieldClass = \Bedrock\Common\Form::getMapping($type);
				
				if($fieldClass) {
					$this->_fields[$field->name] = new $fieldClass($field);
				}
				else {
					$this->_fields[$field->name] = new \Bedrock\Common\Form\Field($field);
				}
			}
			
			// =================================================================
			// Source Data: Array
			// =================================================================
			elseif(is_array($field)) {
				if(!isset($field['id']) || $field['id'] == '') {
					$field['id'] = $this->_properties->id . '_' . $field['name'];
				}
				
				$type = '';
				list($type) = explode(':', (string) $field['type']);
				$fieldClass = \Bedrock\Common\Form::getMapping($type);
				
				if($fieldClass) {
					$this->_fields[$field['name']] = new $fieldClass($field);
				}
				else {
					$this->_fields[$field['name']] = new \Bedrock\Common\Form\Field($field);
				}
			}
			
			// =================================================================
			// Source Data: Unsupported
			// =================================================================
			else {
				throw new \Bedrock\Common\Form\Exception('Invalid data provided, field could not be stored.');
			}
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
		}
	}