PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (16)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-03-01 (8 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 159 All time: 8,650 This week: 372Up
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
  Performance   Level  
Name: Niko <contact>
Classes: 7 packages by
Country: Finland Finland
Age: ???
All time rank: 32296 in Finland Finland
Week rank: 312 Up1 in Finland Finland Up
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 folder image Files  
File Role Description
Files folder imagescripts (1 file)
Files folder imagesrc (3 files, 1 directory)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file .php_cs.dist Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  scripts  
File Role Description
  Accessible without login Plain text file composer.sh Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageException (3 files)
  Plain text file EntityToArray.php Class Class source
  Plain text file Serializable.php Class Class source
  Plain text file SerializableTrait.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file ConversionException.php Class Class source
  Plain text file Exception.php Class Class source
  Plain text file RecursionLimitException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageStubs (2 files)
  Plain text file EntityToArrayTest.php Class Class source

  Files folder image Files  /  tests  /  Stubs  
File Role Description
  Plain text file Country.php Class Class source
  Plain text file Flag.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:159
This week:0
All time:8,650
This week:372Up