Oihana PHP Arango

assertLanguageCode.php

Table of Contents

Functions

assertLanguageCode()  : void
Asserts that a value is a language code safe to interpolate into a query, throwing when it is not. The language counterpart of {@see assertAttributeName()}, and it exists for the same reason: a language tag names an attribute of the translations object, so it is written verbatim into the query string and can never be bound.

Functions

assertLanguageCode()

Asserts that a value is a language code safe to interpolate into a query, throwing when it is not. The language counterpart of {@see assertAttributeName()}, and it exists for the same reason: a language tag names an attribute of the translations object, so it is written verbatim into the query string and can never be bound.

assertLanguageCode(mixed $value[, bool $fromRequest = false ]) : void

The predicate itself lives in oihana/php-controllers (isLanguageCode()) — a language tag is not an ArangoDB notion. What stays here is the part that is one: deciding who to blame, below.

Who is blamed depends on where the tag came from, exactly as for an attribute name. A declared default language (Arango::DEFAULT_LANG, the site or model fallback) is the consumer's own code and no request can fix it: it refuses with a plain ValidationException, answered with a 500. A requested language (Arango::LANG, the ?lang= parameter) is told it wrote something the API cannot read — a 400.

⚠ A controller already filters ?lang= against its supported languages whitelist, so a request rarely reaches this guard. A consumer calling the model directly bypasses that controller, which is precisely why the guard lives here too.

Parameters
$value : mixed

The language tag to validate.

$fromRequest : bool = false

Whether the tag was supplied by the request rather than declared in code.

Tags
example
use function oihana\arango\db\helpers\assertLanguageCode;

assertLanguageCode( 'fr' );                        // ok
assertLanguageCode( 'fr" || 1==1' );               // throws ValidationException
assertLanguageCode( $requested , fromRequest: true ); // throws RequestValidationException
throws
ValidationException

When $value is not a safe language tag — a RequestValidationException (400) when it came from a request.

since
1.0.0
author

Marc Alcaraz

On this page

Search results