AlterUrlPropertyTrait uses trait:short
Generates URL strings from document properties, with support for base URL resolution from a DI container.
This trait is typically used as part of the AlterDocumentTrait workflow to transform
document properties into complete URLs. It leverages joinPaths() for robust path
joining that handles various path types (Unix, Windows, scheme-based URLs, etc.).
Features:
- Combines base URLs (from container or parameters) with path segments
- Extracts property values from arrays or objects using
getKeyValue() - Optionally resolves base URL from a DI container
- Adds trailing slashes when needed
- Handles multiple URL formats (relative paths, absolute URLs, Windows paths, Phar archives)
Usage Examples:
Simple relative URL:
$document = ['id' => 42, 'slug' => 'product-name'];
$url = $this->alterUrlProperty($document, ['/api/products']);
// Result: '/api/products/42'
With custom property:
$url = $this->alterUrlProperty($document, ['/api/products', 'slug']);
// Result: '/api/products/product-name'
Skip container resolution explicitly:
$url = $this->alterUrlProperty($document, ['/api/products', 'id', false]);
// Re
**With container-resolved base URL and trailing slash:**
```php
// Assumes container has 'baseUrl' => 'https://example.com'
$url = $this->alterUrlProperty(
$document,
['/api/products', 'id', 'baseUrl', true],
$modified,
'id',
$container
);
// Result: 'https://example.com/api/products/42/'
In AlterDocumentTrait configuration:
$this->alters =
[
'url' => [Alter::URL, '/places', 'id'],
'link' => [Alter::URL, '/products', 'slug', 'baseUrl', true],
];
Tags
Table of Contents
Properties
- $alterKey : string
- The default alter key reference.
Methods
- alterUrlProperty() : string
- Generates a complete URL by combining path segments and document properties.
- initializeAlterKey() : static
- Initialize the 'alters' property.
Properties
$alterKey
The default alter key reference.
public
string
$alterKey
= \org\schema\constants\Schema::ID
Tags
Methods
alterUrlProperty()
Generates a complete URL by combining path segments and document properties.
public
alterUrlProperty(array<string|int, mixed>|object $document[, array<string|int, mixed> $options = [] ][, Container|null $container = null ][, bool &$modified = false ][, string $propertyName = null ][, string $containerKey = 'baseUrl' ]) : string
The method builds a URL in the following order:
- Resolves the base URL from the container (if containerKey is provided and not false)
- Joins the base URL with the provided path segment
- Appends the property value from the document
- Optionally adds a trailing slash
Parameters via $options array:
[0](string) — Path segment to append after base URL (e.g., '/products', '/api/v1/users')[1](string) — Document property name containing the final segment (default: 'id')[2](string|bool) — Container service key for base URL resolution orfalseto skip (default: 'baseUrl')[3](bool) — Add trailing slash to the result (default: false)
Parameters
- $document : array<string|int, mixed>|object
-
The document to read property values from.
- $options : array<string|int, mixed> = []
-
Configuration array: [path, propertyName, containerKey, trailingSlash]
- $container : Container|null = null
-
DI container for resolving base URL from service definitions.
- $modified : bool = false
-
Reference flag set to true if URL alteration occurs.
- $propertyName : string = null
-
Default property name to use if not specified in options.
- $containerKey : string = 'baseUrl'
-
Default container key for base URL if not specified in options.
Tags
Return values
string —The generated URL.
initializeAlterKey()
Initialize the 'alters' property.
public
initializeAlterKey([array<string|int, mixed> $init = [] ]) : static
Parameters
- $init : array<string|int, mixed> = []