Skip to content

Commit 118e632

Browse files
committed
update some
1 parent 821b24b commit 118e632

File tree

3 files changed

+183
-30
lines changed

3 files changed

+183
-30
lines changed

.github/workflows/release.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Tag-release
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
jobs:
9+
release:
10+
name: Test on php ${{ matrix.php}}
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 10
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [7.3]
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v2
21+
22+
- name: Set ENV for github-release
23+
# https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
24+
run: |
25+
echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
26+
echo "RELEASE_NAME=$GITHUB_WORKFLOW" >> $GITHUB_ENV
27+
28+
# usage refer https://github.com/shivammathur/setup-php
29+
- name: Setup PHP
30+
timeout-minutes: 5
31+
uses: shivammathur/setup-php@v2
32+
with:
33+
php-version: ${{ matrix.php}}
34+
tools: pecl, php-cs-fixer, phpunit
35+
extensions: mbstring, dom, fileinfo, mysql, openssl # , swoole-4.4.19 #optional, setup extensions
36+
ini-values: post_max_size=56M, short_open_tag=On #optional, setup php.ini configuration
37+
coverage: none #optional, setup coverage driver: xdebug, none
38+
39+
- name: Install dependencies # eg: v1.0.3
40+
run: |
41+
tag1=${GITHUB_REF#refs/*/}
42+
echo "release tag: ${tag1}"
43+
composer install --no-progress --no-suggest
44+
45+
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
46+
# Docs: https://getcomposer.org/doc/articles/scripts.md
47+
48+
# - name: Build phar and send to github assets
49+
# run: |
50+
# echo $RELEASE_TAG
51+
# echo $RELEASE_NAME
52+
# php -d phar.readonly=0 bin/kite phar:pack -o kite-${RELEASE_TAG}.phar --no-progress
53+
# php kite-${RELEASE_TAG}.phar -V
54+
55+
# https://github.com/actions/create-release
56+
- uses: meeDamian/github-release@2.0
57+
with:
58+
gzip: false
59+
token: ${{ secrets.GITHUB_TOKEN }}
60+
tag: ${{ env.RELEASE_TAG }}
61+
name: ${{ env.RELEASE_TAG }}
62+
# files: kite-${{ env.RELEASE_TAG }}.phar

.travis.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Valid.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use Inhere\Validate\Exception\ValidateException;
66
use function array_merge;
7+
use function count;
78
use function is_int;
89
use function is_numeric;
10+
use function is_scalar;
11+
use function is_string;
912

1013
/**
1114
* Class Valid - Simple Data Validator TODO
@@ -95,8 +98,126 @@ public static function getString(string $field, int $minLen = null, int $maxLen
9598
return 0;
9699
}
97100

101+
/**
102+
* @param string $field
103+
* @param int|null $min The value length must be >= $min
104+
* @param int|null $max The value length must be <= $min
105+
* @param array|null $default
106+
*
107+
* @return array
108+
*/
98109
public static function getStrings(string $field, int $min = null, int $max = null, array $default = null): array
99110
{
111+
if (!isset(self::$data[$field])) {
112+
if ($default === null) {
113+
throw new ValidateException($field, 'is required and must be string array');
114+
}
115+
116+
return $default;
117+
}
118+
119+
if (!$val = self::$data[$field]) {
120+
if ($default === null) {
121+
throw new ValidateException($field, 'is required and must be string array');
122+
}
123+
124+
return $default;
125+
}
126+
127+
if (is_scalar($val)) {
128+
return [(string)$val];
129+
}
130+
131+
return self::checkArrayValues($val, $field, $min, $max);
132+
}
133+
134+
/**
135+
* @param mixed $val
136+
* @param string $field
137+
* @param int|null $min
138+
* @param int|null $max
139+
*
140+
* @return array
141+
*/
142+
protected static function checkArrayValues($val, string $field, int $min = null, int $max = null): array
143+
{
144+
$arr = (array)$val;
145+
$len = count($arr);
146+
147+
// check min and max value
148+
if ($min !== null && $len < $min) {
149+
throw new ValidateException($field, "length must be greater or equal to $min");
150+
}
151+
if ($max !== null && $len > $max) {
152+
throw new ValidateException($field, "length must be less than or equal to $max");
153+
}
154+
155+
return $arr;
156+
}
157+
158+
/**
159+
* @param string $field
160+
* @param int|null $min
161+
* @param int|null $max
162+
* @param array|null $default
163+
*
164+
* @return array
165+
*/
166+
public static function getArray(string $field, int $min = null, int $max = null, array $default = null): array
167+
{
168+
if (!isset(self::$data[$field])) {
169+
if ($default === null) {
170+
throw new ValidateException($field, 'is required and must be array');
171+
}
172+
173+
return $default;
174+
}
175+
176+
if (!$val = self::$data[$field]) {
177+
if ($default === null) {
178+
throw new ValidateException($field, 'is required and must be array');
179+
}
180+
181+
return $default;
182+
}
183+
184+
if (is_scalar($val)) {
185+
return [$val];
186+
}
187+
188+
return self::checkArrayValues($val, $field, $min, $max);
189+
}
190+
191+
/**
192+
* @param string $field
193+
* @param int|null $min
194+
* @param int|null $max
195+
* @param array|null $default
196+
*
197+
* @return array
198+
*/
199+
public static function getArrayByJSON(string $field, int $min = null, int $max = null, array $default = null): array
200+
{
201+
if (!isset(self::$data[$field])) {
202+
if ($default === null) {
203+
throw new ValidateException($field, 'is required and must be JSON string');
204+
}
205+
206+
return $default;
207+
}
208+
209+
if (!$val = self::$data[$field]) {
210+
if ($default === null) {
211+
throw new ValidateException($field, 'is required and must be JSON string');
212+
}
213+
214+
return $default;
215+
}
216+
217+
if (!is_string($val)) {
218+
throw new ValidateException($field, 'must be an string');
219+
}
220+
100221
return [];
101222
}
102223
}

0 commit comments

Comments
 (0)