DataOptimizer is a PHP package designed to optimize and transform data based on defined rules. It provides a set of classes and interfaces for managing attributes, working with collections, defining data rules, and optimizing data according to those rules.
-
DataAttribute: Manage attributes with methods for setting, getting, and manipulating attribute values.
-
DataCollection: Manipulate and interact with an array of data using various methods.
-
DataRules: Define validation rules and attributes for data with methods to set rules for different data types.
-
DataOptimizer: Optimize and transform data based on customizable rules.
To install the Data Optimizer PHP package, you can use Composer. Run the following command in your project directory:
composer require effectra/data-optimizer
The DataAttribute
class provides methods for managing attributes. Here are some examples of how to use it:
use Effectra\DataOptimizer\DataAttribute;
$dataAttribute = new DataAttribute();
$dataAttribute->setAttribute('name', 'John Doe');
use Effectra\DataOptimizer\DataAttribute;
$dataAttribute = new DataAttribute();
$dataAttribute->setAttributes(['name' => 'John Doe', 'age' => 25, 'city' => 'New York']);
use Effectra\DataOptimizer\DataAttribute;
$dataAttribute = new DataAttribute();
$attributes = $dataAttribute->getAttributes();
The DataCollection
class provides various methods to manipulate and interact with an array of data. Here are some examples:
use Effectra\DataOptimizer\DataCollection;
$dataCollection = new DataCollection([1, 2, 3, 4, 5]);
use Effectra\DataOptimizer\DataCollection;
$dataCollection = new DataCollection([1, 2, 3, 4, 5]);
$filteredCollection = $dataCollection->filter(fn($item) => $item > 2);
use Effectra\DataOptimizer\DataCollection;
$dataCollection = new DataCollection([1, 2, 3, 4, 5]);
$mappedCollection = $dataCollection->map(fn($item) => $item * 2);
The DataOptimizer
class is designed for optimizing and transforming data based on defined rules. Here's an example of how to use it:
use Effectra\DataOptimizer\DataOptimizer;
$data = [
['name' => 'John Doe', 'age' => '25', 'city' => 'New York'],
['name' => 'Jane Doe', 'age' => '30', 'city' => 'San Francisco'],
// ... more data
];
$optimizer = new DataOptimizer($data);
// Define rules using a callback function
$optimizedData = $optimizer->optimize(function ($rules) {
$rules->string('name');
$rules->integer('age');
$rules->string('city');
});
// $optimizedData now contains the transformed data based on the defined rules
The DataRules
class is used for defining validation rules and attributes for data. Here's an example:
use Effectra\DataOptimizer\DataRules;
$rules = new DataRules();
$rules->string('name');
$rules->integer('age');
$rules->string('city');
// Access the defined rules
$definedRules = $rules->getRules();
The DataValidator
class is for validating and processing data. Here's an example:
use Effectra\DataOptimizer\DataValidator;
use Effectra\Database\Exception\DataValidatorException;
$data = [
['name' => 'John Doe', 'age' => 25, 'city' => 'New York'],
['name' => 'Jane Doe', 'age' => 30, 'city' => 'San Francisco'],
// ... more data
];
try {
$validator = new DataValidator($data);
$validator->isArrayOfAssoc();
$validator->validate();
} catch (DataValidatorException $e) {
// Handle validation errors
echo $e->getMessage();
}
If you'd like to contribute to this project, please fork the repository and submit a pull request.
This Data Optimizer PHP package is open-sourced software licensed under the MIT license.