ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ILIAS\UI\URLBuilder Class Reference

URLBuilder. More...

+ Collaboration diagram for ILIAS\UI\URLBuilder:

Public Member Functions

 __construct (URI $uri)
 
 withURI (URI $uri)
 Changes the base URI of the Builder. More...
 
 withFragment (?string $fragment)
 Change the fragment/hash part of the URL. More...
 
 acquireParameter (array $namespace, string $name, ?string $initial_value=null)
 Add a new parameter with a namespace and get its token for subsequent changes. More...
 
 acquireParameters (array $namespace, string ... $names)
 
 deleteParameter (URLBuilderToken $token)
 Delete an acquired parameter if the supplied token is valid. More...
 
 withParameter (URLBuilderToken $token, string|array $value)
 Change an acquired parameter's value if the supplied token is valid. More...
 
 renderTokens (array $tokens)
 Renders a Javascript Map of all given tokens. More...
 
 renderObject (array $tokens)
 Renders a Javascript URLBuilder object with changeable parameters for all given tokens. More...
 
 buildURI ()
 Get a URI representation of the full URL including query string and fragment/hash. More...
 

Data Fields

const URL_MAX_LENGTH = 8192
 A maximum length of 8192 characters should be safe to use in most browsers, even though longer URLs will be supported by some. More...
 
const SEPARATOR = '_'
 Separator for parts of a parameter's namespace. More...
 

Private Member Functions

 buildQuery ()
 Create the query part of the URL from all parameters Claimed parameters overwrite base parameters in array_merge(), numeric indizes of array-parameters are being removed to ensure continous numeration (p[1]=A&p[2]=B –> p[]=A&p[]=B). More...
 
 buildFragment ()
 Create the fragment/hash part of the URL. More...
 
 parameterExists (string $name)
 Check if parameter was already acquired. More...
 
 checkToken (URLBuilderToken $token)
 Check if a token is valid. More...
 
 checkLength (URI $uri)
 Check the full length of the URI against URL_MAX_LENGTH. More...
 

Private Attributes

URI $uri
 Base URI for the URLBuilder. More...
 
