Skip to content

Commit 2be3c12

Browse files
committed
initial commit
0 parents  commit 2be3c12

11 files changed

+1374
-0
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = false
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 4
9+
charset = "utf-8"
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.yml]
15+
indent_style = space
16+
indent_size = 2

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+
.phpunit.result.cache

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# CakePHP4 Messages Validator plugin
2+
3+
CakePHP4 Validator that set the validation message
4+
5+
## Requirements
6+
7+
The master branch has the following requirements:
8+
9+
* CakePHP >4.0.0
10+
* PHP 7.2 or greater
11+
12+
## Installation
13+
14+
You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).
15+
16+
The recommended way to install composer packages is:
17+
18+
```
19+
composer require tyrellsys/cakephp-validator
20+
```
21+
22+
no need plugin load
23+
24+
## Config
25+
26+
no configuration
27+
28+
## Model validation
29+
30+
```php
31+
namespace App\Model\Table;
32+
33+
...
34+
35+
class PostsTable extends Table
36+
{
37+
protected $_validatorClass = \Tyrellsys\CakePHPValidator\Validation\Validator::class;
38+
}
39+
```

bin/createValidator

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env bash
2+
#
3+
# create validator
4+
#
5+
6+
APP=$(cd $(dirname $0)/../; pwd)
7+
8+
# 除外関数
9+
# addNested addNestedMany
10+
EXCEPTS=(
11+
"addNested"
12+
"addNestedMany"
13+
#
14+
"allowEmpty"
15+
"allowEmptyFor"
16+
#
17+
"notEmpty"
18+
"regex"
19+
)
20+
21+
VALIDATOR_METHODS=$(grep "public function" $APP/vendor/cakephp/cakephp/src/Validation/Validator.php | grep \$message)
22+
23+
cat << '_EOL_'
24+
<?php
25+
declare(strict_types=1);
26+
27+
namespace Tyrellsys\CakePHPValidator\Validation;
28+
29+
use Cake\Validation\Validator as CakeValidator;
30+
31+
class Validator extends CakeValidator
32+
{
33+
_EOL_
34+
35+
IFS=$'\n'
36+
for method in $VALIDATOR_METHODS; do
37+
methodName=$(echo $method | sed 's/.*public function \(.*\)(.*/\1/')
38+
39+
if [[ " ${EXCEPTS[@]} " =~ " ${methodName} " ]]; then
40+
continue;
41+
fi
42+
43+
args=()
44+
IFS=" "
45+
for arg in $(echo $method | awk -F'(' '{print $2}'); do
46+
if [ ${arg:0:1} = '$' ]; then
47+
args+=($(echo $arg | sed 's/,//'))
48+
fi
49+
done
50+
51+
strArgs=$(IFS=,;echo "${args[*]}" | sed 's/,/, /g')
52+
translate='__('\'$methodName\'')'
53+
54+
# hasAtLeast / hasAtMost
55+
if [[ " ${args[@]} " =~ " \$count " ]]; then
56+
translate='__('\'$methodName' {0}'\'', $count)';
57+
fi
58+
59+
# minLength / minLengthBytes
60+
if [[ " ${args[@]} " =~ " \$min " ]]; then
61+
translate='__('\'$methodName' {0}'\'', $min)';
62+
fi
63+
64+
# maxLength / maxLengthBytes
65+
if [[ " ${args[@]} " =~ " \$max " ]]; then
66+
translate='__('\'$methodName' {0}'\'', $max)';
67+
fi
68+
69+
# lengthBetween / range
70+
if [[ " ${args[@]} " =~ " \$range " ]]; then
71+
translate='__('\'$methodName' {0} to {1}'\'', $range[0], $range[1])';
72+
fi
73+
74+
echo ' /**'
75+
echo ' * {@inheritDoc}'
76+
echo ' */'
77+
echo ' '$method
78+
echo ' {'
79+
echo ' $message = $message ?? '$translate';'
80+
echo
81+
echo ' return parent::'$methodName'('$strArgs');'
82+
echo ' }'
83+
echo
84+
done
85+
86+
echo '}'

composer.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "tyrellsys/cakephp-validator",
3+
"description": "CakePHP4 Validator that set the validation message",
4+
"type": "cakephp-plugin",
5+
"require": {
6+
"php": ">=7.2",
7+
"cakephp/cakephp": "^4.0"
8+
},
9+
"require-dev": {
10+
"cakephp/cakephp-codesniffer": "~4.0.0",
11+
"phpunit/phpunit": "^8.5"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"Tyrellsys\\CakePHPValidator\\": "src"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Cake\\Test\\": "vendor/cakephp/cakephp/tests",
21+
"Tyrellsys\\CakePHPValidator\\Test\\": "tests"
22+
}
23+
},
24+
"scripts": {
25+
"check": [
26+
"@cs-check",
27+
"@test"
28+
],
29+
"test": "phpunit",
30+
"cs-check": "phpcs --colors -p src/ tests/",
31+
"cs-fix": "phpcs --colors -p src/ tests/"
32+
}
33+
}

phpcs.xml.dist

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="CakePHP Core">
3+
<rule ref="./vendor/cakephp/cakephp-codesniffer/CakePHP/ruleset.xml">
4+
<!-- Exclude unwanted sniffs -->
5+
<exclude name="Generic.Commenting.Todo.TaskFound"/> <!-- Excluded during 3.next development -->
6+
</rule>
7+
8+
<!-- Necessary for class aliases used for backwards compat -->
9+
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
10+
<severity>0</severity>
11+
</rule>
12+
</ruleset>

phpunit.xml.dist

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
colors="true"
4+
processIsolation="false"
5+
stopOnFailure="false"
6+
bootstrap="./tests/bootstrap.php"
7+
>
8+
<php>
9+
<ini name="memory_limit" value="-1"/>
10+
<ini name="apc.enable_cli" value="1"/>
11+
</php>
12+
13+
<!-- Add any additional test suites you want to run here -->
14+
<testsuites>
15+
<testsuite name="Localized Test Suite">
16+
<directory>./tests/TestCase</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<!-- Prevent coverage reports from looking in tests and vendors -->
21+
</phpunit>

src/Plugin.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tyrellsys\CakePHPValidator;
5+
6+
use Cake\Core\BasePlugin;
7+
8+
class Plugin extends BasePlugin
9+
{
10+
}

0 commit comments

Comments
 (0)