vendor/xeroapi/xero-php-oauth2/lib/AccountingObjectSerializer.php line 255

Open in your IDE?
  1. <?php
  2. /**
  3.  * ObjectSerializer
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * @category Class
  8.  * @package  XeroAPI\XeroPHP
  9.  * @author   OpenAPI Generator team
  10.  * @link     https://openapi-generator.tech
  11.  */
  12. /**
  13.  * Xero Accounting API
  14.  *
  15.  * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
  16.  *
  17.  * Contact: [email protected]
  18.  * Generated by: https://openapi-generator.tech
  19.  * OpenAPI Generator version: 5.4.0
  20.  */
  21. /**
  22.  * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
  23.  * https://openapi-generator.tech
  24.  * Do not edit the class manually.
  25.  */
  26. namespace XeroAPI\XeroPHP;
  27. use XeroAPI\XeroPHP\Models\Accounting\ModelInterface;
  28. /**
  29.  * ObjectSerializer Class Doc Comment
  30.  *
  31.  * @category Class
  32.  * @package  XeroAPI\XeroPHP
  33.  * @author   OpenAPI Generator team
  34.  * @link     https://openapi-generator.tech
  35.  */
  36. class AccountingObjectSerializer
  37. {
  38.     /**
  39.      * Validate if date
  40.      *
  41.      * @param  $value the data to validate as a date
  42.      * @return boolean true/false 
  43.      */
  44.     public static function checkThisDate($value
  45.     {
  46.         if (!$value) {
  47.             return false;
  48.         }
  49.         try {
  50.             new \DateTime($value);
  51.             return true;
  52.         } catch (\Exception $e) {
  53.             return false;
  54.         }
  55.     }
  56.     /**
  57.      * Serialize data
  58.      *
  59.      * @param mixed  $data   the data to serialize
  60.      * @param string $type   the OpenAPIToolsType of the data
  61.      * @param string $format the format of the OpenAPITools type of the data
  62.      *
  63.      * @return string|object serialized form of $data
  64.      */
  65.     public static function sanitizeForSerialization($data$type null$format null)
  66.     {
  67.         if (is_scalar($data) || null === $data) {
  68.             return $data;
  69.         } elseif ($data instanceof \DateTime) {
  70.             return ($format === 'date') ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
  71.         } elseif (is_array($data)) {
  72.             foreach ($data as $property => $value) {
  73.                 $data[$property] = self::sanitizeForSerialization($value);
  74.             }
  75.             return $data;
  76.         } elseif (is_object($data)) {
  77.             $values = [];
  78.             if ($data instanceof ModelInterface) {
  79.                 $formats $data::openAPIFormats();
  80.                 foreach ($data::openAPITypes() as $property => $openAPIType) {
  81.                     $getter $data::getters()[$property];
  82.                     $value $data->$getter();
  83.                     if ($value !== null
  84.                         && !in_array($openAPIType, ['\DateTime''\SplFileObject''array''bool''boolean''byte''double''float''int''integer''mixed''number''object''string''void'], true)
  85.                         && method_exists($openAPIType'getAllowableEnumValues')
  86.                         && !in_array($value$openAPIType::getAllowableEnumValues(), true)) {
  87.                         $imploded implode("', '"$openAPIType::getAllowableEnumValues());
  88.                         throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
  89.                     }
  90.                     if ($value !== null) {
  91.                         $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value$openAPIType$formats[$property]);
  92.                     }
  93.                 }
  94.             } else {
  95.                 foreach($data as $property => $value) {
  96.                     $values[$property] = self::sanitizeForSerialization($value);
  97.                 }
  98.             }
  99.             return (object)$values;
  100.         } else {
  101.             return (string)$data;
  102.         }
  103.     }
  104.     /**
  105.      * Sanitize filename by removing path.
  106.      * e.g. ../../sun.gif becomes sun.gif
  107.      *
  108.      * @param string $filename filename to be sanitized
  109.      *
  110.      * @return string the sanitized filename
  111.      */
  112.     public static function sanitizeFilename($filename)
  113.     {
  114.         if (preg_match("/.*[\/\\\\](.*)$/"$filename$match)) {
  115.             return $match[1];
  116.         } else {
  117.             return $filename;
  118.         }
  119.     }
  120.     /**
  121.      * Take value and turn it into a string suitable for inclusion in
  122.      * the path, by url-encoding.
  123.      *
  124.      * @param string $value a string which will be part of the path
  125.      *
  126.      * @return string the serialized object
  127.      */
  128.     public static function toPathValue($value)
  129.     {
  130.         return rawurlencode(self::toString($value));
  131.     }
  132.     /**
  133.      * Take value and turn it into a string suitable for inclusion in
  134.      * the query, by imploding comma-separated if it's an object.
  135.      * If it's a string, pass through unchanged. It will be url-encoded
  136.      * later.
  137.      *
  138.      * @param string[]|string|\DateTime $object an object to be serialized to a string
  139.      *
  140.      * @return string the serialized object
  141.      */
  142.     public static function toQueryValue($object)
  143.     {
  144.         if (is_array($object)) {
  145.             return implode(','$object);
  146.         } else {
  147.             return self::toString($object);
  148.         }
  149.     }
  150.     /**
  151.      * Take value and turn it into a string suitable for inclusion in
  152.      * the header. If it's a string, pass through unchanged
  153.      * If it's a datetime object, format it in ISO8601
  154.      *
  155.      * @param string $value a string which will be part of the header
  156.      *
  157.      * @return string the header string
  158.      */
  159.     public static function toHeaderValue($value)
  160.     {
  161.         return self::toString($value);
  162.     }
  163.     /**
  164.      * Take value and turn it into a string suitable for inclusion in
  165.      * the http body (form parameter). If it's a string, pass through unchanged
  166.      * If it's a datetime object, format it in ISO8601
  167.      *
  168.      * @param string|\SplFileObject $value the value of the form parameter
  169.      *
  170.      * @return string the form string
  171.      */
  172.     public static function toFormValue($value)
  173.     {
  174.         if ($value instanceof \SplFileObject) {
  175.             return $value->getRealPath();
  176.         } else {
  177.             return self::toString($value);
  178.         }
  179.     }
  180.     /**
  181.      * Take value and turn it into a string suitable for inclusion in
  182.      * the parameter. If it's a string, pass through unchanged
  183.      * If it's a datetime object, format it in ISO8601
  184.      *
  185.      * @param string|\DateTime $value the value of the parameter
  186.      *
  187.      * @return string the header string
  188.      */
  189.     public static function toString($value)
  190.     {
  191.         if ($value instanceof \DateTime) { // datetime in ISO8601 format
  192.             return $value->format(\DateTime::ATOM);
  193.         } else {
  194.             return $value;
  195.         }
  196.     }
  197.     /**
  198.      * Serialize an array to a string.
  199.      *
  200.      * @param array  $collection                 collection to serialize to a string
  201.      * @param string $collectionFormat           the format use for serialization (csv,
  202.      * ssv, tsv, pipes, multi)
  203.      * @param bool   $allowCollectionFormatMulti allow collection format to be a multidimensional array
  204.      *
  205.      * @return string
  206.      */
  207.     public static function serializeCollection(array $collection$collectionFormat$allowCollectionFormatMulti false)
  208.     {
  209.         if ($allowCollectionFormatMulti && ('multi' === $collectionFormat)) {
  210.             // http_build_query() almost does the job for us. We just
  211.             // need to fix the result of multidimensional arrays.
  212.             return preg_replace('/%5B[0-9]+%5D=/''='http_build_query($collection'''&'));
  213.         }
  214.         switch ($collectionFormat) {
  215.             case 'pipes':
  216.                 return implode('|'$collection);
  217.             case 'tsv':
  218.                 return implode("\t"$collection);
  219.             case 'ssv':
  220.                 return implode(' '$collection);
  221.             case 'csv':
  222.                 // Deliberate fall through. CSV is default format.
  223.             default:
  224.                 return implode(','$collection);
  225.         }
  226.     }
  227.     /**
  228.      * Deserialize a JSON string into an object
  229.      *
  230.      * @param mixed    $data          object or primitive to be deserialized
  231.      * @param string   $class         class name is passed as a string
  232.      * @param string[] $httpHeaders   HTTP headers
  233.      * @param string   $discriminator discriminator if polymorphism is used
  234.      *
  235.      * @return object|array|null a single or an array of $class instances
  236.      */
  237.     public static function deserialize($data$class$httpHeaders null)
  238.     {
  239.         if (null === $data) {
  240.             return null;
  241.         } elseif (substr($class04) === 'map[') { // for associative array e.g. map[string,int]
  242.             $data is_string($data) ? json_decode($data) : $data;
  243.             settype($data'array');
  244.             $inner substr($class4, -1);
  245.             $deserialized = [];
  246.             if (strrpos($inner",") !== false) {
  247.                 $subClass_array explode(','$inner2);
  248.                 $subClass $subClass_array[1];
  249.                 foreach ($data as $key => $value) {
  250.                     $deserialized[$key] = self::deserialize($value$subClassnull);
  251.                 }
  252.             }
  253.             return $deserialized;
  254.         } elseif (strcasecmp(substr($class, -2), '[]') === 0) {
  255.             $data is_string($data) ? json_decode($data) : $data;
  256.             $subClass substr($class0, -2);
  257.             $values = [];
  258.             foreach ($data as $key => $value) {
  259.                 $values[] = self::deserialize($value$subClassnull);
  260.             }
  261.             return $values;
  262.         } elseif ($class === 'object') {
  263.             settype($data'array');
  264.             return $data;
  265.         } elseif ($class === '\DateTime') {
  266.             // Some API's return an invalid, empty string as a
  267.             // date-time property. DateTime::__construct() will return
  268.             // the current time for empty input which is probably not
  269.             // what is meant. The invalid empty string is probably to
  270.             // be interpreted as a missing field/value. Let's handle
  271.             // this graceful.
  272.             if (!empty($data)) {
  273.                 
  274.                 if( self::checkThisDate($data) ) {
  275.                     return new \DateTime($data);
  276.                 } else {
  277.                     // Data not in a format that simply converts to a new DateTime();
  278.                     // Custom Date Deserializer to allow for Xero's use of .NET JSON Date format
  279.                     $match preg_match'/([\d]{13})/'$data$date );
  280.                     $timestamp $date[1]/1000;
  281.                     
  282.                     $datetime = new \DateTime();
  283.                     $datetime->setTimestamp($timestamp);
  284.             
  285.                     $result $datetime->format('Y-m-d H:i:s');
  286.                         
  287.                     $date = new \DateTime($result);
  288.                     return $date;
  289.                 }
  290.             } else {
  291.                 return null;
  292.             }
  293.         } elseif ($class === '\SplFileObject') {
  294.             /** @var \Psr\Http\Message\StreamInterface $data */
  295.             // determine file name
  296.             if (array_key_exists('Content-Disposition'$httpHeaders) &&
  297.                 preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i'$httpHeaders['Content-Disposition'], $match)) {
  298.                 $filename Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR self::sanitizeFilename($match[1]);
  299.             } else {
  300.                 $filename tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
  301.             }
  302.             $file fopen($filename'w');
  303.             while ($chunk $data->read(200)) {
  304.                 fwrite($file$chunk);
  305.             }
  306.             fclose($file);
  307.             return new \SplFileObject($filename'r');
  308.         } elseif (in_array($class, ['\DateTime''\SplFileObject''array''bool''boolean''byte''double''float''int''integer''mixed''number''object''string''void'], true)) {
  309.             settype($data$class);
  310.             return $data;
  311.         } elseif (method_exists($class'getAllowableEnumValues')) {
  312.             if (!in_array($data$class::getAllowableEnumValues(), true)) {
  313.                 $imploded implode("', '"$class::getAllowableEnumValues());
  314.                 throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
  315.             }
  316.             return $data;
  317.         } else {
  318.             $data is_string($data) ? json_decode($data) : $data;
  319.             // If a discriminator is defined and points to a valid subclass, use it.
  320.             $discriminator $class::DISCRIMINATOR;
  321.             if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) {
  322.                 $subclass '\XeroAPI\XeroPHP\Model\\' $data->{$discriminator};
  323.                 if (is_subclass_of($subclass$class)) {
  324.                     $class $subclass;
  325.                 }
  326.             }
  327.             $instance = new $class();
  328.             foreach ($instance::openAPITypes() as $property => $type) {
  329.                 $propertySetter $instance::setters()[$property];
  330.                 if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) {
  331.                     continue;
  332.                 }
  333.                 $propertyValue $data->{$instance::attributeMap()[$property]};
  334.                 if (isset($propertyValue)) {
  335.                     $instance->$propertySetter(self::deserialize($propertyValue$typenull));
  336.                 }
  337.             }
  338.             return $instance;
  339.         }
  340.     }
  341. }