Skip to content

Commit

Permalink
Merge pull request #4 from dealnews/next
Browse files Browse the repository at this point in the history
Support saving of DateTime as int as well.
  • Loading branch information
brianlmoon authored Feb 27, 2024
2 parents 4685d47 + 9238efa commit bf2db0f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.DS_Store
*~
*.sw[aop]
.php-cs-fixer.cache

# Deployment dependencies
vendor
Expand Down
43 changes: 28 additions & 15 deletions src/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace DealNews\DataMapper;

use \DealNews\DataMapper\Interfaces\Mapper;
use \DealNews\Constraints\Constraint;
use \DealNews\Constraints\ConstraintException as UpstreamConstraintException;
use \DealNews\DataMapper\Interfaces\Mapper;

/**
* Abstract Mapper class providing basic reusable functions
Expand Down Expand Up @@ -123,8 +123,8 @@ protected function setValue(object $object, string $property, array $data, array
if (!empty($mapping['encoding'])) {
switch ($mapping['encoding']) {
case 'json':
$assoc = $mapping['json_assoc'] ?? null;
$depth = $mapping['json_depth'] ?? 512;
$assoc = $mapping['json_assoc'] ?? null;
$depth = $mapping['json_depth'] ?? 512;
$options = $mapping['json_options'] ?? 0;
$value = json_decode($value, $assoc, $depth, $options);
break;
Expand All @@ -142,10 +142,10 @@ protected function setValue(object $object, string $property, array $data, array

if (!empty($mapping['class'])) {
if (
is_a($mapping['class'], "\DateTime", true) ||
is_a($mapping['class'], "\DateTimeImmutable", true) ||
is_subclass_of($mapping['class'], "\DateTime", true) ||
is_subclass_of($mapping['class'], "\DateTimeImmutable", true)
is_a($mapping['class'], '\\DateTime', true) ||
is_a($mapping['class'], '\\DateTimeImmutable', true) ||
is_subclass_of($mapping['class'], '\\DateTime', true) ||
is_subclass_of($mapping['class'], '\\DateTimeImmutable', true)
) {

// Date values that are integers or floats in the database
Expand All @@ -156,7 +156,7 @@ protected function setValue(object $object, string $property, array $data, array
}

$timezone = $mapping['timezone'] ?? null;
$format = $mapping['format'] ?? 'Y-m-d H:i:s';
$format = $mapping['format'] ?? 'Y-m-d H:i:s';
$value = $mapping['class']::createFromFormat($format, $value, $timezone);
} else {

Expand Down Expand Up @@ -185,7 +185,7 @@ protected function setValue(object $object, string $property, array $data, array
}

if (
is_array($value) &&
is_array($value) &&
is_object($object->$property) &&
$object->$property instanceof \ArrayObject
) {
Expand All @@ -210,6 +210,10 @@ protected function setValue(object $object, string $property, array $data, array
protected function getValue(object $object, string $property, array $mapping) {
$value = $object->$property;

if ($value instanceof \ArrayObject) {
$value = $value->getArrayCopy();
}

if (!empty($mapping['encoding'])) {
switch ($mapping['encoding']) {
case 'json':
Expand All @@ -228,13 +232,22 @@ protected function getValue(object $object, string $property, array $mapping) {

if (!empty($mapping['class'])) {
if (
is_a($mapping['class'], "\DateTime", true) ||
is_a($mapping['class'], "\DateTimeImmutable", true) ||
is_subclass_of($mapping['class'], "\DateTime", true) ||
is_subclass_of($mapping['class'], "\DateTimeImmutable", true)
is_a($mapping['class'], '\\DateTime', true) ||
is_a($mapping['class'], '\\DateTimeImmutable', true) ||
is_subclass_of($mapping['class'], '\\DateTime', true) ||
is_subclass_of($mapping['class'], '\\DateTimeImmutable', true)
) {
$format = $mapping['format'] ?? 'Y-m-d H:i:s';
$value = $object->$property->format($format);

// Date values that are integers or floats in the database
// need to be converted to strings for the parent mapper
// base class
if (isset($mapping['type']) && $mapping['type'] == 'int') {
$value = $object->$property->getTimestamp();
} else {
$format = $mapping['format'] ?? 'Y-m-d H:i:s';
$value = $object->$property->format($format);
}

} else {

if (!empty($mapping['one_to_many']) && is_iterable($value)) {
Expand Down
43 changes: 34 additions & 9 deletions tests/AbstractMapper/SetGetValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class SetGetValueTest extends \PHPUnit\Framework\TestCase {
public function testSetInvalidEncoding() {
$this->expectException("\LogicException");
$this->expectException('\\LogicException');
$obj = new SetGetValueMock();
$mapper = new SetGetValueMapperMock();
$mapper->setValue($obj, 'foo', ['foo' => true], ['encoding' => 'bad']);
}

public function testGetInvalidEncoding() {
$this->expectException("\LogicException");
$this->expectException('\\LogicException');
$obj = new SetGetValueMock();
$obj->foo = 1;
$mapper = new SetGetValueMapperMock();
Expand Down Expand Up @@ -119,7 +119,7 @@ public function encodingSetData() {
'YAML' => [
'yaml',
[
'yaml' => yaml_emit(['foo'=>'bar']),
'yaml' => yaml_emit(['foo' => 'bar']),
],
[
'encoding' => 'yaml',
Expand All @@ -144,7 +144,7 @@ public function encodingSetData() {
'dt' => '2020-01-01 12:00:00',
],
[
'class' => "\DateTime",
'class' => '\\DateTime',
],
new \DateTime('2020-01-01 12:00:00'),
],
Expand All @@ -154,7 +154,7 @@ public function encodingSetData() {
'dti' => '2020-01-01 12:00:00',
],
[
'class' => "\DateTimeImmutable",
'class' => '\\DateTimeImmutable',
],
new \DateTimeImmutable('2020-01-01 12:00:00'),
],
Expand All @@ -164,10 +164,18 @@ public function encodingSetData() {
'dt' => strtotime('2020-01-01 12:00:00'),
],
[
'class' => "\DateTime",
'class' => '\\DateTime',
],
new \DateTime('2020-01-01 12:00:00'),
],
'ArrayObject' => [
'arr',
[
'arr' => [1, 2, 3],
],
[],
new \ArrayObject([1, 2, 3]),
],
];
}

Expand Down Expand Up @@ -241,7 +249,7 @@ public function encodingGetData() {
[
'encoding' => 'yaml',
],
yaml_emit(['foo'=>'bar']),
yaml_emit(['foo' => 'bar']),
],
'Serialize' => [
'class',
Expand All @@ -255,18 +263,33 @@ public function encodingGetData() {
'dt',
new \DateTime('2020-01-01 12:00:00'),
[
'class' => "\DateTime",
'class' => '\\DateTime',
],
'2020-01-01 12:00:00',
],
'DateTimeImmutable' => [
'dti',
new \DateTimeImmutable('2020-01-01 12:00:00'),
[
'class' => "\DateTimeImmutable",
'class' => '\\DateTimeImmutable',
],
'2020-01-01 12:00:00',
],
'DateTime as Int' => [
'dt',
new \DateTime('2020-01-01 12:00:00'),
[
'class' => '\\DateTime',
'type' => 'int',
],
strtotime('2020-01-01 12:00:00'),
],
'ArrayObject' => [
'arr',
new \ArrayObject([1, 2, 3]),
[],
[1, 2, 3],
],
];
}
}
Expand All @@ -282,12 +305,14 @@ class SetGetValueMock {
public \stdClass $class;
public \DateTime $dt;
public \DateTimeImmutable $dti;
public \ArrayObject $arr;

public function __construct() {
$this->json_object = new \stdClass();
$this->class = new \stdClass();
$this->dt = new \DateTime();
$this->dti = new \DateTimeImmutable();
$this->arr = new \ArrayObject();
}
}

Expand Down
22 changes: 11 additions & 11 deletions tests/AbstractMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace DealNews\DataMapper\Tests;

use \DealNews\DataMapper\Tests\TestClasses\Course;
use \DealNews\DataMapper\Tests\TestClasses\Mapper\CourseMapper;
use \DealNews\DataMapper\Tests\TestClasses\Student;
use \DealNews\DataMapper\Tests\TestClasses\Teacher;
use \DealNews\DataMapper\Tests\TestClasses\Mapper\CourseMapper;

class AbstractMapperTest extends \PHPUnit\Framework\TestCase {

Expand All @@ -26,7 +26,7 @@ public function testSetData() {
'student_id' => 2,
'name' => 'Student 2',
'create_datetime' => '2020-01-02',
]
],
],
'create_date' => '2020-01-01',
]);
Expand All @@ -43,14 +43,14 @@ public function testSetData() {
}

public function testGetData() {
$student = new Student();
$student->student_id = 2;
$student->name = 'Student 2';
$student = new Student();
$student->student_id = 2;
$student->name = 'Student 2';
$student->create_datetime = '2020-01-02';

$teacher = new Teacher();
$teacher->teacher_id = 1;
$teacher->name = 'Teacher 1';
$teacher = new Teacher();
$teacher->teacher_id = 1;
$teacher->name = 'Teacher 1';
$teacher->create_datetime = '2020-01-03';

$course = new Course();
Expand All @@ -72,7 +72,7 @@ public function testGetData() {
'student_id' => 2,
'name' => 'Student 2',
'create_datetime' => '2020-01-02',
]
],
],
'teacher' => [
'teacher_id' => 1,
Expand Down Expand Up @@ -106,15 +106,15 @@ public function constraintData() {
[
'name' => '',
],
'/Expected: minimum length of 1\.$/',
'/Expected: minimum length of 1\\.$/',
'name',
],

'Long Name' => [
[
'name' => str_repeat('x', 101),
],
'/Expected: maximum length of 100\.$/',
'/Expected: maximum length of 100\\.$/',
'name',
],

Expand Down
8 changes: 4 additions & 4 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,28 @@ public function testGetMapper() {
}

public function testBadNew() {
$this->expectException('\LogicException');
$this->expectException('\\LogicException');
$this->expectExceptionCode('1');
$repo = $this->getRepo();
$obj = $repo->new('Foo');
}

public function testBadDelete() {
$this->expectException('\LogicException');
$this->expectException('\\LogicException');
$this->expectExceptionCode('1');
$repo = $this->getRepo();
$obj = $repo->delete('Foo', 1);
}

public function testBadFind() {
$this->expectException('\LogicException');
$this->expectException('\\LogicException');
$this->expectExceptionCode('1');
$repo = $this->getRepo();
$obj = $repo->find('Foo', []);
}

public function testBadClass() {
$this->expectException('\LogicException');
$this->expectException('\\LogicException');
$this->expectExceptionCode('4');
$repo = $this->getRepo();
$obj = $repo->addMapper('bad', new BadMapper());
Expand Down
4 changes: 2 additions & 2 deletions tests/TestClasses/Mapper/CourseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @package DataMapper
*/
class CourseMapper extends \DealNews\DataMapper\AbstractMapper {
public const MAPPED_CLASS = "\DealNews\DataMapper\Tests\TestClasses\Course";
public const MAPPED_CLASS = '\\DealNews\\DataMapper\\Tests\\TestClasses\\Course';

public const PRIMARY_KEY = 'course_id';

Expand All @@ -22,7 +22,7 @@ class CourseMapper extends \DealNews\DataMapper\AbstractMapper {
'max' => 100,
],
],
'active' => [],
'active' => [],
'teacher' => [
'class' => \DealNews\DataMapper\Tests\TestClasses\Teacher::class,
'one_to_many' => false,
Expand Down

0 comments on commit bf2db0f

Please sign in to comment.