PHP Serializable JSON and Array Entity: Dump objects to strings in JSON and Array formats

Recommend this page to a friend!
  Info   Documentation   View files (16)   Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 163 All time: 8,688 This week: 431
Version License PHP version Categories
php-serializable-ent 1.0Custom (specified...7Data types, PHP 7, Traits
Description Author

This package can dump objects to strings in JSON and Array formats.

It provides interfaces that can be used to define classes of objects so they can output the values of their properties in JSON or serialized array format.

Classes that do not want to implement the toJSON and to toArray functions can use a trait that provides these functions.

Innovation Award
PHP Programming Innovation award winner
March 2019
Winner


Prize: One official elePHPant Plush Mascott
Many applications have to use classes that need to save the values of their object variables to file, databases or other form of storage.

Usually they convert the values into string format like JSON or serialized arrays.

This package can add that functionality to any class by providing a trait that all classes can use to provide a built-in form of serialization to JSON or array formats.

Manuel Lemos
Picture of Niko
  Performance   Level  
Name: Niko <contact>
Classes: 7 packages by
Country: Finland Finland
Age: ???
All time rank: 31526 in Finland Finland
Week rank: 713 2 in Finland Finland
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Details

Serializable Entity

Convert entity into array or json. Apply JsonSerializable into entity by extending a class.

Install

Via composer:

composer require niko9911/niko9911/serializable-entity

Usage

You can use this to convert objects statically.

`EntityToArray::convert(object $entity ,[, int $recursionDepth = 2 [, bool $throwExceptionOnRecursionLimit = true ] [, bool $replaceValuesOnRecursionLimit = true ]]]): array`

Or if you want benefit from implementing \JsonSerializable interface you can extend class \Niko9911\Serializable\Serializable. In that case you will have 3 new methods in your class, toArray, toJson and jsonSerialize.

Example: Using Static Way

<?php
declare(strict_types=1);

// These is just our example entities.
final class Flag
{
    / @var string */
    private $mainColor;
    
    / @var int */
    private $height;
    
    / @var int */
    private $width;
    
    / @var bool */
    private $registered;
    
    public function __construct(
        string $mainColor,
        int $height,
        int $width,
        bool $registered
    ) 
    {
        $this->mainColor = $mainColor;
        $this->height = $height;
        $this->width = $width;
        $this->registered = $registered;
    }
    
    public  function getMainColor(): string
    {
        return $this->mainColor;
    }
    
    public  function getHeight(): int
    {
        return $this->height;
    }
    
    public  function getWidth(): int
    {
        return $this->width;
    }
    
    public  function getRegistered(): bool
    {
        return $this->registered;
    }
}

// It is not mandatory to extend. Only if you want benefit from
// implementing \JsonSerializable and having toArray, toJson methods.
final class Country extends \Niko9911\Serializable\Serializable
{
    // If you don't want implement \JsonSerializable,
    // but you want methods `toArray & toJson` into
    // you entity, you can add this trait.
    use \Niko9911\Serializable\SerializableTrait;
    
    / @var string  */
    private $name;
    
    / @var int  */
    private $id;
    
    / @var Flag */
    private $flag;
    
    public function __construct(string $name, int $id, Flag $flag) 
    {
        $this->name = $name;
        $this->id = $id;
        $this->flag = $flag;
    }
    
    public function getName(): string
    {
        return $this->name;
    }
    
    public function getId(): int
    {
        return $this->id;
    }
    
    public function getFlag(): Flag
    {
        return $this->flag;
    }
}

$entity = new Country('Finland', 358, new Flag('Blue', 150, 245, true));

$result1 = \Niko9911\Serializable\EntityToArray::convert($entity);
$result2 = $entity->toArray();
$result3 = $entity->toJson();
$result4 = \json_encode($entity);

var_dump($result1); // ['name'=>self::NAME,'id'=>self::CODE,'flag'=>['mainColor'=>self::MAIN,'height'=>self::SIZE[0],'width'=>self::SIZE[1],'registered'=>self::REGI,'options'=>[]]]
var_dump($result1 === $result2); // True

var_dump($result3); // {"name":"Finland","id":358,"flag":{"options":[],"mainColor":"Blue","height":150,"width":245,"registered":true}}
var_dump($result3 === $result4); // True

License

Licensed under the MIT license.

  Files  
File Role Description
scripts (1 file)
src (3 files, 1 directory)
tests (1 file, 1 directory)
.php_cs.dist Example Example script
composer.json Data Auxiliary data
composer.lock Data Auxiliary data
LICENSE Lic. License text
phpunit.xml.dist Data Auxiliary data
README.md Doc. Read me

  Files  /  scripts  
File Role Description
   composer.sh Data Auxiliary data

  Files  /  src  
File Role Description
Exception (3 files)
   EntityToArray.php Class Class source
   Serializable.php Class Class source
   SerializableTrait.php Class Class source

  Files  /  src  /  Exception  
File Role Description
   ConversionException.php Class Class source
   Exception.php Class Class source
   RecursionLimitException.php Class Class source

  Files  /  tests  
File Role Description
Stubs (2 files)
   EntityToArrayTest.php Class Class source

  Files  /  tests  /  Stubs  
File Role Description
   Country.php Class Class source
   Flag.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:163
This week:0
All time:8,688
This week:431

For more information send a message to info at phpclasses dot org.