PHP code example of adityasetiono / php-functions

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

    

adityasetiono / php-functions example snippets


use function adityasetiono\util\{deserialize, serialize, generate_alphanumeric};

$string = generate_alphanumeric(5);

// => "aR4z0"

// User.php
class User {
    protected $firstName;
    protected $lastName;
    protected $email;
    protected $username;
    
    public function setFirstName(string $firstName): User
    {
        $this->firstName = $firstName;
        
        return $this;
    }
    
    public function getFirstName(): string
    {
        return $this->firstName;
    }
    
    public function setLastName(string $lastName): User
    {
        $this->lastName = $lastName;
        
        return $this;
    }
    
    public function getLastName(): string
    {
        return $this->lastName;
    }
    
    public function setEmail(string $email): User
    {
        $this->email = $email;
        
        return $this;
    }
    
    public function getEmail(): string
    {
        return $this->email;
    }
    
    public function setUsername(string $username): User
    {
        $this->username = $username;
        
        return $this;
    }
    
    public function getUsername(): string
    {
        return $this->username;
    }
}

$user = serialize([
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => '[email protected]',
    'username' => 'johndoe'
], User::class);

echo $user->getFirstName(); // John
echo $user->getEmail(); // [email protected]

// User.php
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User {
    
    /**
     * @ORM\Id
     * @ORM\Column(type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     */
    protected $uuid;
    
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $firstName;
    
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $lastName;
    
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $email;
    
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $username;
    
    public function getUuid(): string
    {
        return $this->uuid;
    }
    
    public function setFirstName(string $firstName): User
    {
        $this->firstName = $firstName;
        
        return $this;
    }
    
    public function getFirstName(): string
    {
        return $this->firstName;
    }
    
    public function setLastName(string $lastName): User
    {
        $this->lastName = $lastName;
        
        return $this;
    }
    
    public function getLastName(): string
    {
        return $this->lastName;
    }
    
    public function setEmail(string $email): User
    {
        $this->email = $email;
        
        return $this;
    }
    
    public function getEmail(): string
    {
        return $this->email;
    }
    
    public function setUsername(string $username): User
    {
        $this->username = $username;
        
        return $this;
    }
    
    public function getUsername(): string
    {
        return $this->username;
    }
}

$user = serialize([
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => '[email protected]',
    'username' => 'johndoe'
], User::class);

// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
$entityManager->persist($user);
$entityManager->flush();

$uuid = 'e48a0ba1-1504-11e8-ae97-0649b2727d37'; // Hardcoded for display purposes
$user = $entityManager->find(User::class, $uuid);

$array = deserialise($user); // empty option means complete deserialization by default.
echo json_encode($array);

$customArray = deserialize($user,[
    'first_name' => 'firstName',
    'surname' => 'lastName',
    'email' => 'email',
]);
echo json_encode($customArray);