Skip to content

Commit

Permalink
Merge pull request #1 from tsantos84/2.0
Browse files Browse the repository at this point in the history
Major Version 2.0
  • Loading branch information
tsantos84 authored Dec 23, 2017
2 parents b0b5db1 + 450272c commit 3433642
Show file tree
Hide file tree
Showing 83 changed files with 2,627 additions and 282 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ cache:
matrix:
include:
- php: 7.1
- php: 7.2
fast_finish: true

install:
- composer install -a
- composer require symfony/yaml

script:
- vendor/bin/phpunit
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Change Log

## [1.1.1](https://github.com/tsantos84/serializer/tree/1.1.1) (2017-11-24)
[Full Changelog](https://github.com/tsantos84/serializer/compare/1.1.0...1.1.1)

## [1.1.0](https://github.com/tsantos84/serializer/tree/1.1.0) (2017-11-23)
[Full Changelog](https://github.com/tsantos84/serializer/compare/1.0.0...1.1.0)

## [1.0.0](https://github.com/tsantos84/serializer/tree/1.0.0) (2017-11-21)


\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
57 changes: 22 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,58 @@ Flex recipe: comming soon!

## Usage

The best way to get start with `Tsantos Serializer` is by using the builder. With a few configurations you are ready to serialize your data:
The best way to get start with `TSantos Serializer` is by using the builder. With a few configurations you are ready to serialize your data:

```php
$builder = new SerializerBuilder();

$serializer = $builder
->addMetadataDir('My\Namespace\Entity', 'path/to/my/mapping')
->build();
$serializer = $builder->build();

$person = new Person(100, 'Tales Santos');

echo $serializer->serialize($person, 'json'); // {"id":100, "name":"Tales Santos"}
echo $serializer->serialize($person); // {"id":100, "name":"Tales Santos"}
```

All objects should have a mapping file describing the properties and its configuration.

```yaml
# path/to/my/mapping/Person.yaml
My\Namespace\Entity\Person:
properties:
id:
type: integer
name:
type: string
```
Unlike other libraries, only those properties described in the mapping file will be serialized and all properties should have a public `getter` method.
Unlike other libraries, all properties should either be `public` or have a public `getter` and `setter` methods.

```php
# path/to/my/entities
namespace My\Namespace\Entity

class Person
{
private $id;
private $name;
public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}
public function getId() { return $this->id; }
public function getName() { return $this->name; }
/** @var string */
public $name;
public $age;
private $colors;

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

/** @return integer */
public function getAge(): int { return $this->age; }
public function setAge(int $age) { $this->age = $age; }

/** @return array */
public function getColors(): array { return $this->colors; }
public function setColors(array $colors) { $this->colors = $colors; }
}
```

## Features

Current features supported by TSantos Serializer:
Features currently supported by TSantos Serializer:

* Supports `PHP`, `YAML`, `XML` and `InMemory (array)` mapping formats
* Supports `PHP`, `YAML`, `XML`, `Reflection`, `Annotations` and `InMemory (array)` mapping formats
* Supports `JSON` encoders (output)
* Serializes objects of any depth
* Custom `getters`
* (De-)serializes objects of any depth
* Custom `getters` and `setters`
* Virtual properties
* Properties grouping

## Call for contribution

This project is still in development and are fully open to your contribution. Want to contribute? Choose your feature bellow and lets discuss about it:

* Ability to deserialize objects
* Support for `XML` and `CSV` encoders (output)
* Support for ready configuration from annotations
* Documentation
* Any idea?
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
},
"require": {
"php": "^7.1",
"jms/metadata": "^1.6"
"jms/metadata": "^1.6",
"doctrine/annotations": "^1.5",
"doctrine/instantiator": "^1.1",
"symfony/event-dispatcher": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^6.2"
Expand Down
2 changes: 1 addition & 1 deletion example/1 - simple_object.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
$person->setLastName('Santos');
$person->setMarried(true);

$json = $serializer->serialize($person, 'json');
$json = $serializer->serialize($person);

echo $json;
2 changes: 1 addition & 1 deletion example/2 - multi_depth_object.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
$address->setCoordinates(new Coordinates(10.5, 20.9));
$person->setAddress($address);

$json = $serializer->serialize($person, 'json');
$json = $serializer->serialize($person);

echo $json;
2 changes: 1 addition & 1 deletion example/3 - collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
$persons[] = $person;
}

$json = $serializer->serialize($persons, 'json');
$json = $serializer->serialize($persons);

echo $json;
2 changes: 1 addition & 1 deletion example/4 - groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

$person = new Person(1, 'Tales Santos', true);

$json = $serializer->serialize($person, 'json', SerializationContext::create()->setGroups(['web']));
$json = $serializer->serialize($person, SerializationContext::create()->setGroups(['web']));

echo $json;
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="TSantos Serializer Test Suite">
<directory suffix="Test.php">./tests/</directory>
Expand Down
95 changes: 95 additions & 0 deletions src/AbstractContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of the TSantos Serializer package.
*
* (c) Tales Santos <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace TSantos\Serializer;

/**
* Trait ContextTrait
* @package TSantos\Serializer
*/

abstract class AbstractContext
{
/** @var array */
private $groups = ['Default'];

/** @var integer */
private $maxDepth;

/** @var integer */
private $currentDepth;

/**
* @return self
*/
public static function create(): self
{
return new static();
}

/**
* @param array $groups
* @return self
*/
public function setGroups(array $groups): self
{
$this->groups = $groups;
return $this;
}

public function getGroups(): array
{
return $this->groups;
}

/**
* @param $maxDepth
* @return self
*/
public function setMaxDepth($maxDepth): self
{
$this->maxDepth = $maxDepth;
return $this;
}

public function start()
{
if ($this->currentDepth > 0) {
throw new \LogicException('The context cannot be restarted');
}

$this->currentDepth = 0;
}

public function isStarted(): bool
{
return null !== $this->currentDepth;
}

public function enter($object = null)
{
$this->currentDepth++;
}

public function left()
{
$this->currentDepth--;
}

public function isMaxDeepAchieve(): bool
{
// infinite depth as the maxDepth wasn't provided
if (null === $this->maxDepth) {
return false;
}

return $this->currentDepth === $this->maxDepth;
}
}
41 changes: 17 additions & 24 deletions src/AbstractSerializerClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
*/

namespace TSantos\Serializer;

use Metadata\ClassMetadata;
use TSantos\Serializer\EventDispatcher\EventDispatcherInterface;

/**
* Class AbstractSerializerClass
Expand All @@ -19,36 +18,30 @@
*/
abstract class AbstractSerializerClass implements SerializerClassInterface
{
protected $classMetadata;
/**
* @var SerializerInterface
*/
protected $serializer;

public function __construct(Serializer $serializer, ClassMetadata $metadata)
{
$this->serializer = $serializer;
$this->classMetadata = $metadata;
}
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;

/**
* @param string $property
* @param SerializationContext $context
* @return bool
* @var \SplObjectStorage
*/
protected function isPropertyGroupExposed(string $property, SerializationContext $context)
{
$propertyGroups = $this->classMetadata->propertyMetadata[$property]->groups;
$contextGroups = $context->getGroups();
return count(array_intersect($propertyGroups, $contextGroups)) > 0;
}
protected $computedGroupKeys;

/**
* @param string $property
* @param SerializationContext $context
* @return bool
* AbstractSerializerClass constructor.
* @param SerializerInterface $serializer
* @param EventDispatcherInterface $dispatcher
*/
protected function isVirtualPropertyGroupExposed(string $property, SerializationContext $context)
public function __construct(SerializerInterface $serializer, EventDispatcherInterface $dispatcher)
{
$propertyGroups = $this->classMetadata->methodMetadata[$property]->groups;
$contextGroups = $context->getGroups();
return count(array_intersect($propertyGroups, $contextGroups)) > 0;
$this->serializer = $serializer;
$this->computedGroupKeys = new \SplObjectStorage();
$this->dispatcher = $dispatcher;
}
}
45 changes: 45 additions & 0 deletions src/DeserializationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of the TSantos Serializer package.
*
* (c) Tales Santos <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace TSantos\Serializer;

/**
* Class SerializationContext
*
* @package Serializer
* @author Tales Santos <[email protected]>
*/
class DeserializationContext extends AbstractContext
{
/**
* @var object
*/
private $target;

/**
* @return object
*/
public function getTarget()
{
return $this->target;
}

/**
* @param object $target
*/
public function setTarget($target): void
{
if (!is_object($target)) {
throw new \InvalidArgumentException('The $target should be an object, ' . gettype($target) . ' given');
}

$this->target = $target;
}
}
6 changes: 6 additions & 0 deletions src/Encoder/EncoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ interface EncoderInterface
*/
public function encode(array $data): string;

/**
* @param string $content
* @return array
*/
public function decode(string $content): array;

/**
* @return string
*/
Expand Down
Loading

0 comments on commit 3433642

Please sign in to comment.