Example #1
0
 public static function empty_to_null($array)
 {
     foreach ($array as $key => &$element) {
         if (is_array($element)) {
             $element = self::empty_to_null($element);
         } else {
             $array[$key] = PhpHelper::empty_ignoring_zero(trim($element)) ? null : $element;
         }
     }
     return $array;
 }
Example #2
0
 public function writeCache(\Symforce\CoreBundle\PhpHelper\PhpWriter $writer)
 {
     $default_value = $this->getDefaultValue();
     if ($this->_lazy) {
         $this->_class->getLazyWriter()->writeln('$this->' . $this->getName() . ' = ' . PhpHelper::compilePropertyValue($default_value) . ' ; ');
         $default_value = null;
     }
     $writer->write("\n");
     if ($this->getDocblock()) {
         $writer->writeln($this->getDocblock());
     }
     $writer->write($this->getVisibility())->write(' $')->write($this->getName());
     if (null !== $default_value) {
         $writer->write(' = ')->write(PhpHelper::compilePropertyValue($default_value));
     }
     $writer->writeln(";");
     if ($this->_get) {
         $this->genGetter();
     }
 }
Example #3
0
 public function writeCache($_class_file = null)
 {
     if (!$_class_file) {
         $_class_file = \Symforce\CoreBundle\PhpHelper\PhpHelper::findFileByClassName($this->getName());
     }
     $shortName = pathinfo($_class_file, \PATHINFO_FILENAME);
     $namespace = $this->getNamespace();
     $writer = new \Symforce\CoreBundle\PhpHelper\PhpWriter();
     $writer->writeln("<?php\n");
     if (!empty($namespace)) {
         $writer->writeln("namespace " . $namespace . ";\n");
     }
     $imports = $this->getUseStatements();
     foreach ($imports as $alias => $use) {
         $_alias = substr($use, -1 - strlen($alias));
         if ($_alias == '\\' . $alias) {
             $writer->writeln(sprintf("use %s ;", $use));
         } else {
             $writer->writeln(sprintf("use %s as %s ;", $use, $alias));
         }
     }
     $doc_block = $this->getDocblock();
     $writer->writeln("/**")->writeln($doc_block ? $doc_block : sprintf(' * This code has been auto-generated by \\%s', __CLASS__))->writeln(" */\n");
     if ($this->isAbstract()) {
         $writer->write('abstract ');
     } else {
         if ($this->isFinal()) {
             $writer->write('final ');
         }
     }
     $writer->write('class ' . $shortName);
     if ($this->getParentClassName()) {
         $writer->write(' extends \\' . ltrim($this->getParentClassName(), '\\'));
     }
     $writer->writeln(" {\n")->indent();
     foreach ($this->_traits as $_trait) {
         if (!trait_exists($_trait)) {
             throw new \Exception();
         }
         $writer->writeln(' use \\' . ltrim($_trait, '\\') . ';');
     }
     foreach ($this->getConstants() as $name => $value) {
         $writer->writeln(sprintf('const %s = %s;', $name, var_export($value, 1)));
     }
     /**
      * @var $property PhpProperty
      */
     foreach ($this->getProperties() as $property) {
         $property->writeCache($writer);
     }
     foreach ($this->lazy_properties as $visible => $visible_values) {
         foreach ($visible_values as $name => $value) {
             $writer->writeln(sprintf("\n%s \$%s = %s ;", $visible, $name, PhpHelper::compilePropertyValue($value)));
         }
     }
     if ($this->lazy_writer) {
         $this->lazy_writer->writeln($this->lazy_method->getBody());
         $this->lazy_method->setBody($this->lazy_writer->getContent());
     }
     /**
      * @var $method PhpMethod
      */
     foreach ($this->getMethods() as $method) {
         if ($method instanceof PhpMethod) {
             $method->flushLazyCode();
             $_body = $method->getWriter()->getContent();
         } else {
             $_body = $method->getBody();
         }
         $writer->write("\n");
         if ($method->getDocblock()) {
             $writer->writeln($method->getDocblock());
         }
         if ($method->isFinal()) {
             $writer->write('final ');
         }
         $writer->write($method->getVisibility())->write(' function ')->write($method->getName());
         $ps = $method->getParameters();
         if (empty($ps)) {
             $writer->write('()');
         } else {
             $writer->write('(');
             foreach ($method->getParameters() as $i => $p) {
                 if ($p->getType()) {
                     if (in_array($p->getType(), array('mixed'))) {
                         $writer->write('/** @var ' . $p->getType() . ' */ ');
                     } else {
                         $writer->write($p->getType() . ' ');
                     }
                 }
                 if ($p->isPassedByReference()) {
                     $writer->write(' & ');
                 }
                 $writer->write('$')->write($p->getName());
                 if ($p->hasDefaultValue()) {
                     $writer->write(' = ' . json_encode($p->getDefaultValue()));
                 }
                 if ($i < count($ps) - 1) {
                     $writer->write(", ");
                 }
             }
             $writer->write(')');
         }
         $writer->writeln('{')->indent()->write($_body)->outdent()->writeln("}");
     }
     $writer->outdent()->writeln('}');
     $content = PhpHelper::decompilePhpCode($writer->getContent());
     $_class_dir = dirname($_class_file);
     if (!file_exists($_class_dir)) {
         if (!@mkdir($_class_dir, 0755, true)) {
             throw new \Exception(sprintf("mkdir(%s) error!", $_class_dir));
         }
     }
     PhpHelper::write_file($_class_file, $content);
     return $_class_file;
 }
Example #4
0
 /**
  * isCli
  *
  * @return  boolean
  */
 public function isCli()
 {
     return PhpHelper::isCli();
 }