2017 © Pedro Peláez
 

library php-functions

Function helpers for PHP

image

adityasetiono/php-functions

Function helpers for PHP

  • Tuesday, April 3, 2018
  • by adityasetiono
  • Repository
  • 3 Watchers
  • 2 Stars
  • 265 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 24 Versions
  • 67 % Grown

The README.md

php-functions Build Status

A PHP library to serialise/deserialise an array to an object or vice versa., (*1)

Installation

To install it with composer:, (*2)

composer require adityasetiono/php-functions

Usage

Import needed functions, for example:, (*3)

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

Random String Generator

And to use it:, (*4)

$string = generate_alphanumeric(5);

// => "aR4z0"

Serialise / Deserialise

This is a function to serialise/deserialise an array to an object and vice versa. Because the library is built as an external function, the class properties are required to have getters and setters., (*5)

A class example, (*6)

// 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;
    }
}

Serialiser

$user = serialize([
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => 'john.doe@example.com',
    'username' => 'johndoe'
], User::class);

Result, (*7)

echo $user->getFirstName(); // John
echo $user->getEmail(); // john.doe@example.com

Serialised object are compatible with DoctrineORM Entities and can be persisted to database as is (Only tested with Doctrine so far, other ORM Entities need to be tested separately). This is just a generic serialiser to serialise an array to an object., (*8)

Here is an example of DoctrineORM Entity Serialization, (*9)

// 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;
    }
}

Serialiser

Uuid is a primary key and auto-generated by the DB., (*10)

$user = serialize([
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => 'john.doe@example.com',
    'username' => 'johndoe'
], User::class);

Persist to DB, (*11)

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

Deserialiser

Getting the object/entity from Database, (*12)

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

Use the deserialise, (*13)

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

Content of $array will be, (*14)

{
  "uuid": "e48a0ba1-1504-11e8-ae97-0649b2727d37",
  "firstName": "John",
  "lastName": "Doe",
  "email": "john.doe@example.com",
  "username": "johndoe"
}

Another example with custom fields. The second parameter accepts an associative array with key which will be the result's key and value is the property name. The deserialiser expects the value to be exactly the same as camelCase of the getter without get., (*15)

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

Content of $customArray will be, (*16)

{
  "first_name": "John",
  "surname": "Doe",
  "email": "john.doe@example.com"
}

The deserialiser should work with nested object and array of objects. Further example will be provided in the documentation. (It is available in the tests files)., (*17)

Issues

Feel free to use GitHub Issues or email directly to winged.zach@gmail.com as I started this library on my own. Collaborators are welcomed., (*18)

The Versions

03/04 2018

dev-adityasetiono-patch-1

dev-adityasetiono-patch-1

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

29/03 2018

dev-master

9999999-dev

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

29/03 2018

dev-update/docs

dev-update/docs

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

13/03 2018

5.0.0

5.0.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

04/12 2017

dev-feature/deserialize_array

dev-feature/deserialize_array

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

04/12 2017

5.0.0-RC1

5.0.0.0-RC1

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

17/11 2017

4.0.2

4.0.2.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

06/11 2017

4.0.1

4.0.1.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

06/11 2017

4.0.0

4.0.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

03/11 2017

3.0.1

3.0.1.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

03/11 2017

dev-tests/init

dev-tests/init

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

03/11 2017

3.0.0

3.0.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

The Development Requires

by Aditya Setiono

13/10 2017

2.0.2

2.0.2.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

12/10 2017

2.0.1

2.0.1.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

04/10 2017

2.0.0

2.0.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

08/08 2017

1.5.1

1.5.1.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

07/08 2017

1.5.0

1.5.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

07/08 2017

1.4

1.4.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

25/07 2017

1.3.1

1.3.1.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

25/07 2017

1.3.0

1.3.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

25/07 2017

1.2.0

1.2.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

25/07 2017

1.1.1

1.1.1.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

24/07 2017

1.1.0

1.1.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono

24/07 2017

1.0.0

1.0.0.0

Function helpers for PHP

  Sources   Download

MIT

The Requires

  • php ~7

 

by Aditya Setiono