Skip to content Skip to footer

Introduction

PHP String Objects is a library that allows developers to easily access and manipulate PHP arrays and objects using string-based syntax. This can save developers a lot of time and frustration when working with large datasets, as they no longer need to write complex code to access and manipulate specific elements.

In this blog post, we will explore some of the key features and benefits of PHP String Objects and show how you can use it in your own projects.

Getting Started

To get started with PHP String Objects, you will need to install it using Composer:

composer require uuur86/strobj
javascriptCopy codecomposer require uuur86/strobj

Once installed, you can start using PHP String Objects in your PHP code.

Features

Accessing Data

With PHP String Objects, you can easily access specific elements of an array or object using a string-based syntax. For example, to access the name of the first person in an array of persons, you can use the following code:

$test->set('persons/0/name', 'John Doe');

This syntax is much simpler and easier to read than traditional PHP syntax for accessing array elements.

Manipulating Data

PHP String Objects also allows you to manipulate the data in your arrays and objects using a similar string-based syntax. For example, to update the name of the first person in an array of persons, you can use the following code:

$test->set('persons/0/name', 'John Doe');

This can save you a lot of time and frustration when working with large datasets, as you no longer need to write complex code to manipulate specific elements.

Data Validation

Another key feature of PHP String Objects is its data validation capabilities. With PHP String Objects, you can easily validate the data in your arrays and objects to ensure that it meets your specific requirements. For example, you can use regular expressions to ensure that an age field only contains numbers:

$validation_rules = [
    'validation' => [
        'patterns' => [
            'age' => '#^[0-9]+$#siu',
        ],
        'rules' => [
            [
                'path' => 'persons/*/age',
                'pattern' => 'age',
                'required' => true
            ],
        ],
    ],
];

$test = StringObjects::instance($persons, $validation_rules);

Filters

PHP String Objects also allows you to apply filters to the data in your arrays and objects. For example, you can filter all age fields to ensure that they are greater than a certain value:

$filters = [
    'filters' => [
        'persons/*/age' => [
            'type' => 'int',
            'callback' => function ($value) {
                return $value > 10;
            }
        ],
    ],
];

$test = StringObjects::instance($persons, $filters);

Middleware

Finally, PHP String Objects also provides middleware capabilities that allow you to customize the behavior of the library. For example, you can set a memory limit for the library using the following code:

$middleware = [
    'middleware' => [
        'memory_limit' => 1024 * 1024 * 3,
    ],
];

$test = StringObjects::instance($persons, $middleware);

Conclusion

PHP String Objects is a powerful library that can save developers a lot of time and frustration when working with large datasets. Its string-based syntax makes it easy to access and manipulate data in arrays and objects, and its data validation, filters, and middleware