Utils.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. use Swagger\Client\ObjectSerializer;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class Utils
  5. {
  6. /**
  7. * @param mixed $instance Instance of object.
  8. * @param mixed $data Data to be loaded.
  9. * @return mixed Return object with data.
  10. */
  11. public function loadData($instance, $data)
  12. {
  13. foreach ($instance::attributeMap() as $property => $name) {
  14. if (array_key_exists($name, $data) && !is_null($data[$name])) {
  15. if (in_array($instance::swaggerTypes()[$property], ObjectSerializer::primitiveDataType(), true)) {
  16. $propertySet = $instance::setters()[$property];
  17. $instance->$propertySet($data[$name]);
  18. } else {
  19. $obj = $instance::swaggerTypes()[$property];
  20. $instanceObj = new $obj();
  21. $propertySet = $instance::setters()[$property];
  22. $instance->$propertySet($this->loadData($instanceObj, $data[$name]));
  23. }
  24. }
  25. }
  26. return $instance;
  27. }
  28. /**
  29. * @param mixed $value Value to check
  30. * @param mixed $result Value to check
  31. * @param string $function Name of function
  32. * @return null|mixed Return object with data.
  33. */
  34. public function valueExists($value, $result, $function)
  35. {
  36. if (!is_null($result) && count($result) > 0) {
  37. // busco por external id
  38. foreach ($result as $item) {
  39. if ($item->$function() == $value) {
  40. return $item;
  41. }
  42. }
  43. }
  44. return null;
  45. }
  46. /**
  47. * Check value lower case.
  48. * @param mixed $value Value to check
  49. * @param mixed $result Value to check
  50. * @param string $function Name of function
  51. * @return null|mixed Return object with data.
  52. */
  53. public function valueExistsLower($value, $result, $function)
  54. {
  55. if (!is_null($result) && count($result) > 0) {
  56. // busco por external id
  57. foreach ($result as $item) {
  58. if (strtolower($item->$function()) == strtolower($value)) {
  59. return $item;
  60. }
  61. }
  62. }
  63. return null;
  64. }
  65. /**
  66. * Return JSON response with data or {message:messages}.
  67. * @param string $content contento to return
  68. * @param integer $result code of response
  69. * @return Response Return Response.
  70. */
  71. public function returnJSON($content, $codeResponse = 400) {
  72. json_decode($content,true);
  73. if(json_last_error() != JSON_ERROR_NONE)
  74. $content = json_encode(['message' => $content]);
  75. return new Response($content, $codeResponse, ['content-type' => 'application/json']);
  76. }
  77. }