Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. {
  2. ...,
  3. "avatar": "http://example.com/my/image/path/image_name.png",
  4. ....
  5. }
  6.  
  7. {
  8. ...,
  9. "icon": "http://example.com/my/image/path/another_image_name.png",
  10. ...
  11. }
  12.  
  13. (For User instance)
  14. {
  15. ...,
  16. "name": "http://example.com/my/image/path/image_name.png",
  17. ...
  18. }
  19.  
  20. (For Application instance)
  21. {
  22. ...,
  23. "name": "http://example.com/my/image/path/another_image_name.png",
  24. ...
  25. }
  26.  
  27. //srcAppBundleEntityUser.php
  28. //...
  29. /**
  30. * @var integer
  31. *
  32. * @ORMOneToOne(targetEntity="AppBundleEntityImage")
  33. * @ORMJoinColumn(name="avatar", referencedColumnName="id")
  34. *
  35. * @JMSExpose()
  36. * @JMSType("Image")
  37. */
  38. private $avatar;
  39. //...
  40.  
  41. //srcAppBundleEntityApplication.php
  42. //...
  43. /**
  44. * @var integer
  45. *
  46. * @ORMOneToOne(targetEntity="AppBundleEntityImage")
  47. * @ORMJoinColumn(name="icon", referencedColumnName="id")
  48. *
  49. * @JMSExpose()
  50. * @JMSType("Image")
  51. */
  52. private $icon;
  53. //...
  54.  
  55. <?php
  56. //srcAppBundleSerializerImageTypeHandler.php
  57. namespace AppBundleSerializer;
  58.  
  59.  
  60. use AppBundleEntityImage;
  61. use JMSSerializerContext;
  62. use JMSSerializerGraphNavigator;
  63. use JMSSerializerHandlerSubscribingHandlerInterface;
  64. use JMSSerializerJsonSerializationVisitor;
  65. use SymfonyComponentHttpFoundationRequest;
  66.  
  67. class ImageTypeHandler implements SubscribingHandlerInterface
  68. {
  69. private $request;
  70.  
  71. public function __construct(Request $request) {
  72. $this->request = $request;
  73.  
  74. }
  75.  
  76. static public function getSubscribingMethods()
  77. {
  78. return [
  79. [
  80. 'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
  81. 'format' => 'json',
  82. 'type' => 'Image',
  83. 'method' => 'serializeImageToWebPath'
  84. ]
  85. ];
  86. }
  87.  
  88. public function serializeImageToWebPath(JsonSerializationVisitor $visitor, Image $image = null, array $type, Context $context)
  89. {
  90. $path = $image ? "http://" . $this->request->getHost() . "/uploads/images/" . $image->getPath() : '';
  91.  
  92. return $path;
  93. }
  94. }
  95.  
  96. app.image_type_handler:
  97. class: AppBundleSerializerImageTypeHandler
  98. arguments: ["@request"]
  99. scope: request
  100. tags:
  101. - { name: jms_serializer.subscribing_handler }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement