lithium\net\http\Media::negotiate()

public static method

Performs content-type negotiation on a Request object, by iterating over the accepted types in sequence, from most preferred to least, and attempting to match each one against a content type defined by Media::type(), until a match is found. If more than one defined type matches for a given content type, they will be checked in the order they were added (usually, this corresponds to the order they were defined in the application bootstrapping process).

Parameters

  • \lithium\action\Request $request

    The request which contains the details of the request to be content-negotiated.

Returns

string|null

Returns the first matching type name, i.e. 'html' or 'json'. When no matching type is found returns null.

Source

	public static function negotiate($request) {
		$self = get_called_class();

		$match = function($name) use ($self, $request) {
			if (($cfg = $self::type($name)) && $self::match($request, compact('name') + $cfg)) {
				return true;
			}
			return false;
		};

		if (($type = $request->type) && $match($type)) {
			return $type;
		}

		foreach ($request->accepts(true) as $type) {
			if (!$types = (array) static::_types($type)) {
				continue;
			}
			foreach ($types as $name) {
				if (!$match($name)) {
					continue;
				}
				return $name;
			}
		}
	}