Skip to content

Commit

Permalink
readme, base file and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-q committed Jun 15, 2019
1 parent 655906d commit 93cb2db
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 61 deletions.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage_clover: clover.xml
json_path: coveralls.json
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/grumphp.yml export-ignore
/docs/ export-ignore
/phpcs.xml.dist export-ignore
/tests/ export-ignore
/.gitignore export-ignore
/README.md export-ignore
/CHANGELOG.md export-ignore
/CONTRIBUTING.md export-ignore
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.idea
/vendor/
composer.lock
.phpunit.result.cache
clover.xml
coveralls.json
.env
!.gitkeep
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php

php:
- 7.2
- 7.3
- nightly

matrix:
allow_failures:
- php: nightly

before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction

script:
- vendor/bin/grumphp run
- vendor/bin/phpunit tests --coverage-clover clover.xml

after_success:
- travis_retry php vendor/bin/php-coveralls

notifications:
email:
on_success: never
Empty file added CHANGELOG.md
Empty file.
21 changes: 21 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Contributing

We welcome contributions.

We accept contributions via pull requests on [Github](https://github.com/qlimix/encoding-json).

## Pull Requests

- [Qlimix/code-standard](https://github.com/qlimix/code-standard)

- Use the least possible magic (not __get() etc)

- Tests are required to ensure the quality of the code base

- Document changes in the CHANGELOG.md and relevant other doc files

- Use semantic versioning ([SemVer v2.0.0](http://semver.org/))

- Create feature branches, create a new branch for each feature

- Be respectful to other contributors
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 Frank Naber

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Encoding-json

[![Travis CI](https://api.travis-ci.org/qlimix/encoding-json.svg?branch=master)](https://travis-ci.org/qlimix/encoding-json)
[![Coveralls](https://img.shields.io/coveralls/github/qlimix/encoding-json.svg)](https://coveralls.io/qlimix/encoding-json)
[![Packagist](https://img.shields.io/packagist/v/qlimix/encoding-json.svg)](https://packagist.org/packages/qlimix/encoding-json)
[![MIT License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/qlimix/encoding-json/blob/master/LICENSE)

Encode and decode json based on the qlimix/encoding package.

## Install

Using Composer:

~~~
$ composer require qlimix/encoding-json
~~~

## usage

```php
<?php

use Qlimix/Encoding/Encode/JsonEncode;
use Qlimix/Encoding/Decode/JsonDecode;

$encode = new JsonEncode();
$decode = new JsonDecode();

$value = ['foo' => 'bar', 'number' => 1];

$encoded = $encode->encode($value);
$decode = $decode->decode($encoded);

```

## Testing
To run all unit tests locally with PHPUnit:

~~~
$ vendor/bin/phpunit
~~~

## Quality
To ensure code quality run grumphp which will run all tools:

~~~
$ vendor/bin/grumphp run
~~~

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
55 changes: 23 additions & 32 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
{
"name": "qlimix/encoding-json",
"type": "package",
"license": "LGPL-3.0-only",
"type": "library",
"description": "Encoding & decoding array's to string and vice versa",
"license": "MIT",
"keywords": [
"qlimix",
"encoding",
"decoding",
"decode",
"encode",
"json"
],
"authors": [
{
"name": "frank",
"email": "frank@qlimix.net"
}
],
"require": {
"ext-json": "*",
"php": ">=7.2",
"qlimix/encoding": "^1.0"
},
"autoload": {
"psr-4": {
"Qlimix\\Encoding\\": "src/"
}
},
"require": {
"qlimix/encoding": "dev-master",
"ext-json": "*"
},
"require-dev": {
"qlimix/code-standard": "dev-master",
"qlimix/dependency-container": "dev-master"
},
"suggest": {
"qlimix/dependency-container": "Encoding json providers"
"autoload-dev": {
"psr-4": {
"Qlimix\\Tests\\Encoding\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"repositories": [
{
"type": "vcs",
"url": "git@bitbucket.org:qlimix/codesniffer-standard.git"
},
{
"type": "vcs",
"url": "git@bitbucket.org:qlimix/code-standard.git"
},
{
"type": "vcs",
"url": "git@bitbucket.org:qlimix/encoding.git"
},
{
"type": "vcs",
"url": "git@bitbucket.org:qlimix/dependency-container.git"
}
],
"minimum-stability": "dev",
"prefer-stable": true
"require-dev": {
"qlimix/code-standard": "^1.0"
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.2/phpunit.xsd"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Test">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
21 changes: 20 additions & 1 deletion src/Decode/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,34 @@
namespace Qlimix\Encoding\Decode;

use Qlimix\Encoding\Decode\Exception\DecodeException;
use function json_decode;
use function json_last_error;
use const JSON_ERROR_NONE;

final class JsonDecode implements DecodeInterface
{
/** @var bool */
private $assoc;

/** @var int */
private $depth;

/** @var int */
private $options;

public function __construct(bool $assoc = true, int $depth = 512, int $options = 0)
{
$this->assoc = $assoc;
$this->depth = $depth;
$this->options = $options;
}

/**
* @inheritDoc
*/
public function decode(string $message): array
{
$json = json_decode($message, true);
$json = json_decode($message, $this->assoc, $this->depth, $this->options);

$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
Expand Down
21 changes: 20 additions & 1 deletion src/Encode/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,34 @@
namespace Qlimix\Encoding\Encode;

use Qlimix\Encoding\Encode\Exception\EncodeException;
use function json_encode;
use function json_last_error;
use const JSON_ERROR_NONE;

final class JsonEncode implements EncodeInterface
{
/** @var int */
private $options;

/** @var int */
private $depth;

public function __construct(int $options = 0, int $depth = 512)
{
$this->options = $options;
$this->depth = $depth;
}

/**
* @inheritDoc
*/
public function encode(array $message): string
{
$json = json_encode($message);
$json = json_encode($message, $this->options, $this->depth);

if ($json === false) {
throw new EncodeException('Failed to decode json');
}

$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
Expand Down
27 changes: 0 additions & 27 deletions src/Providers/JsonEncodingProvider.php

This file was deleted.

37 changes: 37 additions & 0 deletions tests/Decode/JsonDecodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Qlimix\Tests\Encoding\Decode;

use PHPUnit\Framework\TestCase;
use Qlimix\Encoding\Decode\Exception\DecodeException;
use Qlimix\Encoding\Decode\JsonDecode;

final class JsonDecodeTest extends TestCase
{
/** @var JsonDecode */
private $jsonDecode;

public function setUp(): void
{
$this->jsonDecode = new JsonDecode();
}

/**
* @test
*/
public function shouldDecode(): void
{
$this->jsonDecode->decode('{"foo":"bar"}');
$this->addToAssertionCount(1);
}

/**
* @test
*/
public function shouldThrowOnJsonDecodeError(): void
{
$this->expectException(DecodeException::class);

$this->jsonDecode->decode('{foo:"\xB1\x31"}');
}
}
Loading

0 comments on commit 93cb2db

Please sign in to comment.