Oihana PHP System

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
author

Marc Alcaraz (ekameleon)

since
1.0.0

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

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:

  1. Resolves the base URL from the container (if containerKey is provided and not false)
  2. Joins the base URL with the provided path segment
  3. Appends the property value from the document
  4. 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 or false to 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
throws
DependencyException

If container service resolution fails.

throws
NotFoundException

If container service is not found.

Complete Example:

class DocumentMapper
{
    use AlterUrlPropertyTrait ;

    public function mapPlace( $document, $container )
{
        return $this->alterUrlProperty
(
            $document,
            ['/places', 'id', 'baseUrl', true],
            $modified,
            'id',
            $container,
            'baseUrl'
        );
    }
}

// With container containing: 'baseUrl' => 'https://api.example.com'
// And document: ['id' => 123]
// Result: 'https://api.example.com/places/123/'
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> = []
Return values
static

        
On this page

Search results