caiochami/bundles

A laravel set of tools for non-laravel projects

v1.9 2020-12-16 01:49 UTC

This package is auto-updated.

Last update: 2025-06-16 11:27:30 UTC


README

A laravel-like set of tools for non-laravel projects

Installation

[composer] composer require caiochami/bundles

Usage

The [Request] class gathers and validates all http input data. If errors are found at the end of validation, it will display the information in json format. That behavior changes when no "Content-Type: application/json" is present in the headers. It will redirect to the previous page with the errors and the request data attached to the $_SESSION variable.

  1. Instantiate the class Request. Passing in a PDO instance is required when using the rule "exists".
  2. Specify the rules
  3. (Optional) Specify custom messages

Let's say we are seding a json request with the following data.

[
    "name" : "John Doe",
    "city" : "",
    "password" : "12345678",
    "password_confirmation" : "1234568",
    "id" : null,
    "patients": [
        {
            "name": "Foo",
            "age": 2
        },
        {
            "name": "Bar",
            "age": 10
        }
    ]
];

The validation will be like this:

require "vendor/autoload.php";

header("Content-Type: application/json;");

use Bundles\Request;

$request = new Request;

//all empty string variables are converted to null automatically
$request->validate([
    "name" => ["required","string", "minimum:3", "maximum:100"],
    "password" => ["required", "string", "confirmed"],
    "id" => ["nullable", "integer"],
    "patients" => ["required", "array", "gte:1"],
    "patients.*.name" => ["required", "string"],
    "patients.*.age" => ["required", "integer"]
],
[
    "required" => "The field %s is required"
]);

Let's assume we have a users table and addresses table. The model class have all the methods you need to create, update, delete and query entries. Check out the following code:

 require "vendor/autoload.php";

 use Bundles\Model;
 use Bundles\DatabaseConnection as Database;

 class User extends Model
 {

    protected static $tableName = "users";

    protected static $columns = "id,name,address.city";

    protected static $joins = "";

    protected static $key = "id";

    public function addresses(){
        $addresses = DB::use(self::$conn)
        ->table('addresses')
        ->select(['id', 'city'])
        ->where('user_id', $this->id)
        ->retrieve()
        ->get();
   
        $this->addresses = $addresses;
   
    }

 }

class Address extends Model
 {

    protected static $tableName = "addresses";

    protected static $columns = "id,city";

    protected static $joins = "";

    protected static $key = "id";

    protected static $fillable = [ "city" ];

    public function user(){
        $user = DB::use(self::$conn)
        ->table('users')
        ->select(['id', 'name'])
        ->where('id', $this->id)
        ->retrieve()
        ->first();
   
        $this->user = $user;
   
    }

    //mutator
    
    public function setCityAttribute($value){
        return strtolower($value);
    }


 }

 $connection = Database::attempt([
    "db_host" =>  "HOST",
    "db_name" =>  "NAME",
    "db_user" =>  "USER",
    "db_psw" =>  "PASSWORD"
])

 //creating a user
 $user = new User($connection);
 $user->name = "Foo";
 $user->save();

 //or

 $address = Address::create(["city"=> "My homeland"]);

 //finding and updating the user

 $user = User::find($connection, 1); 
 //or 
 $address = Address::use($connection)->where("id", 1)->first();

 $user->name = "Bar";
 $user->save();

 //or

 User::update($connection, 1, [
     "name" => "Bar"
 ]);

 // deleting a user

 User::destroy($connection, 1);

 $user = User::find($connection, 1);
 $user->delete();

 //querying all addresses

 Address::all($connection);

 //or more specific

 User::use($connection)
 ->where("name", "LIKE", "%bar%")
 ->whereBetween("birthday", "1993-01-01", "2020-01-01"),
 ->orWhere("address.city", "California")
 ->with(['addresses'])
 ->limit(15)
 ->retrieve()
 ->get();


//for debugging you can pass an array just like this
User::find($connection, 1, ["debug" => true]);

//or 

User::where("birthday", ">", "1972-11-11")
->orderBy(["name", "DESC"])
->retrieve(["debug" => true, "show_query" => true ])
->get(); 

For more examples, check out the examples/ folder