From 1c1ddaf3d07d64211d88e2ea5d304d8ccaca0096 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 14 Jun 2021 15:40:32 +0300 Subject: [PATCH] refactoring --- application/Espo/Core/Job/Data.php | 17 ++++++++++++++++- tests/unit/Espo/Core/Job/DataTest.php | 9 +++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/application/Espo/Core/Job/Data.php b/application/Espo/Core/Job/Data.php index 42c7c62db2..6472fffeae 100644 --- a/application/Espo/Core/Job/Data.php +++ b/application/Espo/Core/Job/Data.php @@ -31,6 +31,7 @@ namespace Espo\Core\Job; use Espo\Core\Utils\ObjectUtil; +use TypeError; use stdClass; class Data @@ -46,8 +47,22 @@ class Data $this->data = $data ?? (object) []; } - public static function create(?stdClass $data = null): self + /** + * Create an instance. + * + * @param stdClass|array|null $data Raw data. + * @return self + */ + public static function create($data = null): self { + if ($data !== null && !is_object($data) && !is_array($data)) { + throw new TypeError(); + } + + if (is_array($data)) { + $data = (object) $data; + } + return new self($data); } diff --git a/tests/unit/Espo/Core/Job/DataTest.php b/tests/unit/Espo/Core/Job/DataTest.php index 3f2be4aade..afbf299ade 100644 --- a/tests/unit/Espo/Core/Job/DataTest.php +++ b/tests/unit/Espo/Core/Job/DataTest.php @@ -44,4 +44,13 @@ class DataTest extends \PHPUnit\Framework\TestCase $this->assertEquals('target-id', $data->getTargetId()); $this->assertEquals('target-type', $data->getTargetType()); } + + public function testData2() + { + $data = Data + ::create(['test' => '1']); + + $this->assertEquals('1', $data->get('test')); + $this->assertEquals(null, $data->getTargetId()); + } }