Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. <?php
  2.  
  3. require __DIR__ . '/../vendor/autoload.php';
  4.  
  5.  
  6. use Doctrine\Common\Annotations\AnnotationRegistry;
  7. use JMS\Serializer\Annotation as Serializer;
  8. use JMS\Serializer\Annotation\Type;
  9. use JMS\Serializer\SerializerBuilder;
  10. use pcrov\JsonReader\JsonReader;
  11.  
  12. AnnotationRegistry::registerLoader('class_exists');
  13.  
  14. $json = <<<'JSON'
  15. {
  16. "type": "donut",
  17. "name": "Cake",
  18. "test": {
  19. "type": "donut",
  20. "name": "Cake"
  21. },
  22. "toppings": [
  23. { "id": 5002, "type": "Glazed" },
  24. { "id": 5006, "type": "Chocolate with Sprinkles" },
  25. { "id": 5004, "type": "Maple" }
  26. ]
  27. }
  28. JSON;
  29.  
  30. class Toppings {
  31. /**
  32. * @Type("int")
  33. */
  34. private $id;
  35. /**
  36. * @Type("string")
  37. */
  38. private $type;
  39.  
  40. /**
  41. * @param mixed $id
  42. */
  43. public function setId($id)
  44. {
  45. $this->id = $id;
  46. }
  47.  
  48. /**
  49. * @param mixed $type
  50. */
  51. public function setType($type)
  52. {
  53. $this->type = $type;
  54. }
  55. }
  56.  
  57. class Donut {
  58. /**
  59. * @Type("string")
  60. */
  61. private $type;
  62. /**
  63. * @Type("string")
  64. */
  65. private $name;
  66. /**
  67. * @Type("Toppings")
  68. */
  69. private $toppings;
  70.  
  71. /**
  72. * @Type("Donut")
  73. */
  74. private $test;
  75.  
  76. /**
  77. * @param mixed $test
  78. */
  79. public function setTest($test)
  80. {
  81. $this->test = $test;
  82. }
  83.  
  84. public function setType($type)
  85. {
  86. $this->type = $type;
  87. }
  88.  
  89. /**
  90. * @param mixed $name
  91. */
  92. public function setName($name)
  93. {
  94. $this->name = $name;
  95. }
  96.  
  97. /**
  98. * @param mixed $toppings
  99. */
  100. public function setToppings($toppings)
  101. {
  102. $this->toppings = $toppings;
  103. }
  104. }
  105.  
  106.  
  107. $reader = new JsonReader();
  108. $reader->json($json);
  109.  
  110. $map = [
  111. Donut::class => [
  112. 'properties' => [
  113. 'toppings' => Toppings::class,
  114. 'test' => Donut::class
  115. ]
  116. ]
  117. ];
  118.  
  119. $mapper = new ObjectMapper($map);
  120.  
  121. $start = microtime(true);
  122. $result = $mapper->map($json, Donut::class);
  123. var_dump((microtime(true) - $start) * 1000);
  124.  
  125. $start = microtime(true);
  126. $result = json_decode($json);
  127. var_dump((microtime(true) - $start) * 1000);
  128.  
  129. $serializer = SerializerBuilder::create()
  130. ->addMetadataDir(__DIR__)
  131. ->setCacheDir(__DIR__)
  132. ->build();
  133. $start = microtime(true);
  134. $object = $serializer->deserialize($json, Donut::class, 'json');
  135. var_dump((microtime(true) - $start) * 1000);
  136.  
  137. var_dump($result);
  138.  
  139.  
  140. class ObjectMapper {
  141. private $typeMap;
  142.  
  143. /**
  144. * ObjectMapper constructor.
  145. * @param $typeMap
  146. */
  147. public function __construct($typeMap)
  148. {
  149. $this->typeMap = $typeMap;
  150. }
  151.  
  152.  
  153. public function map($data, $type) {
  154. $reader = new JsonReader();
  155. $reader->json($data);
  156. return $this->read($reader, $type);
  157. }
  158.  
  159. private function read(JsonReader $reader, $type) {
  160. $result = null;
  161. while($reader->read()) {
  162. switch ($reader->type()) {
  163. case JsonReader::BOOL:
  164. case JsonReader::NUMBER:
  165. case JsonReader::STRING:
  166. return $reader->value();
  167. case JsonReader::OBJECT:
  168. $result = new $type;
  169. return $this->readProperties($reader, $type, $result);
  170. case JsonReader::END_OBJECT:
  171. return $result;
  172. case JsonReader::ARRAY:
  173. return $this->readItems($reader, $type);
  174. break;
  175. case JsonReader::END_ARRAY:
  176. return $result;
  177. }
  178. }
  179. }
  180.  
  181. private function readProperties(JsonReader $reader, $type, $result) {
  182. while ($reader->read()) {
  183. switch ($reader->type()) {
  184. case JsonReader::BOOL:
  185. case JsonReader::NUMBER:
  186. case JsonReader::STRING:
  187. $method = 'set' . ucfirst($reader->name());
  188. $result->$method($reader->value());
  189. break;
  190. case JsonReader::OBJECT:
  191. $name = $reader->name();
  192. if (isset($this->typeMap[$type]['properties'][$name])) {
  193. $propertyType = $this->typeMap[$type]['properties'][$name];
  194. }
  195. $method = 'set' . ucfirst($reader->name());
  196. $object = new $propertyType;
  197. $result->$method($this->readProperties($reader, $propertyType, $object));
  198. break;
  199. case JsonReader::END_OBJECT:
  200. return $result;
  201. case JsonReader::ARRAY:
  202. $name = $reader->name();
  203. if (isset($this->typeMap[$type]['properties'][$name])) {
  204. $type = $this->typeMap[$type]['properties'][$name];
  205. }
  206. $method = 'set' . ucfirst($reader->name());
  207. $items = $this->readItems($reader, $type);
  208. $result->$method($items);
  209. break;
  210. case JsonReader::END_ARRAY:
  211. break;
  212. }
  213. }
  214. }
  215.  
  216. private function readItems(JsonReader $reader, $type) {
  217. $result = [];
  218. while ($reader->read()) {
  219. switch ($reader->type()) {
  220. case JsonReader::BOOL:
  221. case JsonReader::NUMBER:
  222. case JsonReader::STRING:
  223. $result[] = $reader->value();
  224. break;
  225. case JsonReader::OBJECT:
  226. $object = new $type;
  227. $result[] = $this->readProperties($reader, $type, $object);
  228. break;
  229. case JsonReader::END_OBJECT:
  230. break;
  231. case JsonReader::ARRAY:
  232. $result[] = $this->read($reader, $type);
  233. break;
  234. case JsonReader::END_ARRAY:
  235. return $result;
  236. }
  237. }
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement