PHP code example of midorikocak / arraytools

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

    

midorikocak / arraytools example snippets





declare(strict_types=1);

namespace midorikocak\arraytools;

class User implements ArrayConvertableInterface, ArrayUpdateableInterface
{
    use ArrayConvertableTrait;
    use ArrayUpdateableTrait;

    private ?string $id;
    private string $username;
    private string $email;
    private string $password;

    public function __construct(?string $id, string $username, string $email, string $password)
    {
        $this->id = $id;
        $this->username = $username;
        $this->password = $password;
        $this->email = $email;
    }

    public function getId(): ?string
    {
        return $this->id;
    }

    public function setId(string $id)
    {
        $this->id = $id;
    }

    // Getters and setters

        public function toArray(): array
        {
            $toReturn = [
                'username' => $this->getUsername(),
                'email' => $this->getEmail(),
                'password' => $this->getPassword(),
            ];
    
            if ($this->getId()) {
                $toReturn['id'] = $this->getId();
            }
    
            return $toReturn;
        }
    
        public static function fromArray($array): User
        {
            $id = $array['id'] ?? null;
            $username = $array['username'];
            $email = $array['email'];
            $password = $array['password'];
    
            return new User($id, $username, $email, $password);
        }



declare(strict_types=1);

namespace midorikocak\arraytools;

class User implements ArrayConvertableInterface
{
    use ArrayConvertableTrait;

    private ?string $id;
    private string $username;
    private string $email;
    private string $password;

    // rest

$userData = [
    'id' => '1',
    'username' => 'midorikocak',
    'password' => '24738956349lhksbvlhf',
    'email' => '[email protected]',
];


$user = new User(
    $userData['id'] ?? null,
    $userData['username'],
    $userData['email'],
    $userData['password']
);

$userArray = $user->toArray();
print_r($userArray);


$userData = [
    'username' => 'midorikocak',
    'password' => '24738956349lhksbvlhf',
    'email' => '[email protected]',
];
        
$user = User::fromArray($userData);


$userData = [
    'id' => '2',
    'username' => 'update',
    'password' => 'updatedpw',
    'email' => '[email protected]',
];
        
$user->setFromArray($userData);

use midorikocak\arraytools\ArrayValidator;

$arrayValidator = new ArrayValidator();

use midorikocak\arraytools\ArrayValidator;

$toValidate = [
    'id'=>'1',
    'username'=>'uname',
    'password'=>''
];

$arrayValidator = new ArrayValidator();
$arrayValidator->hasKey('id');
echo $arrayValidator->validate($toValidate); // true

$arrayValidator->hasKey('hasan');
echo $arrayValidator->validate($toValidate); // false

$arrayValidator->hasKeys('id', 'username');
echo $arrayValidator->validate($toValidate); // true

$arrayValidator->keys('id', 'username', 'password');
echo $arrayValidator->validate($toValidate); // true

$arrayValidator->notEmpty('password');
echo $arrayValidator->validate($toValidate); // false

echo $arrayValidator
        ->keys('id', 'username', 'password')
        ->notEmpty('password')
        ->hasKeys('id', 'username')
        ->validate($toValidate); // false

$schema = [
    'username' => 'string',
    'password' => 'string',
    'email' => 'email',
];

$arrayValidator->schema($schema); // true

echo $arrayValidator->validate($toValidate, function($array){
    return array_key_exists('key', $array);
}); // false