refactoring

This commit is contained in:
Yuri Kuznetsov
2021-06-14 15:40:32 +03:00
parent 30316222f3
commit 1c1ddaf3d0
2 changed files with 25 additions and 1 deletions
+16 -1
View File
@@ -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);
}
+9
View File
@@ -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());
}
}