PHP code example of miladm / dou

1. Go to this page and download the library: Download miladm/dou library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

miladm / dou example snippets


$user = new User();
if (!$user->load(['id' => 12])) {
    return 'user not found!';
}
return $user->name;

$user = new User($_POST);
if(!$user->validate()) { // you must config validator 
    return 'bad request';
}
$user->load(); // must update load support DTO

$username = $_POST['username'] ?? false;
if (!$username) {
    return 'bad request';
}
$user = new User();
if (!$user->load(['name'=> $username]){
    return 'user not exist';
    // or return 'username and password does not match';
}
$password = $_POST['password'] ?? '';
if(!$user->checkPassword($password) ) {
    return 'username and password does not match';
}
$user->login();
//or
$token = $user->createAccessToken();

$user = new User();
if (!$user->load(12)) { // the same as ['id' => 12]
    throw new error();
}
array $postList = $user->getPosts(); // array in type of post DOU or []

$user = new User();
$user->load(12);
$user->name = 'other name';
$user->save(); // this will update user data on database

use miladm\table\Connection;

class MainConnection extends Connection
{
    public $host = "127.0.0.1";
    public $databaseName = "sample";
    public $user = 'root';
    public $password = 'root';
}


use miladm\Prototype;
use miladm\prototype\Schema;

class UserP extends Prototype
{
    public function init(): Schema
    {
        return $this->schema('user')
            ->string('name')
            ->email('email')
            ->hash('password')->hashFunction(fn ($data) => md5($data))
            ->json('something');
    }

    public function connection(): Connection
    {
        return new MainConnection;
    }
}

use miladm\DataObjectUnit;
use miladm\prototype\ModelHandler;

class User extends DataObjectUnit
{
    protected string $name;
    protected string $email;
    protected ?string $something;
    protected string $password;

    function model(): ModelHandler
    {
        return UserP::model();
    }
}

use miladm\dou\OnBeforeSave;

class User extends DataObjectUnit implements OnBeforeSave 
{
    protected string $name;
    protected string $email;
    protected ?string $something;
    protected string $password;
    protected array $roles = ['updateData'];

    function model(): ModelHandler
    {
        return UserP::model();
    }

    function onBeforeSave($data): array
    {
        if (!$this->hasPermissionTo('updateData')) {
            return [];
        }
        return $data;
    }

    private function hasPermissionTo($permissionString): bool
    {
        return in_array($permissionString, $this->roles);
    }
}

use miladm\dou\OnBeforeSet;

class User extends DataObjectUnit implements OnBeforeSet
{
    protected string $name;
    protected string $email;
    protected ?string $something;
    protected string $password;
    protected array $roles = ['updateData'];

    function model(): ModelHandler
    {
        return UserP::model();
    }

    function onBeforeSet($name, $value): mixed
    {
        if ($name === 'password') {
            if(!$this->passwordValidation($value)) {
                return $this->password;
            }
        }
        return $value;
    }

    private function passwordValidation($value): bool
    {
        // check if 8 character and has special characters and ect.
    }
}