-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from fergthh/multilang
Allow translate rules descriptions.
- Loading branch information
Showing
8 changed files
with
257 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Mpociot\ApiDoc\Parsers; | ||
|
||
class RuleDescriptionParser | ||
{ | ||
private $rule; | ||
|
||
private $parameters = []; | ||
|
||
/** | ||
* @param null $rule | ||
*/ | ||
public function __construct($rule = null) | ||
{ | ||
$this->rule = $rule; | ||
} | ||
|
||
/** | ||
* @return array|string | ||
*/ | ||
public function getDescription() | ||
{ | ||
$key = "apidoc::rules.{$this->rule}"; | ||
|
||
$description = $this->parameters ? $this->translateWithAttributes($key) : trans($key); | ||
|
||
return $description != $key ? $description : []; | ||
} | ||
|
||
/** | ||
* @param string|array $parameters | ||
* | ||
* @return $this | ||
*/ | ||
public function with($parameters) | ||
{ | ||
is_array($parameters) ? $this->parameters += $parameters : $this->parameters[] = $parameters; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $key | ||
* | ||
* @return string | ||
*/ | ||
protected function translateWithAttributes($key) | ||
{ | ||
$translate = trans($key); | ||
|
||
foreach ($this->parameters as $parameter) { | ||
$translate = preg_replace('/:attribute/', $parameter, $translate, 1); | ||
} | ||
|
||
return $translate; | ||
} | ||
|
||
/** | ||
* @param null $rule | ||
* | ||
* @return static | ||
*/ | ||
public static function parse($rule = null) | ||
{ | ||
return new static($rule); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
return [ | ||
'after' => 'Must be a date after: `:attribute`', | ||
'alpha' => 'Only alphabetic characters allowed', | ||
'alpha_dash' => 'Allowed: alpha-numeric characters, as well as dashes and underscores.', | ||
'alpha_num' => 'Only alpha-numeric characters allowed', | ||
'in' => ':attribute', | ||
'not_in' => 'Not in: :attribute', | ||
'min' => 'Minimum: `:attribute`', | ||
'max' => 'Maximum: `:attribute`', | ||
'between' => 'Between: `:attribute` and `:attribute`', | ||
'before' => 'Must be a date preceding: `:attribute`', | ||
'date_format' => 'Date format: `:attribute`', | ||
'different' => 'Must have a different value than parameter: `:attribute`', | ||
'digits' => 'Must have an exact length of `:attribute`', | ||
'digits_between' => 'Must have a length between `:attribute` and `:attribute`', | ||
'image' => 'Must be an image (jpeg, png, bmp, gif, or svg)', | ||
'json' => 'Must be a valid JSON string.', | ||
'mimetypes' => 'Allowed mime types: :attribute', | ||
'mimes' => 'Allowed mime types: :attribute', | ||
'required_if' => 'Required if `:attribute` is `:attribute`', | ||
'required_unless' => 'Required unless `:attribute` is `:attribute`', | ||
'required_with' => 'Required if the parameters :attribute are present.', | ||
'required_with_all' => 'Required if the parameters :attribute are present.', | ||
'required_without' => 'Required if the parameters :attribute are not present.', | ||
'required_without_all' => 'Required if the parameters :attribute are not present.', | ||
'same' => 'Must be the same as `:attribute`', | ||
'size' => 'Must have the size of `:attribute`', | ||
'timezone' => 'Must be a valid timezone identifier', | ||
'exists' => 'Valid :attribute :attribute', | ||
'regex' => 'Must match this regular expression: `:attribute`', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.