**JsonShelter** is a lightweight JSON database library for PHP, enabling easy CRUD operations for structured data. Simple file-based storage, intuitive interface, and unique record management make data handling effortless. Perfect for prototypes and small applications.
Welcome to JsonShelter! This is a nifty PHP library designed to help you store and manage your JSON data with ease. Letβs dive into the installation and usage instructions! π
You can bring JsonShelter into your project using one of the methods below:
Use Composer to include the package seamlessly in your project:
composer require almhdy/json-shelter
Prefer to have a local copy? Clone the repository directly from GitHub:
git clone https://github.com/almhdy24/JsonShelter.git
You can also download a release archive from GitHub. Simply visit the releases page and grab the latest version as a ZIP file. Extract it to your project directory.
Using JsonShelter in your project is easy-peasy! Include the class by using Composer or directly. Letβs walk through it!
If you used Composer, just include the autoload file and get started! Hereβs an example:
// Include Composer's autoload file
require 'vendor/autoload.php';
// Use the JsonShelter namespace
use Almhdy\JsonShelter.JsonShelter;
// Create a new JsonShelter instance
$baseDir = "myDatabase"; // Base directory path
$secretKey = "your_secret_key"; // Your secret key
$secretIv = "your_secret_iv"; // Your secret IV
$logger = new Logger('json_shelter');
$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG));
$db = new JsonShelter($baseDir, $secretKey, $secretIv, $logger);
If you cloned the repo or downloaded the archive, include the JsonShelter.php
file directly. Adjust the path as needed!
// Include the JsonShelter class file
require 'path/to/JsonShelter.php'; // Set the correct path
// Use the JsonShelter namespace
use Almhdy.JsonShelter.JsonShelter;
// Create a new JsonShelter instance
$baseDir = "myDatabase"; // Base directory path
$secretKey = "your_secret_key"; // Your secret key
$secretIv = "your_secret_iv"; // Your secret IV
$logger = new Logger('json_shelter');
$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG));
$db = new JsonShelter($baseDir, $secretKey, $secretIv, $logger);
You can easily enable or disable encryption for your records. This is a handy feature to keep your data secure!
$db->enableEncryption();
$db->disableEncryption();
You can perform CRUD operations (Create, Read, Update, Delete) with the following methods:
// Create a new record in 'myTable'
$db->create('myTable', ['name' => 'John', 'age' => 30]);
// Read a record from 'myTable' by ID
$record = $db->read('myTable', 1); // Replace 1 with the record ID
print_r($record); // Display the retrieved record
// Update a record in 'myTable' by ID
$db->update('myTable', 1, ['age' => 31]); // Increment age
// Delete a record from 'myTable' by ID
$db->delete('myTable', 1); // Replace 1 with the record ID
The Model
class provides an ORM-like interface to interact with the JsonShelter
class.
First, initialize a Model
for a specific table:
use Almhdy.JsonShelter.Model;
// Initialize a Model for the 'users' table
$userModel = new Model($db, 'users');
Inserting data using the Model
:
// Insert a new record
$userModel->create(['name' => 'Jane Doe', 'email' => '[email protected]']);
Reading all records:
// Read all records
$users = $userModel->all();
print_r($users);
Reading a specific record by ID:
// Read a specific record by ID
$user = $userModel->find(1);
print_r($user);
Updating a record:
// Update a record
$userModel->update(1, ['email' => '[email protected]']);
Deleting a record:
// Delete a record
$userModel->delete(1);
Filtering records:
// Get users with the name 'Jane Doe'
$users = $userModel->where(['name' => 'Jane Doe']);
print_r($users);
Searching within a field:
// Search for users with 'example.com' in their email
$users = $userModel->search('email', 'example.com');
print_r($users);
Ordering and limiting data:
// Order users by name in ascending order
$orderedUsers = $userModel->orderBy('name', 'asc');
print_r($orderedUsers);
// Limit the number of users returned
$limitedUsers = $userModel->limit(10, 0);
print_r($limitedUsers);
The JsonEncryptor
class handles encryption and decryption of data.
Initialize the JsonEncryptor
class with a secret key and initialization vector:
use Almhdy.JsonShelter.JsonEncryptor;
// Initialize JsonEncryptor
$encryptor = new JsonEncryptor('your_secret_key', 'your_secret_iv');
Encrypting data:
$data = ['name' => 'Jane Doe', 'email' => '[email protected]'];
$encryptedData = $encryptor->encrypt($data);
echo $encryptedData;
Decrypting data:
$decryptedData = $encryptor->decrypt($encryptedData);
print_r($decryptedData);
Encrypting a large file using streams:
$inputStream = fopen('path/to/large_file.json', 'rb');
$outputStream = fopen('path/to/encrypted_file.enc', 'wb');
$encryptor->encryptStream($inputStream, $outputStream);
fclose($inputStream);
fclose($outputStream);
Decrypting a large file using streams:
$inputStream = fopen('path/to/encrypted_file.enc', 'rb');
$outputStream = fopen('path/to/decrypted_file.json', 'wb');
$encryptor->decryptStream($inputStream, $outputStream);
fclose($inputStream);
fclose($outputStream);
Our database manager π, now affectionately called βdb,β comes packed with handy extra methods π that make managing JSON files and checking their directory status a breeze! Letβs explore them.
Before diving into the JSON realm, make sure your directory is in tip-top shape! This method checks if itβs readable and writable:
$status = $db->checkDirectoryStatus();
print_r($status);
Curious about your JSON files? This method gives you the lowdown on their size and permissions:
$directoryInfo = $db->getJsonFilesInfo();
print_r($directoryInfo);
Letβs tighten up security! This method automatically sets the best permissions for your JSON files:
$permissionResults = $db->setBestPermissionsForJsonFiles();
print_r($permissionResults);
And thatβs a wrap! π Youβve now unlocked the power of JsonShelter, making it super simple to integrate into your PHP applications! π With just a few easy steps, youβre ready to harness the power of file-based JSON data management, complete with encryption and ORM-like capabilities.
Remember, every line of code brings you closer to your goals. Embrace the journey and happy coding! ππ»β¨