Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.lock
  • Loading branch information
Dropelikeit committed May 20, 2020
2 parents f12aaba + 2aa3bd6 commit cc45a34
Show file tree
Hide file tree
Showing 10 changed files with 767 additions and 465 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_name: travis-ci
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
60 changes: 60 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Project language
language: php

# Allows use container-based infrastructure
sudo: false

# Start mysql service
#services:
# - mysql

# Cache composer packages so "composer install" is faster
cache:
directories:
- $HOME/.composer/cache/files

# Matrix to test in every php version
matrix:
# Fast finish allows to set the build as "finished" even if the "allow_failures" matrix elements are not finished yet.
fast_finish: true
include:
- php: 7.3
allow_failures:
- php: hhvm

# Define an environment variable
#env:
#- SYMFONY_VERSION="3.0.*" DB=mysql

# Update composer
before-install:
- composer self-update

# Install composer dependencies,
# Create database, schema and fixtures
install:
- composer install
- wget -c -nc --retry-connrefused --tries=0 https://github.com/php-coveralls/php-coveralls/releases/download/v2.0.0/php-coveralls.phar
- chmod +x php-coveralls.phar
- php php-coveralls.phar --version
#- cp app/config/parameters.yml.dist app/config/parameters.yml
#- php bin/console doctrine:database:create --env=test
#- php bin/console doctrine:schema:create --env=test
#- php bin/console doctrine:fixtures:load -n --env=test
#- php bin/console assets:install

# Run script
script:
- composer test
- phpunit --coverage-clover build/logs/clover.xml
- composer cs-check

after_success:
- travis_retry php php-coveralls.phar -v


# After a build, send email notification with the build results
notifications:
email:
on_success: never
on_failure: always
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
[![Build Status](https://travis-ci.org/Dropelikeit/laravel-jms-serializer.svg?branch=master)](https://travis-ci.org/Dropelikeit/PriceCalculator)
[![Coverage Status](https://coveralls.io/repos/github/Dropelikeit/laravel-jms-serializer/badge.svg)](https://coveralls.io/github/Dropelikeit/laravel-jms-serializer)
[![Monthly Downloads](https://poser.pugx.org/dropelikeit/laravel-jms-serializer/d/monthly)](https://packagist.org/packages/dropelikeit/laravel-jms-serializer)
[![Daily Downloads](https://poser.pugx.org/dropelikeit/laravel-jms-serializer/d/daily)](https://packagist.org/packages/dropelikeit/laravel-jms-serializer)
[![Total Downloads](https://poser.pugx.org/dropelikeit/laravel-jms-serializer/downloads)](https://packagist.org/packages/dropelikeit/laravel-jms-serializer)
[![Latest Stable Version](https://poser.pugx.org/dropelikeit/laravel-jms-serializer/v/stable)](https://packagist.org/packages/dropelikeit/laravel-jms-serializer)
[![Total Downloads](https://poser.pugx.org/dropelikeit/laravel-jms-serializer/downloads)](https://packagist.org/packages/dropelikeit/laravel-jms-serializer)
[![License](https://poser.pugx.org/dropelikeit/laravel-jms-serializer/license)](https://packagist.org/packages/dropelikeit/laravel-jms-serializer)
[![composer.lock](https://poser.pugx.org/dropelikeit/laravel-jms-serializer/composerlock)](https://packagist.org/packages/dropelikeit/laravel-jms-serializer)

# JMS Serializer for Laravel

This package integrates the JMS serializer into Laravel.

JMS-Serializer: https://github.com/schmittjoh/serializer

You are also welcome to use the Issue Tracker to set bugs, improvements or upgrade requests.

### Installation

``` composer require dropelikeit/laravel-jms-serializer ```



### How to use

Laravel 5.5 and later uses Package Auto-Discovery, so you do not need to add the service provider manually.
For Laravel versions below 5.5, the package must be added manually, add the following line in the "providers" array in config/app.php:

```php
Dropelikeit\LaravelJmsSerializer\ServiceProvider::class,
```

For example, to use the JMS serializer in a controller, add the ResponseFactory in the constructor.

```php
<?php
namespace App\Http\Controller;

use Dropelikeit\LaravelJmsSerializer\ResponseFactory;
use Symfony\Component\HttpFoundation\JsonResponse;

class ExampleController extends Controller
{
/**
* @var ResponseFactory
*/
private $responseFactory;

public function __construct(ResponseFactory $responseFactory)
{
$this->responseFactory = $responseFactory;
}

public function myAction(): JsonResponse
{
$myDataObjectWithSerializerAnnotations = new Object('some data');
return $this->responseFactory->create($myDataObjectWithSerializerAnnotations);
}
}
```

Publish the Serializer Config with the command

```bash
php artisan vendor:publish
```

After that you will see a config file in your config folder named "laravel-jms-serializer.php" with the following content:


```php
<?php
return [
'serialize_null' => true,
'serialize_type' => Config\Config::SERIALIZE_TYPE_JSON,
'debug' => false,
];
```

As you can see zero values are serialized by default.

## Using Custom-Context

To use your own JMS contexts, use the "withContext" method

To learn more about JMS context, read the JMS Serializer documentation: http://jmsyst.com/libs/serializer/master

```php
<?php
namespace App\Http\Controller;

use Dropelikeit\LaravelJmsSerializer\ResponseFactory;
use Symfony\Component\HttpFoundation\JsonResponse;
use JMS\Serializer\SerializationContext;

class ExampleController extends Controller
{
/**
* @var ResponseFactory
*/
private $responseFactory;

public function __construct(ResponseFactory $responseFactory)
{
$this->responseFactory = $responseFactory;
}

public function myAction(): JsonResponse
{
$myDataObjectWithSerializerAnnotations = new Object('some data');

$context = SerializationContext::create()->setSerializeNull(true);

$this->responseFactory->withContext($context);
return $this->responseFactory->create($myDataObjectWithSerializerAnnotations);
}
}
```

## Using Status-Code

You do not always want to hand over a status code of 200 to the frontend. You can achieve this with the following code. Use the method "withStatusCode" for this

```php
<?php
namespace App\Http\Controller;

use Dropelikeit\LaravelJmsSerializer\ResponseFactory;
use Symfony\Component\HttpFoundation\JsonResponse;

class ExampleController extends Controller
{
/**
* @var ResponseFactory
*/
private $responseFactory;

public function __construct(ResponseFactory $responseFactory)
{
$this->responseFactory = $responseFactory;
}

public function myAction(): JsonResponse
{
$myDataObjectWithSerializerAnnotations = new Object('some data');

$this->responseFactory->withStatusCode(400);
return $this->responseFactory->create($myDataObjectWithSerializerAnnotations);
}
}
```



4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
"cs-check": "php-cs-fixer -v --dry-run --using-cache=no fix",
"cs-fix": "php-cs-fixer --using-cache=no fix",
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover build/logs/clover.xml",
"check": [
"@cs-check",
"@test"
"@test",
"@test-coverage"
]
}
}
Loading

0 comments on commit cc45a34

Please sign in to comment.