12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- use Swagger\Client\ObjectSerializer;
- use Symfony\Component\HttpFoundation\Response;
- class Utils
- {
- /**
- * @param mixed $instance Instance of object.
- * @param mixed $data Data to be loaded.
- * @return mixed Return object with data.
- */
- public function loadData($instance, $data)
- {
- foreach ($instance::attributeMap() as $property => $name) {
- if (array_key_exists($name, $data) && !is_null($data[$name])) {
- if (in_array($instance::swaggerTypes()[$property], ObjectSerializer::primitiveDataType(), true)) {
- $propertySet = $instance::setters()[$property];
- $instance->$propertySet($data[$name]);
- } else {
- $obj = $instance::swaggerTypes()[$property];
- $instanceObj = new $obj();
- $propertySet = $instance::setters()[$property];
- $instance->$propertySet($this->loadData($instanceObj, $data[$name]));
- }
- }
- }
- return $instance;
- }
- /**
- * @param mixed $value Value to check
- * @param mixed $result Value to check
- * @param string $function Name of function
- * @return null|mixed Return object with data.
- */
- public function valueExists($value, $result, $function)
- {
- if (!is_null($result) && count($result) > 0) {
- // busco por external id
- foreach ($result as $item) {
- if ($item->$function() == $value) {
- return $item;
- }
- }
- }
- return null;
- }
- /**
- * Check value lower case.
- * @param mixed $value Value to check
- * @param mixed $result Value to check
- * @param string $function Name of function
- * @return null|mixed Return object with data.
- */
- public function valueExistsLower($value, $result, $function)
- {
- if (!is_null($result) && count($result) > 0) {
- // busco por external id
- foreach ($result as $item) {
- if (strtolower($item->$function()) == strtolower($value)) {
- return $item;
- }
- }
- }
- return null;
- }
- /**
- * Return JSON response with data or {message:messages}.
- * @param string $content contento to return
- * @param integer $result code of response
- * @return Response Return Response.
- */
- public function returnJSON($content, $codeResponse = 400) {
- json_decode($content,true);
- if(json_last_error() != JSON_ERROR_NONE)
- $content = json_encode(['message' => $content]);
-
- return new Response($content, $codeResponse, ['content-type' => 'application/json']);
- }
- }
|