public function build($attributes = null) {

		if ($attributes instanceof \ElggEntity) {
			return $attributes;
		}

		if (is_numeric($attributes)) {
			return $this->get($attributes);
		}

		$attributes = (array) $attributes;

		if (!empty($attributes['guid'])) {
			return $this->get($attributes['guid']);
		}


		$type = elgg_extract('type', $attributes, 'object');
		$subtype = elgg_extract('subtype', $attributes, ELGG_ENTITIES_ANY_VALUE);

		unset($attributes['type']);
		unset($attributes['subtype']);

		$class = get_subtype_class($type, $subtype);
		if (class_exists($class)) {
			$entity = new $class();
		} else {
			switch ($type) {
				case 'object' :
					$entity = new \ElggObject();
					$entity->subtype = $subtype;
					break;

				case 'user' :
					$entity = new \ElggUser();
					$entity->subtype = $subtype;
					break;

				case 'group' :
					$entity = new \ElggGroup();
					$entity->subtype = $subtype;
					break;
			}
		}

		foreach ($attributes as $key => $value) {
			if (in_array($key, $this->getAttributeNames($entity))) {
				$entity->$key = $value;
			}
		}

		return $entity;
	}