Skip to content

Commit

Permalink
Merge pull request #3 from dealnews/next
Browse files Browse the repository at this point in the history
Add support for dates which are stored as numbers
  • Loading branch information
brianlmoon authored Feb 27, 2024
2 parents 251ac68 + 257262d commit 4685d47
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Testing DealNews\DataMapper

on: [push]

jobs:
test:

runs-on: ubuntu-latest

strategy:
matrix:
php-versions: ['8.0', '8.1', '8.2']
include:
- operating-system: 'ubuntu-latest'
php-versions: '8.0'
phpunit-versions: 9
steps:

- name: Checkout
uses: actions/checkout@v3

- name: Composer Install
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php-versions }}

- name: PHPUnit tests
uses: php-actions/phpunit@v3
with:
php_extensions: "pcov yaml"
version: "9.6"
php_version: ${{ matrix.php-versions }}

- name: Run Phan
uses: k1LoW/phan-action@v0
10 changes: 9 additions & 1 deletion src/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ protected function setValue(object $object, string $property, array $data, array
is_subclass_of($mapping['class'], "\DateTime", true) ||
is_subclass_of($mapping['class'], "\DateTimeImmutable", true)
) {

// Date values that are integers or floats in the database
// need to be converted to strings for the parent mapper
// base class
if ($value !== null && is_numeric($value)) {
$value = date($mapping['format'] ?? 'Y-m-d H:i:s', $value);
}

$timezone = $mapping['timezone'] ?? null;
$format = $mapping['format'] ?? 'Y-m-d H:i:s';
$value = $mapping['class']::createFromFormat($format, $value, $timezone);
Expand Down Expand Up @@ -229,7 +237,7 @@ protected function getValue(object $object, string $property, array $mapping) {
$value = $object->$property->format($format);
} else {

if (!empty($mapping['one_to_many'])) {
if (!empty($mapping['one_to_many']) && is_iterable($value)) {
$new_value = [];
foreach ($value as $v) {
$new_value[] = (array)$v;
Expand Down
10 changes: 10 additions & 0 deletions tests/AbstractMapper/SetGetValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ public function encodingSetData() {
],
new \DateTimeImmutable('2020-01-01 12:00:00'),
],
'DateTime as Int' => [
'dt',
[
'dt' => strtotime('2020-01-01 12:00:00'),
],
[
'class' => "\DateTime",
],
new \DateTime('2020-01-01 12:00:00'),
],
];
}

Expand Down

0 comments on commit 4685d47

Please sign in to comment.