PHP code example of isayalcintr / baseobjects

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

    

isayalcintr / baseobjects example snippets


use isayalcintr\BaseObjects\BaseObject;

class UserObject extends BaseObject
{
    private ?string $name = null;
    private ?string $email = null;

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;
        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(?string $email): self
    {
        $this->email = $email;
        return $this;
    }
}

// Kullanım
$user = (new UserObject())->initFromArray([
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

print_r($user->toArray());

use isayalcintr\BaseObjects\BaseFilterObject;
use Illuminate\Database\Eloquent\Builder;

class UserFilter extends BaseFilterObject
{
    protected ?string $name = null;

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;
        return $this;
    }

    public function filterToName(Builder $query): Builder
    {
        return $query->where('name', 'like', '%' . $this->name . '%');
    }

    public function isFilterableName(): bool
    {
        return !is_null($this->name);
    }
}

// Kullanım
$query = \App\Models\User::query();
$filter = (new UserFilter())->setName('John');
$query = $filter->apply($query);

$users = $query->get();
print_r($users->toArray());
sh
php artisan vendor:publish --tag=base-objects