PHPackages                             bazarin/bazarin-php-library - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. bazarin/bazarin-php-library

ActiveLibrary[Database &amp; ORM](/categories/database)

bazarin/bazarin-php-library
===========================

A PHP library for database operations, file management, API calls, and security utilities.

v1.2.0(11mo ago)115MITPHPPHP &gt;=7.4

Since Feb 22Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/BazarinTech/bazarin-php-v2)[ Packagist](https://packagist.org/packages/bazarin/bazarin-php-library)[ RSS](/packages/bazarin-bazarin-php-library/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

Bazarin PHP Library - Comprehensive Guide
=========================================

[](#bazarin-php-library---comprehensive-guide)

Bazarin PHP Library is a lightweight and efficient PHP library for database operations, file handling, API interactions, and security functions.

📌 Installation
--------------

[](#-installation)

To install the library using **Composer**, run:

```
composer require bazarin/bazarin-php-library
```

After installation, include the **autoload** file in your PHP project:

```
include 'QueryBuilder.php';

$query = new QueryBuilder($conn);
```

---

**Usage**
---------

[](#usage)

### **1. `select()`**

[](#1-select)

Retrieve data from a table.

```
$results = $query->select('users', '*', ['id' => 1]);
```

---

### **2. `insert()`**

[](#2-insert)

Add new data to a table.

```
$newId = $query->insert('users', ['name' => 'Jane Doe', 'email' => 'jane@example.com']);
```

---

### **3. `update()`**

[](#3-update)

Modify existing records.

```
$affectedRows = $query->update('users', ['email' => 'new@example.com'], ['id' => 1]);
```

---

### **4. `delete()`**

[](#4-delete)

Remove records from a table.

```
$deletedRows = $query->delete('users', ['id' => 1]);
```

---

**Error Handling**
------------------

[](#error-handling)

Use a `try-catch` block to handle exceptions:

```
try {
    $query->select('non_existing_table');
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
```

Here's what you should add to the README file to document the `RestClient` and `ApiManager` classes:

---

API Library Documentation
=========================

[](#api-library-documentation)

This library provides a simple way to interact with RESTful APIs using PHP's cURL. It supports all standard HTTP methods (GET, POST, PUT, DELETE) and includes features such as custom headers, error handling, and debug logging.

Features
--------

[](#features)

- Perform CRUD operations with RESTful APIs.
- Supports custom headers for authorization or other requirements.
- Handles HTTP errors and invalid JSON responses gracefully.
- Debugging mode for logging requests and responses.
- Easy-to-use methods for common API operations.

---

Installation
------------

[](#installation)

Simply download or clone this repository. Include the `RestClient` and `ApiManager` classes in your project.

```
require_once './APIS/curl.php';
```

---

Usage
-----

[](#usage-1)

### Initialization

[](#initialization)

```
$apiManager = new ApiManager(['Authorization: Bearer your_api_token'], true);
```

- The first parameter is an array of default headers.
- The second parameter enables or disables debug mode.

---

### Methods

[](#methods)

#### Fetch All Records

[](#fetch-all-records)

Fetches data using a GET request.

```
$response = $apiManager->fetchAll('https://api.example.com/items');
```

#### Create a Record

[](#create-a-record)

Sends a POST request to create a new resource.

```
$newItem = $apiManager->create('https://api.example.com/items', [
    'name' => 'Item Name',
    'description' => 'Description of the item'
]);
```

#### Update a Record

[](#update-a-record)

Sends a PUT request to update an existing resource.

```
$updateResponse = $apiManager->update('https://api.example.com/items/1', [
    'name' => 'Updated Name'
]);
```

#### Delete a Record

[](#delete-a-record)

Sends a DELETE request to remove a resource.

```
$deleteResponse = $apiManager->delete('https://api.example.com/items/1');
```

---

### Advanced Features

[](#advanced-features)

#### Custom Headers

[](#custom-headers)

You can pass additional headers for specific requests:

```
$response = $apiManager->fetchAll('https://api.example.com/items', ['Custom-Header: value']);
```

#### Debugging Mode

[](#debugging-mode)

Enable debug mode to log detailed information about each request and response:

```
$apiManager->enableDebugMode(true);
```

#### Error Handling

[](#error-handling-1)

The library automatically throws exceptions for:

- HTTP errors (status codes 400 and above).
- cURL connection errors.
- Invalid JSON responses.

Use a try-catch block to handle exceptions:

```
try {
    $response = $apiManager->fetchAll('https://api.example.com/items');
} catch (RuntimeException $e) {
    echo "Error: " . $e->getMessage();
}
```

---

### Example

[](#example)

```
$apiManager = new ApiManager(['Authorization: Bearer your_api_token'], true);

try {
    // Fetch items
    $items = $apiManager->fetchAll('https://api.example.com/items');
    print_r($items);

    // Create an item
    $newItem = $apiManager->create('https://api.example.com/items', ['name' => 'New Item']);
    echo "Created Item ID: " . $newItem['id'];

    // Update an item
    $updatedItem = $apiManager->update('https://api.example.com/items/1', ['name' => 'Updated Name']);
    echo "Updated Item: " . json_encode($updatedItem);

    // Delete an item
    $apiManager->delete('https://api.example.com/items/1');
    echo "Item deleted successfully.";
} catch (RuntimeException $e) {
    echo "API Error: " . $e->getMessage();
}
```

The provided `FileGetContent` class is designed to handle incoming HTTP requests with JSON payloads and apply CORS (Cross-Origin Resource Sharing) headers to allow secure access from specified origins. Below is an explanation and usage guide for the class, which you can also include in your README file.

---

FileGetContent Class Documentation
==================================

[](#filegetcontent-class-documentation)

Features
--------

[](#features-1)

- Enables CORS support for handling cross-origin requests.
- Processes incoming JSON payloads from HTTP requests.
- Handles `OPTIONS` preflight requests automatically for better CORS compliance.

---

Methods
-------

[](#methods-1)

### Constructor: `__construct($origin)`

[](#constructor-__constructorigin)

Initializes the class with the allowed origin for CORS requests.

- **Parameter**:
    - `$origin` (string): The domain allowed to access this API. For example, `'https://example.com'`.

---

### `cors_auth()`

[](#cors_auth)

Handles CORS headers to allow secure communication between origins.

- Adds the following headers:

    - `Access-Control-Allow-Origin`: Matches the provided origin.
    - `Access-Control-Allow-Methods`: Specifies allowed HTTP methods (POST, GET, OPTIONS).
    - `Access-Control-Allow-Headers`: Lists allowed headers (e.g., Content-Type, Authorization).
    - `Access-Control-Allow-Credentials`: Indicates whether cookies and credentials are allowed.
- Automatically responds to `OPTIONS` requests with a 200 status code and exits. This is necessary for preflight requests in modern browsers.

---

### `get_content()`

[](#get_content)

Processes the request body and decodes JSON payloads.

- **Returns**:

    - Associative array: Parsed JSON from the request body.
    - If no JSON is sent or it is invalid, `null` is returned.
- **Example**:

    ```
    $fileGetContent = new FileGetContent('https://example.com');
    $data = $fileGetContent->get_content();
    print_r($data); // Output the parsed JSON payload
    ```

---

Example Usage
-------------

[](#example-usage)

Here is an example of how to use the `FileGetContent` class in an API endpoint:

```
// Include the class file
require_once 'FileGetContent.php';

// Set the allowed origin
$fileHandler = new FileGetContent('https://your-frontend-domain.com');

// Retrieve the JSON content from the request
$data = $fileHandler->get_content();

// Process the incoming data
if ($data) {
    echo json_encode([
        'status' => 'success',
        'message' => 'Data received successfully',
        'data' => $data,
    ]);
} else {
    echo json_encode([
        'status' => 'error',
        'message' => 'Invalid or missing JSON payload',
    ]);
}
```

---

Notes
-----

[](#notes)

1. **CORS Configuration**: Update the `$origin` parameter in the constructor to match your frontend domain. Use `'*'` to allow all origins (not recommended for production).
2. **Security**: This class does not validate or sanitize the incoming JSON payload. Ensure that additional validation is performed as needed.

---

Great! The provided code uses AES-256-CBC encryption, which is a symmetric encryption method. It generates an encryption key, uses an initialization vector (IV), and supports both encryption and decryption processes. Below is the updated documentation for the `Crypt` class using your provided code.

---

Crypt Class Documentation
=========================

[](#crypt-class-documentation)

Overview
--------

[](#overview)

The `Crypt` class provides functionality for **encrypting** and **decrypting** data using the AES-256-CBC encryption method. The class uses a symmetric encryption system where the same key is used for both encryption and decryption. The key must be securely managed, as anyone with access to the key can decrypt the data.

Methods
-------

[](#methods-2)

- **`__construct($key)`**: Initializes the class with an encryption key.
- **`encrypt($data)`**: Encrypts the provided data.
- **`decrypt($data)`**: Decrypts the provided encrypted data.

---

### 1. **`__construct($key)`**

[](#1-__constructkey)

#### Description:

[](#description)

The constructor method initializes the encryption key used for both encryption and decryption.

#### Parameters:

[](#parameters)

- **`$key`** (string): The encryption key used for AES-256-CBC. It is crucial that this key is kept secret and secure.

#### Throws:

[](#throws)

- **`InvalidArgumentException`**: If the provided key is empty.

#### Example Usage:

[](#example-usage-1)

```
$crypt = new Crypt("my_secret_key");  // Initializes with the encryption key
```

---

### 2. **`encrypt($data)`**

[](#2-encryptdata)

#### Description:

[](#description-1)

Encrypts the provided plaintext data using AES-256-CBC encryption. The method generates a random initialization vector (IV) for encryption to enhance security.

#### Parameters:

[](#parameters-1)

- **`$data`** (string): The plaintext data to encrypt.

#### Returns:

[](#returns)

- (string): The encrypted data as a base64-encoded string, containing both the encrypted data and the IV.

#### Example Usage:

[](#example-usage-2)

```
$crypt = new Crypt("my_secret_key");
$plaintext = "Hello, this is a secret message!";
$encryptedData = $crypt->encrypt($plaintext);
echo $encryptedData;  // Encrypted data in base64 format
```

---

### 3. **`decrypt($data)`**

[](#3-decryptdata)

#### Description:

[](#description-2)

Decrypts the provided encrypted data using the AES-256-CBC method and the same encryption key. The method extracts the IV from the base64-encoded string and uses it for decryption.

#### Parameters:

[](#parameters-2)

- **`$data`** (string): The base64-encoded encrypted data containing the ciphertext and IV.

#### Returns:

[](#returns-1)

- (string): The decrypted plaintext data.

#### Example Usage:

[](#example-usage-3)

```
$crypt = new Crypt("my_secret_key");
$encryptedData = "base64_encrypted_data_with_iv";  // The base64-encoded encrypted string
$decryptedData = $crypt->decrypt($encryptedData);
echo $decryptedData;  // Decrypted message
```

---

Important Notes
---------------

[](#important-notes)

1. **Key Management**: The encryption key is critical for both encrypting and decrypting data. Ensure the key is kept secure and not exposed.
2. **AES-256-CBC**: This encryption method is considered very secure. It uses a 256-bit key and a 128-bit initialization vector (IV). The IV is randomly generated for each encryption operation to prevent identical data from having the same ciphertext.
3. **Initialization Vector (IV)**: An IV is required for AES-CBC mode. It is generated randomly for each encryption to ensure that the same plaintext encrypted multiple times produces different ciphertexts. The IV is stored alongside the ciphertext in the base64-encoded string.
4. **Base64 Encoding**: The encrypted data and IV are returned as a base64-encoded string. This is done so that binary data can be safely transmitted over protocols that only support text-based data.

---

Example Workflow
----------------

[](#example-workflow)

### Encryption

[](#encryption)

1. **Encrypt data** using the `encrypt()` method: ```
    $crypt = new Crypt("my_secret_key");
    $plaintext = "Sensitive information that needs encryption.";
    $encrypted = $crypt->encrypt($plaintext);
    echo $encrypted;
    ```

### Decryption

[](#decryption)

2. **Decrypt data** using the `decrypt()` method: ```
    $crypt = new Crypt("my_secret_key");
    $encryptedData = "base64_encrypted_string";
    $decrypted = $crypt->decrypt($encryptedData);
    echo $decrypted;  // Should display the original plaintext
    ```

---

Conclusion
----------

[](#conclusion)

The `Crypt` class provides an easy and secure way to encrypt and decrypt data using AES-256-CBC. The class ensures confidentiality by encrypting data with a secure algorithm and managing the initialization vector. To decrypt the data, the same key used for encryption is required.

---

Here is the documentation for the `FileHelper` class:

---

**FileHelper Class Documentation**
==================================

[](#filehelper-class-documentation)

The `FileHelper` class provides utility functions for handling file uploads in PHP. It allows for uploading files to a specified destination with optional validation for file type and file size.

**Methods**
-----------

[](#methods-3)

### `upload($file, $destination, $allowedTypes = [], $maxSize = 0)`

[](#uploadfile-destination-allowedtypes---maxsize--0)

This method is used to upload a file to the server. It optionally validates the file's type and size before moving it to the specified destination.

#### **Parameters:**

[](#parameters-3)

- **`$file`** (array) – The file to be uploaded, typically from the `$_FILES` superglobal array (e.g., `$_FILES['file']`).
- **`$destination`** (string) – The target location on the server where the file will be uploaded (e.g., `'uploads/filename.ext'`).
- **`$allowedTypes`** (array, optional) – An array of allowed MIME types for the file. This is an optional parameter. If provided, the function will check that the file type matches one of the allowed MIME types. Example: `['image/jpeg', 'image/png']`.
- **`$maxSize`** (int, optional) – The maximum file size allowed in bytes. If set to a value greater than 0, the file size will be checked before upload. Example: `2000000` (2MB).

#### **Return Value:**

[](#return-value)

- **`string`** – Returns the destination path of the uploaded file on success (e.g., `'uploads/filename.ext'`).
- **Throws Exception** – If the file fails the type or size validation, or if the upload fails, an exception is thrown with a relevant error message.

#### **Exceptions:**

[](#exceptions)

The method may throw the following exceptions:

- **File upload error**: If the file upload fails due to an internal error (e.g., `$_FILES['file']['error']`).
- **Invalid file type**: If the uploaded file's MIME type does not match the allowed types.
- **File too large**: If the uploaded file exceeds the maximum allowed size.

---

**Example Usage:**
------------------

[](#example-usage-4)

```
try {
    // File to be uploaded
    $file = $_FILES['file'];

    // Destination path
    $destination = 'uploads/' . basename($file['name']);

    // Optional validation for file type and size
    $allowedTypes = ['image/jpeg', 'image/png'];
    $maxSize = 2000000; // 2MB

    // Attempt to upload the file
    $uploadedFile = FileHelper::upload($file, $destination, $allowedTypes, $maxSize);
    echo "File uploaded successfully: $uploadedFile";
} catch (\Exception $e) {
    // Handle any errors that occur during the upload
    echo "Error: " . $e->getMessage();
}
```

In this example:

- The file is uploaded with type and size validation.
- If the file is of the wrong type or too large, an exception is thrown with an error message.
- On successful upload, the file path is returned.

---

**Usage Notes:**
----------------

[](#usage-notes)

- If no validation is required, both `$allowedTypes` and `$maxSize` can be left empty or set to default values (empty array and 0, respectively).
- The MIME type of the uploaded file is determined using PHP's `mime_content_type()` function.
- The file is moved using PHP's `move_uploaded_file()` function, and the destination is returned on success.
- Exception handling should be implemented to catch errors related to file upload failures, invalid file types, or exceeding the file size limit.

---

### **Common Errors:**

[](#common-errors)

- **File upload failed:** This could be due to an invalid file, server misconfiguration, or permission issues.
- **Invalid file type:** The uploaded file type does not match any of the allowed MIME types.
- **File too large:** The uploaded file exceeds the maximum allowed size.

---

Here is the documentation for the `DateHelper` class:

---

**DateHelper Class Documentation**
==================================

[](#datehelper-class-documentation)

The `DateHelper` class provides utility functions for working with dates in PHP. It includes a method to format a date according to a specified format.

**Methods**
-----------

[](#methods-4)

### `format($date, $format = 'Y-m-d')`

[](#formatdate-format--y-m-d)

This method is used to format a given date into a specific format.

#### **Parameters:**

[](#parameters-4)

- **`$date`** (string|DateTime) – The date to be formatted. This can either be a string representing a date (e.g., `'2024-11-29'`) or a `DateTime` object.
- **`$format`** (string, optional) – The format in which the date should be returned. This is optional and defaults to `'Y-m-d'` (e.g., `2024-11-29`). You can use any format supported by PHP's `DateTime::format` method. For example, `'d/m/Y'` for `29/11/2024`, or `'l, F j, Y'` for `'Friday, November 29, 2024'`.

#### **Return Value:**

[](#return-value-1)

- **`string`** – Returns the formatted date as a string.

#### **Exceptions:**

[](#exceptions-1)

- **InvalidArgumentException** – If the provided `$date` cannot be parsed into a valid date, an exception will be thrown.

---

**Example Usage:**
------------------

[](#example-usage-5)

```
try {
    // Format a date string to the default format (Y-m-d)
    $formattedDate = DateHelper::format('2024-11-29');
    echo $formattedDate; // Output: 2024-11-29

    // Format a date string with a custom format (d/m/Y)
    $formattedDate = DateHelper::format('2024-11-29', 'd/m/Y');
    echo $formattedDate; // Output: 29/11/2024

    // Format a DateTime object with a custom format (l, F j, Y)
    $date = new \DateTime('2024-11-29');
    $formattedDate = DateHelper::format($date, 'l, F j, Y');
    echo $formattedDate; // Output: Friday, November 29, 2024
} catch (\Exception $e) {
    // Handle any errors that occur during the date formatting
    echo "Error: " . $e->getMessage();
}
```

In this example:

- A date string is formatted using the default format (`Y-m-d`).
- A custom format (`d/m/Y`) is used to format the date.
- A `DateTime` object is passed to the `format` method and is formatted as well.

---

**Usage Notes:**
----------------

[](#usage-notes-1)

- The `$date` parameter can be a string (e.g., `'2024-11-29'`) or an instance of `DateTime`. If a string is passed, it will be converted into a `DateTime` object internally.
- The `$format` parameter is optional. If not provided, the default format (`'Y-m-d'`) will be used. The format string can follow PHP’s `DateTime::format` syntax.
- If the provided `$date` string is not a valid date, the method will throw an exception.

---

### **Common Errors:**

[](#common-errors-1)

- **Invalid date format**: If the provided date string is invalid, it will cause a parsing error when creating a `DateTime` object.

Requirements
------------

[](#requirements)

- PHP 7.4 or higher.
- cURL extension enabled.

---

License
-------

[](#license)

This project is licensed under the MIT License. See the LICENSE file for details.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance50

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~46 days

Total

3

Last Release

349d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9ecbd0b8426be75421d5373603043caa34cd5b82e198faca3398dca82ed1a352?d=identicon)[bazarin](/maintainers/bazarin)

---

Top Contributors

[![BazarinTech](https://avatars.githubusercontent.com/u/161435099?v=4)](https://github.com/BazarinTech "BazarinTech (18 commits)")

### Embed Badge

![Health badge](/badges/bazarin-bazarin-php-library/health.svg)

```
[![Health](https://phpackages.com/badges/bazarin-bazarin-php-library/health.svg)](https://phpackages.com/packages/bazarin-bazarin-php-library)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M543](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
