bvp/trimmer

The BVP Trimmer package for strings, arrays, and objects.

4.0.1 2025-04-12 06:04 UTC

This package is auto-updated.

Last update: 2025-04-21 05:59:47 UTC


README

English | 日本語

Build Status codecov PHP Version Require Latest Stable Version Latest Unstable Version License

The BVP Trimmer package extends PHP's built-in trim, ltrim, and rtrim functions so they can also be used with arrays and objects.

Features

  • Recursively trims all string elements in arrays
  • Trims object properties via getter/setter methods
  • Supports nested arrays and nested objects
  • API is consistent with trim, ltrim, and rtrim

Installation

composer require bvp/trimmer

Usage

<?php

require __DIR__ . '/vendor/autoload.php';

use BVP\Trimmer\Trimmer;

For strings

Trimmer::trim(' trimmer ');                // "trimmer"
Trimmer::trim(' @trimmer@ ', "\x20\x40");  // "trimmer"

Trimmer::ltrim(' trimmer ');               // "trimmer "
Trimmer::ltrim(' @trimmer@ ', "\x20\x40"); // "trimmer@ "

Trimmer::rtrim(' trimmer ');               // " trimmer"
Trimmer::rtrim(' @trimmer@ ', "\x20\x40"); // " @trimmer"

For arrays

Trimmer::trim([' trimmerA ']);
// => ["trimmerA"]

Trimmer::trim([' trimmerA ', [' trimmerB ']]);
// => ["trimmerA", ["trimmerB"]]

Trimmer::trim([' trimmerA ', 1, 1.0, true, null]);
// => ["trimmerA", 1, 1.0, true, null]

Trimmer::trim([' trimmerA ', [' trimmerB ', 1, 1.0, true, null]]);
// => ["trimmerA", ["trimmerB", 1, 1.0, true, null]]

Examples for ltrim and rtrim are omitted for brevity but work the same way.

For objects

Trimming object properties requires both getter and setter methods. Nested objects are also supported.

$objectA = new class {
    private string $propertyA = ' trimmerA ';
    private string $propertyB = ' trimmerB '; // This will not be trimmed.
    public function getPropertyA(): string { return $this->propertyA; }
    public function setPropertyA(string $value): void { $this->propertyA = $value; }
    public function getPropertyB(): string { return $this->propertyB; }
};

Trimmer::trim($objectA);
// $propertyA will be trimmed, $propertyB will remain unchanged.

Nested objects are supported as well:

$objectB = new class($objectA) {
    private string $propertyC = ' trimmerC ';
    private string $propertyD = ' trimmerD '; // This will not be trimmed.
    private object $objectA;
    public function __construct(object $objectA) {
        $this->objectA = $objectA;
    }
    public function getPropertyC(): string { return $this->propertyC; }
    public function setPropertyC(string $value): void { $this->propertyC = $value; }
    public function getPropertyD(): string { return $this->propertyD; }
    public function getObjectA(): object { return $this->objectA; }
};

Trimmer::trim($objectB);
// $propertyC and $objectA->propertyA will be trimmed, $propertyD and $objectA->$propertyB will remain unchanged.

Examples for ltrim and rtrim are omitted for brevity but are fully supported.

Notes

All Trimmer::trim, Trimmer::ltrim, and Trimmer::rtrim methods are non-destructive (they return new values rather than modifying the original data).

License

The BVP Trimmer package is open source software licensed under the MIT license.