Skip to content

Commit

Permalink
init package files
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Nov 11, 2023
0 parents commit 41ff1a7
Show file tree
Hide file tree
Showing 14 changed files with 2,029 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Mohammed Taha [BMT]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
161 changes: 161 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Effectra/DataOptimizer PHP Package

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![PHP Version](https://img.shields.io/badge/PHP-%3E%3D7.0-8892BF.svg)](https://www.php.net/)

**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.

## Features

- **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.

## Installation

To install the Data Optimizer PHP package, you can use Composer. Run the following command in your project directory:

```bash
composer require effectra/data-optimizer
```

## Usage

### 1. DataAttribute Class

The `DataAttribute` class provides methods for managing attributes. Here are some examples of how to use it:

#### Set a Single Attribute:

```php
use Effectra\DataOptimizer\DataAttribute;

$dataAttribute = new DataAttribute();
$dataAttribute->setAttribute('name', 'John Doe');
```

#### Set Multiple Attributes:

```php
use Effectra\DataOptimizer\DataAttribute;

$dataAttribute = new DataAttribute();
$dataAttribute->setAttributes(['name' => 'John Doe', 'age' => 25, 'city' => 'New York']);
```

#### Get All Attributes:

```php
use Effectra\DataOptimizer\DataAttribute;

$dataAttribute = new DataAttribute();
$attributes = $dataAttribute->getAttributes();
```

### 2. DataCollection Class

The `DataCollection` class provides various methods to manipulate and interact with an array of data. Here are some examples:

#### Create a Collection:

```php
use Effectra\DataOptimizer\DataCollection;

$dataCollection = new DataCollection([1, 2, 3, 4, 5]);
```

#### Filter the Collection:

```php
use Effectra\DataOptimizer\DataCollection;

$dataCollection = new DataCollection([1, 2, 3, 4, 5]);
$filteredCollection = $dataCollection->filter(fn($item) => $item > 2);
```

#### Map Over the Collection:

```php
use Effectra\DataOptimizer\DataCollection;

$dataCollection = new DataCollection([1, 2, 3, 4, 5]);
$mappedCollection = $dataCollection->map(fn($item) => $item * 2);
```

### 3. DataOptimizer Class

The `DataOptimizer` class is designed for optimizing and transforming data based on defined rules. Here's an example of how to use it:

```php
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
```

### 4. DataRules Class

The `DataRules` class is used for defining validation rules and attributes for data. Here's an example:

```php
use Effectra\DataOptimizer\DataRules;

$rules = new DataRules();

$rules->string('name');
$rules->integer('age');
$rules->string('city');

// Access the defined rules
$definedRules = $rules->getRules();
```

### 5. DataValidator Class

The `DataValidator` class is for validating and processing data. Here's an example:

```php
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();
}
```

## Contributing

If you'd like to contribute to this project, please fork the repository and submit a pull request.

## License

This Data Optimizer PHP package is open-sourced software licensed under the [MIT license](LICENSE).
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "effectra/data-optimizer",
"description": "The Effectra Data Optimizer package.",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Effectra\\DataOptimizer\\": "src/"
}
},
"authors": [
{
"name": "Mohammed Taha",
"email": "[email protected]"
}
],
"require": {
},
"minimum-stability": "stable"
}
83 changes: 83 additions & 0 deletions src/Contracts/DataAttributeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Effectra\DataOptimizer;

/**
* Class DataAttribute
*
* A class for managing attributes.
*
* @package Effectra\DataOptimizer
*/
interface DataAttributeInterface
{
/**
* Set a specific attribute to a given value.
*
* @param string $attribute The name of the attribute.
* @param mixed $value The value to set for the attribute.
*/
public function setAttribute(string $attribute, $value): void;


/**
* Set multiple attributes using an associative array.
*
* @param array $attributes An associative array of attributes and their values.
*/
public function setAttributes(array $attributes): void;


/**
* Merge the current attributes with an array of attributes.
*
* @param array $attributes An associative array of attributes to merge.
*/
public function mergeAttribute(array $attributes): void;


/**
* Add a value to an attribute that is an array.
*
* @param string $attribute The name of the attribute.
* @param mixed $value The value to add to the attribute (should be an array).
*/
public function addToAttribute(string $attribute, $value): void;


/**
* Get all attributes as an associative array.
*
* @return array An associative array of attributes and their values.
*/
public function getAttributes(): array;

/**
* Get the value of a specific attribute.
*
* @param string $attribute The name of the attribute.
* @return mixed|null The value of the attribute or null if it doesn't exist.
*/
public function getAttribute(string $attribute);


/**
* Check if a specific attribute exists.
*
* @param string $attribute The name of the attribute to check.
* @return bool True if the attribute exists, false otherwise.
*/
public function hasAttribute(string $attribute): bool;


/**
* Remove a specific item from an attribute array.
*
* @param string $attribute The attribute name.
* @param mixed $itemCleared The item to be removed.
* @return void
*/
public function clearFromAttribute($attribute, $itemCleared): void;
}
Loading

0 comments on commit 41ff1a7

Please sign in to comment.