/**
  * @param $obj object to transform
  * @param OutputStream $output
  * @param Configuration $conf
  * @return void
  */
 public function transform($obj, OutputStream $output, Configuration $conf)
 {
     if ($conf == null) {
         $conf = new Configuration();
     }
     $strategy = $conf->getString(Configuration::DATE_STRATEGY_PROPERTY, Configuration::DATE_STRATEGY_TIMESTAMP);
     if ($strategy == Configuration::DATE_STRATEGY_TIMESTAMP) {
         $res = $obj->getTimestamp();
         $output->write("" . $res);
     } else {
         $format = $conf->getString(Configuration::DATE_FORMAT_PROPERTY, Configuration::DEFAULT_DATE_FORMAT);
         $res = $obj->format($format);
         $output->write("\"" . $res . "\"");
     }
 }
Esempio n. 2
0
 /**
  * @param $obj object to transform
  * @param OutputStream $output
  * @param Configuration $conf
  * @throws \Exception
  */
 public function transform($obj, OutputStream $output, Configuration $conf)
 {
     $i = 0;
     $refClass = new \ReflectionClass($obj);
     $props = $refClass->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE);
     if (count($props) > 0) {
         $output->write("{");
         $clazz = get_class($obj);
         $excludeNull = $conf->getBoolean(Configuration::EXCLUDE_NULL_PROPERTY, false);
         $includeClass = $conf->getBoolean(Configuration::INCLUDE_CLASS_NAME_PROPERTY, false);
         $clazzName = $conf->getString(Configuration::CLASS_PROPERTY, Configuration::DEFAULT_CLASS_PROPERTY_VALUE);
         $clazzStrategy = $conf->getString(Configuration::CLASS_TYPE_PROPERTY, LanguageStrategyFactory::LANG_PHP);
         foreach ($props as $prop) {
             // check getter
             $val = null;
             try {
                 $refMethod = new \ReflectionMethod($clazz, "get" . ucfirst($prop->getName()));
                 if ($refMethod->isPublic()) {
                     $val = $refMethod->invoke($obj);
                 }
             } catch (\Exception $e) {
             }
             if (!isset($val)) {
                 $prop->setAccessible(true);
                 $val = $prop->getValue($obj);
             }
             if ($excludeNull === false || ($val !== null || $val !== "")) {
                 if ($prop->getName() != $clazzName) {
                     if ($i != 0) {
                         $output->write(",");
                     }
                     $output->write("\"" . JsonEscapeUtils::escapeJson($prop->getName()) . "\":");
                     TransformerFactory::get($val)->transform($val, $output, $conf);
                     $i++;
                 }
             }
         }
         if ($includeClass) {
             $clazz = LanguageStrategyFactory::getClassStrategy($clazzStrategy)->className($clazz);
             if ($i != 0) {
                 $output->write(",");
             }
             $output->write("\"{$clazzName}\":\"{$clazz}\"");
         }
         $output->write("}");
     } else {
         $output->write("null");
     }
 }
Esempio n. 3
0
 /**
  * @param $c
  * @return Pair
  */
 private function parseStructure($c)
 {
     $m = null;
     $l = null;
     $sb = "";
     $pn = null;
     switch ($c) {
         case self::START_MAP:
             $m = array();
             $this->queue->enqueue($m);
             $c = $this->parseMap($m);
             if ($c == self::END_MAP) {
                 $this->queue->dequeue();
                 $cl = $this->conf->getString(Configuration::CLASS_PROPERTY, Configuration::DEFAULT_CLASS_PROPERTY_VALUE);
                 $p = new Pair($c, $m);
                 if (isset($m[$cl])) {
                     $o = $this->createClass($m);
                     $p = new Pair($c, $o);
                 }
                 if (false !== ($ch = $this->in->nextChar())) {
                     $p->setLeft($ch);
                 }
                 return $p;
             }
             break;
         case self::START_ARRAY:
             $l = array();
             $this->queue->enqueue($l);
             $c = $this->parseList($l);
             if ($c == self::END_ARRAY) {
                 $this->queue->dequeue();
                 if (false !== ($ch = $this->in->nextChar())) {
                     $c = $ch;
                 }
                 return new Pair($c, $l);
             }
             break;
         case self::STRING_CHAR:
         case self::CHAR_CHAR:
             $sb = "";
             $this->queue->enqueue($sb);
             $c = $this->parseString($c, $sb);
             $pn = trim($sb);
             $this->queue->dequeue();
             if (strtolower($pn) == "null") {
                 return new Pair($c, "");
             }
             return new Pair($c, $pn);
         default:
             $sb = $c;
             $this->queue->enqueue($sb);
             $c = $this->parseNumber($sb);
             $pn = trim($sb);
             $this->queue->dequeue();
             if (strtolower($pn) == "false" || strtolower($pn) == "true") {
                 return new Pair($c, strtolower($pn) == "false" ? false : true);
             } else {
                 if (strtolower($pn) == "null") {
                     return new Pair($c, "");
                 }
             }
             if (preg_replace("/[0-9]/", "", $pn) == "") {
                 $o = intval($pn);
             } else {
                 if (preg_replace("/[0-9\\.]/", "", $pn) == "") {
                     $o = floatval($pn);
                 } else {
                     $o = $pn;
                 }
             }
             return new Pair($c, $o);
     }
     return new Pair($c, null);
 }