Пример #1
0
 public function transform($obj, OutputStream $output, Configuration $conf)
 {
     $i = 0;
     $output->write("[");
     foreach ($obj as $elem) {
         if ($i != 0) {
             $output->write(",");
         }
         TransformerFactory::get($elem)->transform($elem, $output, $conf);
         $i++;
     }
     $output->write("]");
 }
Пример #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;
     $output->write("{");
     while (list($key, $value) = each($obj)) {
         if ($i != 0) {
             $output->write(",");
         }
         $output->write("\"" . JsonEscapeUtils::escapeJson($key) . "\":");
         TransformerFactory::get($value)->transform($value, $output, $conf);
         $i++;
     }
     $output->write("}");
 }
Пример #3
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");
     }
 }
Пример #4
0
 /**
  * @param $obj
  */
 public function parse($obj)
 {
     TransformerFactory::get($obj)->transform($obj, $this->out, $this->conf);
 }