Oihana PHP System

AlterListififyPropertyTrait

Provides a method to transform a string or array into a normalized list string.

This trait is typically used in alteration pipelines to convert semicolon-separated strings or arrays into clean, formatted list strings with configurable separators.

The transformation process:

  • Splits strings by a separator (default: ;)
  • Trims each element
  • Removes empty elements
  • Joins elements with a replacement separator (default: PHP_EOL)
  • Returns a default value if the result is empty

The $modified flag is set to true if the resulting value differs from the original.

Tags
see
resolveList

For the underlying list resolution logic.

example
use oihana\models\traits\alters\AlterListifyPropertyTrait;

class Product {
use AlterListifyPropertyTrait;
}

$product = new Product();
$modified = false;

// Default behavior (split by ';', join with PHP_EOL)
$value = $product->alterListifyProperty('foo;bar;baz', [], $modified);
// Result: "foo\nbar\nbaz"
// $modified === true

// Custom separator and replacement
$value = $product->alterListifyProperty('a,b,c', [',', ' | '], $modified);
// Result: "a | b | c"
// $modified === true

// With default fallback for empty input
$value = $product->alterListifyProperty(';;;', [';', PHP_EOL, 'N/A'], $modified);
// Result: "N/A"
// $modified === true

// Array input
$value = $product->alterListifyProperty(['foo', '  bar  ', '', 'baz'], [], $modified);
// Result: "foo\nbar\nbaz"
// $modified === true
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

alterListifyProperty()  : string|null
Transforms a string or array into a normalized list string.

Methods

alterListifyProperty()

Transforms a string or array into a normalized list string.

public alterListifyProperty(mixed $value[, array<string|int, mixed> $definition = [] ][, bool &$modified = false ]) : string|null

The transformation can be customized via the $definition array:

  • $definition[0] (string): Input separator for strings (default: ;)
  • $definition[1] (string): Output separator for joining (default: PHP_EOL)
  • $definition[2] (string|null): Default value if result is empty (default: null)
Parameters
$value : mixed

The value to transform (string, array, or null)

$definition : array<string|int, mixed> = []

Optional parameters: [separator, replace, default]

$modified : bool = false

Reference flag indicating if the value was modified

Tags
example
// Use default separators
$this->alterListifyProperty('a;b;c');
// Result: "a\nb\nc"

// Custom separators
$this->alterListifyProperty('a,b,c', [',', ' - ']);
// Result: "a - b - c"

// With fallback default
$this->alterListifyProperty('', [';', PHP_EOL, 'empty']);
// Result: "empty"
Return values
string|null

The normalized list string, or default if empty


        
On this page

Search results