<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses('webservices.rest.Payload');

  /**
   * Deserializer abstract base class
   *
   */
  abstract class RestDeserializer extends Object {

    /**
     * Calculate variants of a given name
     *
     * @param   string name
     * @return  string[] names
     */
    protected function variantsOf($name) {
      $variants= array($name);
      $chunks= explode('_', $name);
      if (sizeof($chunks) > 1) {      // product_id => productId
        $variants[]= array_shift($chunks).implode(array_map('ucfirst', $chunks));
      }
      return $variants;
    }

    /**
     * Returns the first element of a given traversable data structure
     * or the data structure itself, or NULL if the structure has more
     * than one element.
     *
     * @param  var struct
     * @param  var[]
     */
    protected function keyOf($struct) {
      if (is_array($struct) || $struct instanceof Traversable) {
        $return= NULL;
        foreach ($struct as $element) {
          if (NULL === $return) {
            $return= array($element);
            continue;
          }
          return NULL;    // Found a second element, return NULL
        }
        return $return;   // Will be NULL if we have no elements
      }
      return array($struct);
    }

    /**
     * Returns the first element of a given traversable data structure
     * or the data structure itself
     *
     * @param  var struct
     * @param  var
     */
    protected function valueOf($struct) {
      if (is_array($struct) || $struct instanceof Traversable) {
        foreach ($struct as $element) {
          return $element;
        }
      }
      return $struct;
    }
    
    /**
     * Convert data based on type
     *
     * @param   lang.Type type
     * @param   [:var] data
     * @return  var
     */
    public function convert($type, $data) {
      if (NULL === $type || $type->equals(Type::$VAR)) {  // No conversion
        return $data;
      } else if (NULL === $data) {                        // Valid for any type
        return NULL;
      } else if ($type->equals(XPClass::forName('util.Date'))) {
        return $type->newInstance($data);
      } else if ($type instanceof XPClass) {

        // Check if a one-arg public constructor exists and pass first element<commit_msg>- support for lang.types.String serialization<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses('webservices.rest.Payload');

  /**
   * Deserializer abstract base class
   *
   */
  abstract class RestDeserializer extends Object {

    /**
     * Calculate variants of a given name
     *
     * @param   string name
     * @return  string[] names
     */
    protected function variantsOf($name) {
      $variants= array($name);
      $chunks= explode('_', $name);
      if (sizeof($chunks) > 1) {      // product_id => productId
        $variants[]= array_shift($chunks).implode(array_map('ucfirst', $chunks));
      }
      return $variants;
    }

    /**
     * Returns the first element of a given traversable data structure
     * or the data structure itself, or NULL if the structure has more
     * than one element.
     *
     * @param  var struct
     * @param  var[]
     */
    protected function keyOf($struct) {
      if (is_array($struct) || $struct instanceof Traversable) {
        $return= NULL;
        foreach ($struct as $element) {
          if (NULL === $return) {
            $return= array($element);
            continue;
          }
          return NULL;    // Found a second element, return NULL
        }
        return $return;   // Will be NULL if we have no elements
      }
      return array($struct);
    }

    /**
     * Returns the first element of a given traversable data structure
     * or the data structure itself
     *
     * @param  var struct
     * @param  var
     */
    protected function valueOf($struct) {
      if (is_array($struct) || $struct instanceof Traversable) {
        foreach ($struct as $element) {
          return $element;
        }
      }
      return $struct;
    }
    
    /**
     * Convert data based on type
     *
     * @param   lang.Type type
     * @param   [:var] data
     * @return  var
     */
    public function convert($type, $data) {
      if (NULL === $type || $type->equals(Type::$VAR)) {  // No conversion
        return $data;
      } else if (NULL === $data) {                        // Valid for any type
        return NULL;
      } else if ($type->equals(XPClass::forName('lang.types.String'))) {
        return new String($this->valueOf($data));
      } else if ($type->equals(XPClass::forName('util.Date'))) {
        return $type->newInstance($data);
      } else if ($type instanceof XPClass) {

        // Check if a one-arg public constructor exists and pass first element
{'fraction_non_alphanumeric': 0.12205128205128205, 'fraction_numerical': 0.00041025641025641023, 'mean_word_length': 1.9695493300852618, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3748886', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1358, 'n_words': 433}