string $fragment = null
 Stores the URL fragment/hash (#) (always changeable due to its usage) More...
 
array $params = []
 Stores all acquired parameters These always take precedence over existing parameters in the base URI. More...
 
array $tokens = []
 Stores all generated tokens for acquired parameters. More...
 

Detailed Description

URLBuilder.

This provides an abstract representation of an URL and its parameters with the option of changing/removing parameters only by providing a token. These tokens are created when a new parameter is acquired and are therefore controlled by the component that added the parameter. This gives us better control over who gets to change which parameter. Besides that, parameters are always given a namespace so that parameters with the same name can exist beside each other. The in- and output of the URLBuilder are objects.

Along with this class, an equivalent Javascript class is provided in UI/Core that offers a similar functionality. The PHP object can be "transferred" to JS in any renderer by using the provided render...() functions to create JS objects/maps.

Definition at line 39 of file URLBuilder.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\UI\URLBuilder::__construct ( URI  $uri)

Definition at line 73 of file URLBuilder.php.

References ILIAS\UI\URLBuilder\$uri.

74  {
75  $this->uri = $uri;
76  }
URI $uri
Base URI for the URLBuilder.
Definition: URLBuilder.php:53

Member Function Documentation

◆ acquireParameter()

ILIAS\UI\URLBuilder::acquireParameter ( array  $namespace,
string  $name,
?string  $initial_value = null 
)

Add a new parameter with a namespace and get its token for subsequent changes.

The namespace can consists of one or more levels which are noted as an array. They will be joined with the separator (see constant) and used as a prefix for the name, e.g. Namespace: ["ilOrgUnit","filter"] Name: "name" Resulting parameter: "ilOrgUnit_filter_name"

The return value is an array containing both the changed URLBuilder as well as the token for any subsequent changes to the acquired parameter.

Returns
array{0: URLBuilder, 1: URLBuilderToken}
Exceptions

Definition at line 118 of file URLBuilder.php.

References $token, and ILIAS\UI\URLBuilder\parameterExists().

Referenced by ILIAS\components\ResourceStorage\Container\View\ActionBuilder\appendNamespaceToURIBuilder(), and ILIAS\Test\Results\Presentation\AttemptResultsTable\getViewControls().

118  : array
119  {
120  if ($name === '' || empty($namespace)) {
121  throw new \InvalidArgumentException("Parameter name or namespace not set");
122  }
123 
124  $parameter = implode(self::SEPARATOR, $namespace) . self::SEPARATOR . $name;
125  if ($this->parameterExists($parameter)) {
126  throw new \ilException("Parameter '" . $parameter . "' already reserved in URL");
127  }
128 
129  $token = new URLBuilderToken($namespace, $name);
130  $clone = clone $this;
131  $clone->params[$parameter] = ($initial_value) ?? '';
132  $clone->tokens[$parameter] = $token;
133 
134  return [$clone, $token];
135  }
if($err=$client->getError()) $namespace
parameterExists(string $name)
Check if parameter was already acquired.
Definition: URLBuilder.php:249
$token
Definition: xapitoken.php:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ acquireParameters()

ILIAS\UI\URLBuilder::acquireParameters ( array  $namespace,
string ...  $names 
)

Definition at line 137 of file URLBuilder.php.

References $token, and ILIAS\UI\URLBuilder\$tokens.

Referenced by ILIAS\Test\Scoring\Manual\TestScoringByQuestionGUI\__construct(), ILIAS\Test\Scoring\Marks\MarkSchemaGUI\__construct(), ilStudyProgrammeTypeGUI\__construct(), and ILIAS\Bibliographic\FieldFilter\Table\initURIBuilder().

137  : array
138  {
139  $tokens = [];
140  $builder = $this;
141  foreach ($names as $name) {
142  list($builder, $token) = $builder->acquireParameter($namespace, $name);
143  $tokens[] = $token;
144  }
145  array_unshift($tokens, $builder);
146  return $tokens;
147  }
if($err=$client->getError()) $namespace
$token
Definition: xapitoken.php:67
array $tokens
Stores all generated tokens for acquired parameters.
Definition: URLBuilder.php:71
+ Here is the caller graph for this function:

◆ buildFragment()

ILIAS\UI\URLBuilder::buildFragment ( )
private

Create the fragment/hash part of the URL.

Definition at line 237 of file URLBuilder.php.

References ILIAS\UI\URLBuilder\$fragment.

Referenced by ILIAS\UI\URLBuilder\buildURI().

237  : string
238  {
239  if ($this->fragment !== null) {
240  return ($this->fragment !== '') ? '#' . $this->fragment : '';
241  }
242  $fragment = ($this->uri->getFragment()) ? '#' . $this->uri->getFragment() : '';
243  return $fragment;
244  }
string $fragment
Stores the URL fragment/hash (#) (always changeable due to its usage)
Definition: URLBuilder.php:58
+ Here is the caller graph for this function:

◆ buildQuery()

ILIAS\UI\URLBuilder::buildQuery ( )
private

Create the query part of the URL from all parameters Claimed parameters overwrite base parameters in array_merge(), numeric indizes of array-parameters are being removed to ensure continous numeration (p[1]=A&p[2]=B –> p[]=A&p[]=B).

Definition at line 226 of file URLBuilder.php.

References ILIAS\UI\URLBuilder\$params.

Referenced by ILIAS\UI\URLBuilder\buildURI().

226  : string
227  {
228  $params = array_merge($this->uri->getParameters(), $this->params);
229  $query = (! empty($params)) ? '?' . http_build_query($params) : '';
230  $query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
231  return $query;
232  }
array $params
Stores all acquired parameters These always take precedence over existing parameters in the base URI...
Definition: URLBuilder.php:65
+ Here is the caller graph for this function:

◆ buildURI()

ILIAS\UI\URLBuilder::buildURI ( )

Get a URI representation of the full URL including query string and fragment/hash.

Exceptions

Definition at line 213 of file URLBuilder.php.

References ILIAS\UI\URLBuilder\$uri, ILIAS\UI\URLBuilder\buildFragment(), ILIAS\UI\URLBuilder\buildQuery(), and ILIAS\UI\URLBuilder\checkLength().

Referenced by ILIAS\UI\Implementation\Component\Table\Action\Action\__construct(), ILIAS\Test\Participants\ParticipantTableDeleteParticipantAction\getModal(), ILIAS\Test\Participants\ParticipantTableIpRangeAction\getModal(), ILIAS\Test\Participants\ParticipantTableDeleteResultsAction\getModal(), ILIAS\Test\Participants\ParticipantTableExtraTimeAction\getModal(), ILIAS\Test\Participants\ParticipantTableFinishTestAction\getModal(), ilStudyProgrammeTypeGUI\getUrl(), ilObjLanguageFolderGUI\getUrl(), ILIAS\Test\Results\Presentation\AttemptResultsTable\getViewControls(), and ILIAS\UI\URLBuilder\renderObject().

213  : URI
214  {
215  $uri = new URI($this->uri->getBaseURI() . $this->buildQuery() . $this->buildFragment());
216  $this->checkLength($uri);
217  return $uri;
218  }
URI $uri
Base URI for the URLBuilder.
Definition: URLBuilder.php:53
buildFragment()
Create the fragment/hash part of the URL.
Definition: URLBuilder.php:237
buildQuery()
Create the query part of the URL from all parameters Claimed parameters overwrite base parameters in ...
Definition: URLBuilder.php:226
checkLength(URI $uri)
Check the full length of the URI against URL_MAX_LENGTH.
Definition: URLBuilder.php:276
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkLength()

ILIAS\UI\URLBuilder::checkLength ( URI  $uri)
private

Check the full length of the URI against URL_MAX_LENGTH.

Exceptions

Definition at line 276 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilder\buildURI().

276  : void
277  {
278  if (! (strlen((string) $uri) <= self::URL_MAX_LENGTH)) {
279  throw new \LengthException("The final URL is longer than " . self::URL_MAX_LENGTH . " and will not be valid.");
280  }
281  }
URI $uri
Base URI for the URLBuilder.
Definition: URLBuilder.php:53
+ Here is the caller graph for this function:

◆ checkToken()

ILIAS\UI\URLBuilder::checkToken ( URLBuilderToken  $token)
private

Check if a token is valid.

Exceptions

Definition at line 260 of file URLBuilder.php.

References ILIAS\UI\URLBuilderToken\getName(), ILIAS\UI\URLBuilderToken\getToken(), and ILIAS\UI\URLBuilder\parameterExists().

Referenced by ILIAS\UI\URLBuilder\deleteParameter(), and ILIAS\UI\URLBuilder\withParameter().

260  : void
261  {
262  if (! in_array($token, $this->tokens)
263  || $this->tokens[$token->getName()]->getToken() !== $token->getToken()) {
264  throw new \DomainException("Token for '" . $token->getName() . "' is not valid");
265  }
266  if (! $this->parameterExists($token->getName())) {
267  throw new \ilException("Parameter '" . $token->getName() . "' does not exist in URL");
268  }
269  }
parameterExists(string $name)
Check if parameter was already acquired.
Definition: URLBuilder.php:249
$token
Definition: xapitoken.php:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deleteParameter()

ILIAS\UI\URLBuilder::deleteParameter ( URLBuilderToken  $token)

Delete an acquired parameter if the supplied token is valid.

Definition at line 152 of file URLBuilder.php.

References ILIAS\UI\URLBuilder\checkToken(), and ILIAS\UI\URLBuilderToken\getName().

152  : self
153  {
154  $this->checkToken($token);
155  $clone = clone $this;
156  unset($clone->params[$token->getName()]);
157  unset($clone->tokens[$token->getName()]);
158 
159  return $clone;
160  }
$token
Definition: xapitoken.php:67
checkToken(URLBuilderToken $token)
Check if a token is valid.
Definition: URLBuilder.php:260
+ Here is the call graph for this function:

◆ parameterExists()

ILIAS\UI\URLBuilder::parameterExists ( string  $name)
private

Check if parameter was already acquired.

Definition at line 249 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilder\acquireParameter(), and ILIAS\UI\URLBuilder\checkToken().

249  : bool
250  {
251  return array_key_exists($name, $this->params);
252  }
+ Here is the caller graph for this function:

◆ renderObject()

ILIAS\UI\URLBuilder::renderObject ( array  $tokens)

Renders a Javascript URLBuilder object with changeable parameters for all given tokens.

Note: By providing only the tokens that need to be changed on the JS side, all other parameters will be passed as unchangeable.

Parameters
array<URLBuilderToken>$tokens

Definition at line 202 of file URLBuilder.php.

References ILIAS\UI\URLBuilder\buildURI(), and ILIAS\UI\URLBuilder\renderTokens().

202  : string
203  {
204  $output = 'new il.UI.core.URLBuilder(new URL("' . $this->buildURI() . '"), ' . $this->renderTokens($tokens) . ')';
205  return $output;
206  }
buildURI()
Get a URI representation of the full URL including query string and fragment/hash.
Definition: URLBuilder.php:213
renderTokens(array $tokens)
Renders a Javascript Map of all given tokens.
Definition: URLBuilder.php:182
array $tokens
Stores all generated tokens for acquired parameters.
Definition: URLBuilder.php:71
+ Here is the call graph for this function:

◆ renderTokens()

ILIAS\UI\URLBuilder::renderTokens ( array  $tokens)

Renders a Javascript Map of all given tokens.

Note: Only the tokens needed for changing parameters on the JS side should be used here.

Parameters
array<URLBuilderToken>$tokens

Definition at line 182 of file URLBuilder.php.

References $token.

Referenced by ILIAS\UI\URLBuilder\renderObject().

182  : string
183  {
184  $token_render = [];
185  foreach ($tokens as $token) {
186  $token_render[] = '["' . $token->getName() . '",' . $token->render() . ']';
187  }
188  $output = 'new Map([' . implode(',', $token_render) . '])';
189  return $output;
190  }
$token
Definition: xapitoken.php:67
array $tokens
Stores all generated tokens for acquired parameters.
Definition: URLBuilder.php:71
+ Here is the caller graph for this function:

◆ withFragment()

ILIAS\UI\URLBuilder::withFragment ( ?string  $fragment)

Change the fragment/hash part of the URL.

Definition at line 91 of file URLBuilder.php.

References ILIAS\UI\URLBuilder\$fragment.

91  : self
92  {
93  $clone = clone $this;
94  $clone->fragment = $fragment;
95  return $clone;
96  }
string $fragment
Stores the URL fragment/hash (#) (always changeable due to its usage)
Definition: URLBuilder.php:58

◆ withParameter()

◆ withURI()

ILIAS\UI\URLBuilder::withURI ( URI  $uri)

Changes the base URI of the Builder.

Definition at line 81 of file URLBuilder.php.

References ILIAS\UI\URLBuilder\$uri.

81  : self
82  {
83  $clone = clone $this;
84  $clone->uri = $uri;
85  return $clone;
86  }
URI $uri
Base URI for the URLBuilder.
Definition: URLBuilder.php:53

Field Documentation

◆ $fragment

string ILIAS\UI\URLBuilder::$fragment = null
private

Stores the URL fragment/hash (#) (always changeable due to its usage)

Definition at line 58 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilder\buildFragment(), and ILIAS\UI\URLBuilder\withFragment().

◆ $params

array ILIAS\UI\URLBuilder::$params = []
private

Stores all acquired parameters These always take precedence over existing parameters in the base URI.

array<string, string>

Definition at line 65 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilder\buildQuery().

◆ $tokens

array ILIAS\UI\URLBuilder::$tokens = []
private

Stores all generated tokens for acquired parameters.

array<string, URLBuilderToken>

Definition at line 71 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilder\acquireParameters().

◆ $uri

URI ILIAS\UI\URLBuilder::$uri
private

◆ SEPARATOR

◆ URL_MAX_LENGTH

const ILIAS\UI\URLBuilder::URL_MAX_LENGTH = 8192

A maximum length of 8192 characters should be safe to use in most browsers, even though longer URLs will be supported by some.

Definition at line 45 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilderTest\testUrlTooLong().


The documentation for this class was generated from the following file: