Skip to content

Commit

Permalink
Merge pull request #1 from m1x0n/feature/translator-support
Browse files Browse the repository at this point in the history
Added translator support.
  • Loading branch information
m1x0n committed Oct 4, 2019
2 parents cfae73d + 4c2bf91 commit 1edb003
Show file tree
Hide file tree
Showing 49 changed files with 292 additions and 357 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ install:
- travis_retry composer install --no-interaction --prefer-dist

script:
- vendor/bin/phpcs
- vendor/bin/phpstan analyse -l 4 src
- vendor/bin/phpunit

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"opis/json-schema": "^1.0",
"phpunit/phpunit": ">=7.1",
"dg/bypass-finals": "^1.1",
"phpstan/phpstan": "^0.11.5"
"phpstan/phpstan": "^0.11.5",
"squizlabs/php_codesniffer": "^3.4"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 10 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>

<ruleset name="Opis Error Presenter">
<description>Opis Error Presenter Rules</description>

<rule ref="PSR12"/>

<file>./src</file>
<file>./tests</file>
</ruleset>
2 changes: 1 addition & 1 deletion src/Contracts/Keyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface Keyword
self::CONTAINS,
self::PATTERN_PROPERTIES,
self::ALL_OF,
self::ANY_OF.
self::ANY_OF ,
self::ONE_OF,
self::NOT,
];
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/MessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace OpisErrorPresenter\Contracts;

use Opis\JsonSchema\ValidationError;

interface MessageFormatter
{
public function format(ValidationError $error): string;
public function keyword(): string;

public function replacements(): array;
}
10 changes: 10 additions & 0 deletions src/Contracts/MessageTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace OpisErrorPresenter\Contracts;

interface MessageTranslator
{
public function translate(string $key, array $replacements = [], $locale = null): string;
}
1 change: 1 addition & 0 deletions src/Contracts/PresentStrategy.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace OpisErrorPresenter\Contracts;
Expand Down
12 changes: 2 additions & 10 deletions src/Implementation/Formatters/AdditionalItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class AdditionalItems implements MessageFormatter
class AdditionalItems extends Formatter
{
private const MESSAGE = 'The attribute must not contain additional items.';

public function format(ValidationError $error): string
{
return self::MESSAGE;
}
public const MESSAGE = 'The attribute must not contain additional items.';
}
12 changes: 2 additions & 10 deletions src/Implementation/Formatters/AdditionalProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class AdditionalProperties implements MessageFormatter
class AdditionalProperties extends Formatter
{
private const MESSAGE = 'The attribute must not contain additional properties.';

public function format(ValidationError $error): string
{
return self::MESSAGE;
}
public const MESSAGE = 'The attribute must not contain additional properties.';
}
12 changes: 2 additions & 10 deletions src/Implementation/Formatters/AllOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class AllOf implements MessageFormatter
class AllOf extends Formatter
{
private const MESSAGE = 'The attribute must be valid against all of the subschemas.';

public function format(ValidationError $error): string
{
return self::MESSAGE;
}
public const MESSAGE = 'The attribute must be valid against all of the subschemas.';
}
12 changes: 2 additions & 10 deletions src/Implementation/Formatters/AnyOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class AnyOf implements MessageFormatter
class AnyOf extends Formatter
{
private const MESSAGE = 'The attribute does not match any of the subschemas.';

public function format(ValidationError $error): string
{
return self::MESSAGE;
}
public const MESSAGE = 'The attribute does not match any of the subschemas.';
}
19 changes: 6 additions & 13 deletions src/Implementation/Formatters/Constant.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class Constant implements MessageFormatter
class Constant extends Formatter
{
private const MESSAGE = "The attribute value expected to be ':expected:'.";
public const MESSAGE = "The attribute value expected to be ':expected:'.";

public function format(ValidationError $error): string
public function replacements(): array
{
$expected = $error->keywordArgs()['expected'];

$replacements = [
':expected:' => $expected
return [
':expected:' => $this->error->keywordArgs()['expected']
];

return strtr(self::MESSAGE, $replacements);
}
}
12 changes: 2 additions & 10 deletions src/Implementation/Formatters/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class Contains implements MessageFormatter
class Contains extends Formatter
{
private const MESSAGE = 'The attribute contains invalid items.';

public function format(ValidationError $error): string
{
return self::MESSAGE;
}
public const MESSAGE = 'The attribute contains invalid items.';
}
17 changes: 6 additions & 11 deletions src/Implementation/Formatters/Dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class Dependencies implements MessageFormatter
class Dependencies extends Formatter
{
private const MESSAGE = "The attribute property ':missing:' is required.";
public const MESSAGE = "The attribute property ':missing:' is required.";

public function format(ValidationError $error): string
public function replacements(): array
{
$missing = $error->keywordArgs()['missing'];

$replacements = [':missing:' => $missing];

return strtr(self::MESSAGE, $replacements);
return [
':missing:' => $this->error->keywordArgs()['missing']
];
}
}
12 changes: 2 additions & 10 deletions src/Implementation/Formatters/ElseKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class ElseKeyword implements MessageFormatter
class ElseKeyword extends Formatter
{
private const MESSAGE = "The attribute does not match schema for 'else'.";

public function format(ValidationError $error): string
{
return self::MESSAGE;
}
public const MESSAGE = "The attribute does not match schema for 'else'.";
}
24 changes: 13 additions & 11 deletions src/Implementation/Formatters/Enum.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php

declare(strict_types=1);

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class Enum implements MessageFormatter
class Enum extends Formatter
{
private const MESSAGE = 'The attribute must be one of the following values: :expected:.';
public const MESSAGE = 'The attribute must be one of the following values: :expected:.';

public function format(ValidationError $error): string
public function replacements(): array
{
$expected = $error->keywordArgs()['expected'];
$keywordArgs = $this->error->keywordArgs();
$expected = (array)$keywordArgs['expected'];

$replacements = [
':expected:' => "'" . implode ("', '", $expected) . "'",
return [
':expected:' => implode(', ', array_map(
function ($item) {
return "'{$item}'";
},
$expected
)),
];

return strtr(self::MESSAGE, $replacements);
}
}
17 changes: 6 additions & 11 deletions src/Implementation/Formatters/ExclusiveMaximum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class ExclusiveMaximum implements MessageFormatter
class ExclusiveMaximum extends Formatter
{
private const MESSAGE = 'The attribute value must be less than :max:.';
public const MESSAGE = 'The attribute value must be less than :max:.';

public function format(ValidationError $error): string
public function replacements(): array
{
$max = $error->keywordArgs()['max'];

$replacements = [':max:' => $max];

return strtr(self::MESSAGE, $replacements);
return [
':max:' => $this->error->keywordArgs()['max']
];
}
}
17 changes: 6 additions & 11 deletions src/Implementation/Formatters/ExclusiveMinimum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class ExclusiveMinimum implements MessageFormatter
class ExclusiveMinimum extends Formatter
{
private const MESSAGE = 'The attribute value must be greater than :min:.';
public const MESSAGE = 'The attribute value must be greater than :min:.';

public function format(ValidationError $error): string
public function replacements(): array
{
$min = $error->keywordArgs()['min'];

$replacements = [':min:' => $min];

return strtr(self::MESSAGE, $replacements);
return [
':min:' => $this->error->keywordArgs()['min']
];
}
}
14 changes: 5 additions & 9 deletions src/Implementation/Formatters/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

class Format implements MessageFormatter
class Format extends Formatter
{
private const MESSAGE = "The attribute should match ':format:' format.";
public const MESSAGE = "The attribute should match ':format:' format.";

public function format(ValidationError $error): string
public function replacements(): array
{
$format = $error->keywordArgs()['format'] ?? null;
$replacements = $format ? [':format:' => $format] : [];
$format = $this->error->keywordArgs()['format'] ?? null;

return strtr(self::MESSAGE, $replacements);
return $format ? [':format:' => $format] : [];
}
}
31 changes: 31 additions & 0 deletions src/Implementation/Formatters/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace OpisErrorPresenter\Implementation\Formatters;

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;

abstract class Formatter implements MessageFormatter
{
/**
* @var ValidationError
*/
protected $error;

public function __construct(ValidationError $error)
{
$this->error = $error;
}

public function keyword(): string
{
return $this->error->keyword();
}

public function replacements(): array
{
return [];
}
}
15 changes: 4 additions & 11 deletions src/Implementation/Formatters/InvalidAttribute.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<?php
declare(strict_types = 1);

namespace OpisErrorPresenter\Implementation\Formatters;
declare(strict_types=1);

use Opis\JsonSchema\ValidationError;
use OpisErrorPresenter\Contracts\MessageFormatter;
namespace OpisErrorPresenter\Implementation\Formatters;

class InvalidAttribute implements MessageFormatter
class InvalidAttribute extends Formatter
{
private const MESSAGE = 'The attribute is invalid.';

public function format(ValidationError $error): string
{
return self::MESSAGE;
}
public const MESSAGE = 'The attribute is invalid.';
}
Loading

0 comments on commit 1edb003

Please sign in to comment.