2017 © Pedro Peláez
 

library doctrine-json-object-type

Doctrine Json Object Type

image

aurimasniekis/doctrine-json-object-type

Doctrine Json Object Type

  • Tuesday, May 16, 2017
  • by gcds
  • Repository
  • 1 Watchers
  • 2 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Doctrine Json Object Type

Latest Version Software License Build Status Code Coverage Quality Score Total Downloads, (*1)

Email, (*2)

Doctrine Json Object Type provides a ability to serialize/deserialize object which implements JsonObject interface to json and backwards., (*3)

Install

Via Composer, (*4)

$ composer require aurimasniekis/doctrine-json-object-type

Configuration

Symfony:, (*5)

doctrine:
    dbal:
        url: '%env(DATABASE_URL)%'
        types:
          json_object: AurimasNiekis\DoctrineJsonObjectType\JsonObjectType

Plain Doctrine:, (*6)

<?php

use Doctrine\DBAL\Types\Type;

Type::addType('json_object', 'AurimasNiekis\DoctrineJsonObjectType\JsonObjectType');

Usage

Value object should implement JsonObject interface., (*7)

<?php

use AurimasNiekis\DoctrineJsonObjectType\JsonObject;

class ValueObject implements JsonObject
{
    private $name;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public static function fromJson(array $data)
    {
        $inst = new self();

        $inst->setName($data['name']);

        return $inst;
    }

    public function jsonSerialize()
    {
        return [
            'name' => $this->getName()
        ];
    }
}

Entity, (*8)

<?php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="entity")
 */
class Entity
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * "json_object" is extended "json" type 
     * 
     * @var ValueObject
     *
     * @ORM\Column(type="json_object)
     */
    private $value;

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @return ValueObject
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param ValueObject $value
     */
    public function setValue(ValueObject $value)
    {
        $this->value = $value;
    }
}

Usage, (*9)

<?php

$value = new ValueObject();
$value->setName('foo_bar');

$entity = new Entity();
$entity->setValue($value);

$em->persist($entity);
$em->flush(); // INSERT INTO `entity` (`id`, `value`) VALUES (1, '{"name": "foo_bar", "__class": "ValueObject"}');


$findResult = $repo->find(1);

/// $findResult->getValue() === $value;

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*10)

License

Please see License File for more information., (*11)

The Versions

16/05 2017

dev-master

9999999-dev https://github.com/aurimasniekis/doctrine-json-object-type

Doctrine Json Object Type

  Sources   Download

MIT

The Requires

 

The Development Requires

json doctrine doctrine-json-object-type

16/05 2017

1.0.0

1.0.0.0 https://github.com/aurimasniekis/doctrine-json-object-type

Doctrine Json Object Type

  Sources   Download

MIT

The Requires

 

The Development Requires

json doctrine doctrine-json-object-type