PHP code example of mom / data

1. Go to this page and download the library: Download mom/data 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/ */

    

mom / data example snippets




namespace App\User\Properties;

use Mom\Data\AbstractString;

class Uuid extends AbstractString
{
    public static function getName(): string
    {
        return 'uuid';
    }
}



namespace App\User;

use Mom\Data\AbstractData;
use App\User\Properties\Uuid;

class User extends AbstractData
{
    public function __construct(
        private Uuid $uuid,
    ) {}
    
    public function getUuid(): Uuid
    {
        return $this->uuid;
    }
    
    public function setUuid(mixed $uuid): User
    {
        $this->uuid = new Uuid($uuid);
        
        return $this;
    }
}

use App\User\User;

$data = [
    'uuid' => '123e4567-e89b-12d3-a456-426614174000',
];

$user = User::fromArray($data);

echo $user->getUuid()->toString(); // Outputs: 123e4567-e89b-12d3-a456-426614174000



namespace App\User\Properties;

use Mom\Data\AbstractString;

class Uuid extends AbstractString
{
    public static function getName(): string
    {
        return 'uuid';
    }
    
    // Optionally, you can define the database column name if it is different from the getName()
    public static function getDatabaseTableColumnName() : string
    {
        return 'id' 
    }
}

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create(Table::Users->value, function (Blueprint $table): void {
            $table->uuid(Uuid::getDatabaseTableColumnName())->primary();
            $table->string(FirstName::getDatabaseTableColumnName());
            $table->string(LastName::getDatabaseTableColumnName());
            $table->string(Email::getDatabaseTableColumnName())->unique();
            $table->string(Password::getDatabaseTableColumnName())->nullable();
        );
    }
}



namespace App\User;

use Mom\Data\AbstractData;
use App\User\Properties\Uuid;
use App\User\Properties\FirstName;
use App\User\Properties\LastName;
use App\User\Properties\Email;
use App\User\Properties\Password;

class User extends AbstractData
{
    public function __construct(
        public Uuid $uuid,
        public FirstName $firstName,
        public LastName $lastName,
        public Email $email,
        public Password $password
    ) {}
    
    public function getUuid(): Uuid
    {
        return $this->uuid;
    }
    
    public function setUuid(mixed $uuid): User
    {
        $this->uuid = new Uuid($uuid);
        
        return $this;
    }
    
    public function getFirstName(): Uuid
    {
        return $this->firstName;
    }
    
    public function setFirstName(mixed $uuid): User
    {
        $this->uuid = new FirstName($uuid);
        
        return $this;
    }
    
    ...
}

use App\User\User;
use App\Models\User as UserEloquentModel

UserEloquentModel::query()->create([
    'uuid' => '123e4567-e89b-12d3-a456-426614174000'
    ... 
])

$model = UserEloquentModel::find('123e4567-e89b-12d3-a456-426614174000'); 

$user = User::fromEloquentModel($model);

echo $user->getUuid()->toString(); // Outputs: 123e4567-e89b-12d3-a456-426614174000