实体类型转换

Testing Is Documentation

tests/Database/Ddd/EntityConversionTest.php实体类型转换 - 图1

实体所有的属性设置和获取都会经过 settersetter 处理,每个实体都有通用的 settergetter,也支持自定义 settergetter

我们可以通过自定义 settersetter 方法实现属性类型转换。

Uses

  1. <?php
  2. use Leevel\Collection\Collection;
  3. use Leevel\Database\Ddd\Entity;
  4. use Tests\Database\DatabaseTestCase as TestCase;
  5. use Tests\Database\Ddd\Entity\DemoConversionEntity;

基本使用方法

fixture 定义

  1. # Tests\Database\Ddd\EntityConversionTest::makeEntity
  2. protected function makeEntity(): DemoConversionEntity
  3. {
  4. $entity = new DemoConversionEntity();
  5. $this->assertInstanceof(Entity::class, $entity);
  6. return $entity;
  7. }

Tests\Database\Ddd\Entity\DemoConversionEntity

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Collection\Collection;
  3. use Leevel\Database\Ddd\Entity;
  4. use Leevel\Database\Ddd\GetterSetterProp;
  5. use stdClass;
  6. class DemoConversionEntity extends Entity
  7. {
  8. use GetterSetterProp;
  9. const TABLE = 'test';
  10. const ID = 'id';
  11. const AUTO = 'id';
  12. const STRUCT = [
  13. 'id' => [
  14. self::READONLY => true,
  15. ],
  16. 'int1' => [],
  17. 'int2' => [],
  18. 'float1' => [],
  19. 'float2' => [],
  20. 'float3' => [],
  21. 'string1' => [],
  22. 'string2' => [],
  23. 'bool1' => [],
  24. 'bool2' => [],
  25. 'bool3' => [],
  26. 'bool4' => [],
  27. 'obj1' => [],
  28. 'obj2' => [],
  29. 'obj3' => [],
  30. 'arr1' => [],
  31. 'arr2' => [],
  32. 'json1' => [],
  33. 'json2' => [],
  34. 'coll1' => [],
  35. 'coll2' => [],
  36. 'invalid_setter' => [],
  37. ];
  38. private $_id;
  39. private $_int1;
  40. private $_int2;
  41. private $_int3;
  42. private $_float1;
  43. private $_float2;
  44. private $_float3;
  45. private $_string1;
  46. private $_string2;
  47. private $_bool1;
  48. private $_bool2;
  49. private $_bool3;
  50. private $_bool4;
  51. private $_obj1;
  52. private $_obj2;
  53. private $_obj3;
  54. private $_arr1;
  55. private $_arr2;
  56. private $_json1;
  57. private $_json2;
  58. private $_coll1;
  59. private $_coll2;
  60. private $_invalidSetter;
  61. public function setInt1($value): Entity
  62. {
  63. $this->_int1 = (int) $value;
  64. return $this;
  65. }
  66. public function getInt1(): int
  67. {
  68. return $this->_int1 + 1;
  69. }
  70. public function setInt2(int $value): Entity
  71. {
  72. $this->_int2 = $value;
  73. return $this;
  74. }
  75. public function getInt2(): int
  76. {
  77. return $this->_int2;
  78. }
  79. public function setFloat1($value): Entity
  80. {
  81. $this->_float1 = (float) $value;
  82. return $this;
  83. }
  84. public function getFloat1(): float
  85. {
  86. return $this->_float1 + 1;
  87. }
  88. public function setFloat2(float $value): Entity
  89. {
  90. $this->_float2 = $value;
  91. return $this;
  92. }
  93. public function getFloat2(): float
  94. {
  95. return $this->_float2;
  96. }
  97. public function setFloat3(float $value): Entity
  98. {
  99. $this->_float3 = $value;
  100. return $this;
  101. }
  102. public function getFloat3(): float
  103. {
  104. return $this->_float3;
  105. }
  106. public function setString1($value): Entity
  107. {
  108. $this->_string1 = (string) $value;
  109. return $this;
  110. }
  111. public function getString1(): string
  112. {
  113. return $this->_string1;
  114. }
  115. public function setString2(string $value): Entity
  116. {
  117. $this->_string2 = $value;
  118. return $this;
  119. }
  120. public function getString2(): string
  121. {
  122. return $this->_string2;
  123. }
  124. public function setBool1($value): Entity
  125. {
  126. $this->_bool1 = (bool) $value;
  127. return $this;
  128. }
  129. public function getBool1(): bool
  130. {
  131. return $this->_bool1;
  132. }
  133. public function setBool2($value): Entity
  134. {
  135. $this->_bool2 = (bool) $value;
  136. return $this;
  137. }
  138. public function getBool2(): bool
  139. {
  140. return $this->_bool2;
  141. }
  142. public function setBool3(bool $value): Entity
  143. {
  144. $this->_bool3 = $value;
  145. return $this;
  146. }
  147. public function getBool3(): bool
  148. {
  149. return $this->_bool3;
  150. }
  151. public function setBool4(bool $value): Entity
  152. {
  153. $this->_bool4 = $value;
  154. return $this;
  155. }
  156. public function getBool4(): bool
  157. {
  158. return $this->_bool4;
  159. }
  160. public function setObj1($value): Entity
  161. {
  162. $this->_obj1 = json_encode($value, JSON_FORCE_OBJECT);
  163. return $this;
  164. }
  165. public function getObj1(): stdClass
  166. {
  167. return json_decode($this->_obj1);
  168. }
  169. public function setObj2(string $value): Entity
  170. {
  171. $value = json_decode($value, true);
  172. $this->_obj2 = json_encode($value, JSON_FORCE_OBJECT);
  173. return $this;
  174. }
  175. public function getObj2(): stdClass
  176. {
  177. return json_decode($this->_obj2);
  178. }
  179. public function setObj3(stdClass $value): Entity
  180. {
  181. $this->_obj3 = json_encode($value);
  182. return $this;
  183. }
  184. public function getObj3(): stdClass
  185. {
  186. return json_decode($this->_obj3);
  187. }
  188. public function setArr1(array $value): Entity
  189. {
  190. $this->_arr1 = json_encode($value);
  191. return $this;
  192. }
  193. public function getArr1(): array
  194. {
  195. return json_decode($this->_arr1, true);
  196. }
  197. public function setArr2(string $value): Entity
  198. {
  199. $this->_arr2 = $value;
  200. return $this;
  201. }
  202. public function getArr2(): array
  203. {
  204. return json_decode($this->_arr2, true);
  205. }
  206. public function setJson1(array $value): Entity
  207. {
  208. $this->_json1 = json_encode($value);
  209. return $this;
  210. }
  211. public function getJson1(): array
  212. {
  213. return json_decode($this->_json1, true);
  214. }
  215. public function setJson2(string $value): Entity
  216. {
  217. $this->_json2 = $value;
  218. return $this;
  219. }
  220. public function getJson2(): array
  221. {
  222. return json_decode($this->_json2, true);
  223. }
  224. public function setColl1(string $value): Entity
  225. {
  226. $this->_coll1 = $value;
  227. return $this;
  228. }
  229. public function getColl1(): Collection
  230. {
  231. return new Collection(json_decode($this->_coll1, true));
  232. }
  233. public function setColl2(array $value): Entity
  234. {
  235. $this->_coll2 = json_encode($value);
  236. return $this;
  237. }
  238. public function getColl2(): Collection
  239. {
  240. return new Collection(json_decode($this->_coll2, true));
  241. }
  242. public function setInvalidSetter($value)
  243. {
  244. $this->_invalidSetter = $value;
  245. }
  246. }
  1. public function testBaseUse($field, $source, $prop, $conversion): void
  2. {
  3. $entity = $this->makeEntity();
  4. $entity->withProp($field, $source);
  5. $assertMethod = in_array($field, [
  6. 'obj1', 'obj2',
  7. 'obj3', 'obj4',
  8. 'coll1', 'coll2',
  9. 'collection1', 'collection2',
  10. ], true) ? 'assertEquals' : 'assertSame';
  11. $this->assertSame($prop, $this->getTestProperty($entity, '_'.$field));
  12. $this->{$assertMethod}($conversion, $entity->prop($field));
  13. }