Skip to content

Commit 4f3ccc2

Browse files
committed
Title-case converter folder to make Travis unit-test work correctly
1 parent 141647e commit 4f3ccc2

File tree

9 files changed

+843
-0
lines changed

9 files changed

+843
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php namespace Bllim\Laravalid\Converter\Base;
2+
/**
3+
* This container class brings to extended class extendibility and also base convert function
4+
*
5+
* @package Laravel Validation For Client-Side
6+
* @author Bilal Gultekin <bilal@bilal.im>
7+
* @license MIT
8+
* @see Illuminate\Html\FormBuilder
9+
* @version 0.9
10+
*/
11+
12+
abstract class Container {
13+
14+
protected $customMethods = array();
15+
16+
public function convert($name, $parameters = array())
17+
{
18+
$methodName = strtolower($name);
19+
20+
if (isset($this->customMethods[$methodName]))
21+
{
22+
return call_user_func_array($this->customMethods[$methodName], $parameters);
23+
}
24+
25+
if (method_exists($this, $methodName))
26+
{
27+
return call_user_func_array(array($this, $methodName), $parameters);
28+
}
29+
30+
return null;
31+
}
32+
33+
public function extend($name, $function)
34+
{
35+
$this->customMethods[$name] = $function;
36+
}
37+
38+
}
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
<?php namespace Bllim\Laravalid\Converter\Base;
2+
/**
3+
* Base converter class for converter plugins
4+
*
5+
* @package Laravel Validation For Client-Side
6+
* @author Bilal Gultekin <bilal@bilal.im>
7+
* @license MIT
8+
* @see Illuminate\Html\FormBuilder
9+
* @version 0.9
10+
*/
11+
12+
abstract class Converter {
13+
14+
/**
15+
* Rule converter class instance
16+
*
17+
* @var Rule
18+
*/
19+
protected static $rule;
20+
21+
/**
22+
* Message converter class instance
23+
*
24+
* @var Message
25+
*/
26+
protected static $message;
27+
28+
/**
29+
* Route redirector class instance
30+
*
31+
* @var Route
32+
*/
33+
protected static $route;
34+
35+
/**
36+
* Laravel validation rules.
37+
*
38+
* @var array
39+
*/
40+
protected $validationRules = array();
41+
42+
protected static $multiParamRules = array(
43+
'between', 'digits_between',
44+
'in', 'not_in',
45+
'mimes',
46+
'required_if', 'required_with', 'required_with_all', 'required_without', 'required_without_all',
47+
'exists', 'unique',
48+
);
49+
50+
/**
51+
* Rules which specify input type is file
52+
*
53+
* @var array
54+
*/
55+
protected static $fileRules = array('image', 'mimes');
56+
57+
/**
58+
* Rules which specify input type is numeric
59+
*
60+
* @var array
61+
*/
62+
protected static $numericRules = array('integer', 'numeric', 'digits', 'digits_between');
63+
64+
/**
65+
* @var bool
66+
*/
67+
protected $useLaravelMessages;
68+
69+
/**
70+
* @param \Illuminate\Container\Container $app
71+
*/
72+
public function __construct($app)
73+
{
74+
/* @var $config \Illuminate\Config\Repository */
75+
$config = $app['config'];
76+
$routeUrl = $app['url']->to($config->get('laravalid::route', 'laravalid'));
77+
78+
$ns = substr($class = get_class($this), 0, strrpos($class, '\\')) . '\\';
79+
($class = $ns . 'Rule') and static::$rule = new $class($routeUrl, $app['encrypter']);
80+
($class = $ns . 'Message') and static::$message = new $class($app['translator']);
81+
($class = $ns . 'Route') and static::$route = new $class($app['validator'], $app['encrypter']);
82+
83+
$this->useLaravelMessages = $config->get('laravalid::useLaravelMessages', true);
84+
}
85+
86+
public function rule()
87+
{
88+
return static::$rule;
89+
}
90+
91+
public function message()
92+
{
93+
return static::$message;
94+
}
95+
96+
public function route()
97+
{
98+
return static::$route;
99+
}
100+
101+
/**
102+
* Set rules for validation
103+
*
104+
* @param array $rules Laravel validation rules
105+
*/
106+
public function set($rules)
107+
{
108+
if (isset($rules))
109+
$this->validationRules = (array)$rules;
110+
}
111+
112+
/**
113+
* Reset validation rules
114+
*/
115+
public function reset()
116+
{
117+
$this->validationRules = array();
118+
}
119+
120+
/**
121+
* Get all given validation rules
122+
*
123+
* @return array Laravel validation rules
124+
*/
125+
public function getValidationRules()
126+
{
127+
return $this->validationRules;
128+
}
129+
130+
/**
131+
* Returns validation rules for given input name
132+
*
133+
* @param string
134+
* @return array
135+
*/
136+
protected function getValidationRule($inputName)
137+
{
138+
return is_array($this->validationRules[$inputName])
139+
? $this->validationRules[$inputName]
140+
: explode('|', $this->validationRules[$inputName]);
141+
}
142+
143+
/**
144+
* Checks if there is a rules for given input name
145+
*
146+
* @param string
147+
* @return bool
148+
*/
149+
protected function checkValidationRule($inputName)
150+
{
151+
return isset($this->validationRules[$inputName]);
152+
}
153+
154+
public function convert($inputName, $inputType = null)
155+
{
156+
if (!$this->checkValidationRule($inputName)) {
157+
return array();
158+
}
159+
160+
$rules = $this->getValidationRule($inputName);
161+
$type = $this->getTypeOfInput($rules);
162+
163+
$outputAttributes = array();
164+
foreach ($rules as $rule)
165+
{
166+
$parsedRule = $this->parseValidationRule($rule);
167+
168+
$ruleAttributes = $this->rule()->convert($parsedRule['name'], array($parsedRule, $inputName, $type));
169+
if (!empty($ruleAttributes)) {
170+
$outputAttributes = $this->rule()->mergeOutputAttributes($outputAttributes, $ruleAttributes, $inputType);
171+
172+
if (empty($ruleAttributes)) continue;
173+
}
174+
175+
if ($this->useLaravelMessages)
176+
{
177+
$messageAttributes = $this->message()->convert($parsedRule['name'], array($parsedRule, $inputName, $type));
178+
179+
// if empty message attributes
180+
if (empty($messageAttributes) && !empty($ruleAttributes))
181+
{
182+
$messageAttributes = $this->getDefaultErrorMessage($parsedRule['name'], $inputName);
183+
}
184+
185+
if (!empty($messageAttributes))
186+
$outputAttributes += $messageAttributes;
187+
}
188+
}
189+
190+
return $outputAttributes;
191+
}
192+
193+
/**
194+
* Get all rules and return type of input if rule specifies type
195+
*
196+
* @param array
197+
* @return string
198+
*/
199+
protected function getTypeOfInput($rulesOfInput)
200+
{
201+
foreach ($rulesOfInput as $key => $rule) {
202+
$parsedRule = $this->parseValidationRule($rule);
203+
204+
if (in_array($parsedRule['name'], static::$numericRules))
205+
{
206+
return 'numeric';
207+
}
208+
elseif ($parsedRule['name'] === 'array')
209+
{
210+
return 'array';
211+
}
212+
elseif (in_array($parsedRule['name'], static::$fileRules))
213+
{
214+
return 'file';
215+
}
216+
}
217+
218+
return 'string';
219+
}
220+
221+
/**
222+
* Parses validation rule of laravel
223+
*
224+
* @param string
225+
* @return array
226+
*/
227+
protected function parseValidationRule($rule)
228+
{
229+
$ruleArray = array();
230+
231+
$parameters = explode(':', $rule, 2);
232+
$ruleArray['name'] = array_shift($parameters);
233+
234+
if (empty($parameters) || !in_array(strtolower($ruleArray['name']), static::$multiParamRules)) {
235+
$ruleArray['parameters'] = $parameters;
236+
} else {
237+
$ruleArray['parameters'] = str_getcsv($parameters[0]);
238+
}
239+
240+
return $ruleArray;
241+
}
242+
243+
/**
244+
* Gets default error message
245+
*
246+
* @param string $laravelRule
247+
* @param string $attribute
248+
* @return string
249+
*/
250+
protected function getDefaultErrorMessage($laravelRule, $attribute)
251+
{
252+
// getting user friendly validation message
253+
$message = $this->message()->getValidationMessage($attribute, $laravelRule);
254+
return array('data-msg-' . $laravelRule => $message);
255+
}
256+
257+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php namespace Bllim\Laravalid\Converter\Base;
2+
3+
use Illuminate\Support\Str;
4+
5+
/**
6+
* Some description...
7+
*
8+
* @package Laravel Validation For Client-Side
9+
* @author Bilal Gultekin <bilal@bilal.im>
10+
* @license MIT
11+
* @see Illuminate\Html\FormBuilder
12+
* @version 0.9
13+
*/
14+
15+
abstract class Message extends Container {
16+
17+
/**
18+
* @var \Illuminate\Translation\Translator
19+
*/
20+
protected $translator;
21+
22+
public function __construct($translator)
23+
{
24+
$this->translator = $translator;
25+
}
26+
27+
/**
28+
* Get user friendly validation message
29+
*
30+
* @param string $attribute
31+
* @param string $rule
32+
* @param array $data
33+
* @param string $type
34+
* @return string
35+
* @see Illuminate\Validation\Validator::getMessage()
36+
*/
37+
public function getValidationMessage($attribute, $rule, $data = array(), $type = null)
38+
{
39+
$path = Str::snake($rule);
40+
if ($type !== null)
41+
{
42+
$path .= '.' . $type;
43+
}
44+
45+
if ($this->translator->has('validation.custom.' . $attribute . '.' . $path))
46+
{
47+
$path = 'custom.' . $attribute . '.' . $path;
48+
}
49+
50+
$niceName = $this->getValidationAttribute($attribute);
51+
52+
return $this->translator->get('validation.' . $path, $data + array('attribute' => $niceName));
53+
}
54+
55+
protected function getValidationAttribute($attribute)
56+
{
57+
$niceName = $this->translator->get($langKey = 'validation.attributes.' . $attribute);
58+
if ($niceName === $langKey) {
59+
$niceName = str_replace('_', ' ', Str::snake($attribute));
60+
}
61+
62+
return $niceName;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)