Skip to content

Commit 64d4bd8

Browse files
PHP 8.1 compatibility (#72)
* PHP 8.1 compatibility * update github action to run tests for php 8.1 * Force the version of CS Fixer * Update PHPUnit before running tests
1 parent a2e8df0 commit 64d4bd8

17 files changed

+408
-50
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
php-version: ['7.1', '7.2', '7.3', '7.4', '8.0']
12+
php-version: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
1313
composer-prefer:
1414
- '--prefer-dist'
1515
- '--prefer-stable --prefer-lowest'
@@ -39,7 +39,9 @@ jobs:
3939
restore-keys: |
4040
${{ runner.os }}-php-
4141
- name: Install dependencies
42-
run: composer update ${{ matrix.composer-prefer }} --no-progress
42+
run: |
43+
composer update ${{ matrix.composer-prefer }} --no-progress
44+
composer update phpunit/phpunit --no-progress
4345
4446
- name: Run Code Style Check for PHP ${{ matrix.php-version }}
4547
run: composer run-script style-check

.install-cs-fixer.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

33
if [ ! -f ./php-cs-fixer ]; then
4-
wget https://cs.symfony.com/download/php-cs-fixer-v3.phar -O php-cs-fixer
4+
wget https://cs.symfony.com/download/v3.4.0/php-cs-fixer.phar -O php-cs-fixer
55
chmod +x php-cs-fixer
66
fi

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
FROM php:8.0-cli
1+
FROM php:8.1-cli
22

3-
RUN apt-get update
3+
RUN apt-get update -y
44

55
## PHP dependencies
66
RUN pecl install xdebug \
@@ -11,4 +11,4 @@ RUN curl -sS https://getcomposer.org/installer | php \
1111
&& apt-get install git unzip -y
1212
ENV COMPOSER_ALLOW_SUPERUSER=1
1313
ENV XDEBUG_MODE=coverage
14-
WORKDIR /src
14+
WORKDIR /src

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"jmikola/geojson": "^1.0"
1212
},
1313
"require-dev": {
14-
"phpunit/phpunit": ">=7.5",
14+
"phpunit/phpunit": ">=7.5 <10.0",
1515
"php-coveralls/php-coveralls": "^2.4",
1616
"psy/psysh": "@stable",
1717
"roave/security-advisories": "dev-latest"

src/Fields/BaseField.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
abstract class BaseField
99
{
10+
/**
11+
* @param object $descriptor
12+
*/
1013
public function __construct($descriptor = null)
1114
{
1215
$this->descriptor = empty($descriptor) ? (object) [] : $descriptor;

src/Fields/DateField.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace frictionlessdata\tableschema\Fields;
44

55
use Carbon\Carbon;
6+
use frictionlessdata\tableschema\Utility\StrptimeFormatTransformer;
67

78
class DateField extends BaseField
89
{
@@ -21,13 +22,13 @@ protected function validateCastValue($val)
2122
}
2223
} else {
2324
$format = 'default' === $this->format() ? self::DEFAULT_FORMAT : $this->format();
24-
$date = strptime($val, $format);
25+
$date = date_parse_from_format(StrptimeFormatTransformer::transform($format), $val);
2526

26-
if (false === $date || '' != $date['unparsed']) {
27+
if ($date['error_count'] > 0) {
2728
throw $this->getValidationException("couldn't parse date/time according to given strptime format '{$format}''", $val);
2829
} else {
2930
return Carbon::create(
30-
(int) $date['tm_year'] + 1900, (int) $date['tm_mon'] + 1, (int) $date['tm_mday'],
31+
(int) $date['year'], (int) $date['month'], (int) $date['day'],
3132
0, 0, 0
3233
);
3334
}

src/Fields/DatetimeField.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace frictionlessdata\tableschema\Fields;
44

55
use Carbon\Carbon;
6+
use frictionlessdata\tableschema\Utility\StrptimeFormatTransformer;
67

78
class DatetimeField extends BaseField
89
{
@@ -33,13 +34,13 @@ protected function validateCastValue($val)
3334
throw $this->getValidationException($e->getMessage(), $val);
3435
}
3536
default:
36-
$date = strptime($val, $this->format());
37-
if (false === $date || '' != $date['unparsed']) {
37+
$date = date_parse_from_format(StrptimeFormatTransformer::transform($this->format()), $val);
38+
if ($date['error_count'] > 0) {
3839
throw $this->getValidationException("couldn't parse date/time according to given strptime format '{$this->format()}''", $val);
3940
} else {
4041
return Carbon::create(
41-
(int) $date['tm_year'] + 1900, (int) $date['tm_mon'] + 1, (int) $date['tm_mday'],
42-
(int) $date['tm_hour'], (int) $date['tm_min'], (int) $date['tm_sec']
42+
(int) $date['year'], (int) $date['month'], (int) $date['day'],
43+
(int) $date['hour'], (int) $date['minute'], (int) $date['second']
4344
);
4445
}
4546
}

src/Fields/TimeField.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace frictionlessdata\tableschema\Fields;
44

55
use Carbon\Carbon;
6+
use frictionlessdata\tableschema\Utility\StrptimeFormatTransformer;
67

78
/**
89
* Class TimeField
@@ -32,11 +33,15 @@ protected function validateCastValue($val)
3233

3334
return $this->getNativeTime($dt->hour, $dt->minute, $dt->second);
3435
default:
35-
$date = strptime($val, $this->format());
36-
if (false === $date || '' != $date['unparsed']) {
36+
$date = date_parse_from_format(
37+
StrptimeFormatTransformer::transform($this->format()),
38+
$val
39+
);
40+
41+
if ($date['error_count'] > 0) {
3742
throw $this->getValidationException(null, $val);
3843
} else {
39-
return $this->getNativeTime($date['tm_hour'], $date['tm_min'], $date['tm_sec']);
44+
return $this->getNativeTime($date['hour'], $date['minute'], $date['second']);
4045
}
4146
}
4247
}

src/Table.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public function save($outputDataSource)
160160
* @throws Exceptions\FieldValidationException
161161
* @throws Exceptions\DataSourceException
162162
*/
163+
#[\ReturnTypeWillChange]
163164
public function current()
164165
{
165166
if (count($this->castRows) > 0) {
@@ -213,6 +214,7 @@ public function __destruct()
213214
$this->dataSource->close();
214215
}
215216

217+
#[\ReturnTypeWillChange]
216218
public function rewind()
217219
{
218220
if (0 == $this->currentLine) {
@@ -223,18 +225,21 @@ public function rewind()
223225
}
224226
}
225227

228+
#[\ReturnTypeWillChange]
226229
public function key()
227230
{
228231
return $this->currentLine - count($this->castRows);
229232
}
230233

234+
#[\ReturnTypeWillChange]
231235
public function next()
232236
{
233237
if (0 == count($this->castRows)) {
234238
++$this->currentLine;
235239
}
236240
}
237241

242+
#[\ReturnTypeWillChange]
238243
public function valid()
239244
{
240245
return count($this->castRows) > 0 || !$this->dataSource->isEof();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace frictionlessdata\tableschema\Utility;
6+
7+
final class StrptimeFormatTransformer
8+
{
9+
public static function transform(string $strptimeFormat): string
10+
{
11+
return strtr(
12+
$strptimeFormat,
13+
[
14+
// Day
15+
'%d' => 'd', // 09
16+
'%e' => 'j', // 9
17+
18+
// Month
19+
'%m' => 'm', // 02
20+
'%b' => 'M', // Feb
21+
'%B' => 'F', // February
22+
23+
// Year
24+
'%Y' => 'Y', // 2023
25+
'%y' => 'y', // 23
26+
27+
// Hour
28+
'%H' => 'H', // 00 to 23
29+
'%k' => 'G', // 0 to 23
30+
'%I' => 'h', // 00 to 12
31+
'%l' => 'h', // 0 to 12
32+
'%p' => 'A', // AM / PM
33+
'%P' => 'a', // am / pm
34+
35+
// Minute
36+
'%M' => 'i',
37+
38+
// Second
39+
'%S' => 's',
40+
41+
// Date
42+
'%D' => 'm/d/y',
43+
'%F' => 'Y-m-d',
44+
45+
// Time
46+
'%r' => 'h:i:s A',
47+
'%R' => 'H:i',
48+
'%T' => 'H:i:s',
49+
'%s' => 'U',
50+
]
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)