예제 #1
0
 /**
  * @param Property[] $properties
  * @param array $args {
  *      @type integer|null $tab_count
  *      @type string $trim
  * }
  *
  * @return string
  */
 function get_php_initializers($properties, $args = array())
 {
     static $depth = 0;
     if (0 === $depth++) {
         /*
          * Many properties can accept defaults.
          * So remove them from the array so we
          * don't have to generate them redundantly.
          */
         $properties = $this->filter_redundant($properties);
     }
     $args = Util::parse_args($args, array('tab_count' => 3, 'trim' => 'trim'));
     $output = array();
     $tabs = str_repeat("\t", $args['tab_count']);
     /**
      * Find the longest name so we can later pad between the
      * single-quoted name and the => operator to align the values.
      */
     $max_name_length = 0;
     $tracker = 0;
     $is_associative = false;
     foreach (array_keys($properties) as $index) {
         if ($index !== $tracker++) {
             $is_associative = true;
             break;
         }
     }
     /**
      * @var Property $property
      */
     foreach ($properties as $property_name => $property) {
         if ($property instanceof Property) {
             $value = $this->get_object_value($property_name);
             $value = $property->cast($value);
         } else {
             /*
              * For when called recursively on numerically-indexed
              * or other non-property arrays
              */
             $value = $property;
         }
         if (is_null($value)) {
             continue;
         }
         $prefix = $is_associative ? "{$tabs}'{$property_name}' => " : $tabs;
         switch (gettype($value)) {
             case 'string':
                 $output[] = "{$prefix}'{$value}',";
                 break;
             case 'integer':
                 if (0 === $value && $property->is_true('omit_if_zero')) {
                     continue;
                 }
                 $output[] = "{$prefix} {$value},";
                 break;
             case 'boolean':
                 $bool = $value ? 'true' : 'false';
                 $output[] = "{$prefix} {$bool},";
                 break;
             case 'array':
                 $values = $this->get_php_initializers((array) $value, array('tab_count' => 1 + $args['tab_count'], 'trim' => 'notrim'));
                 $output[] = "{$prefix} array(\n{$values}\n{$tabs}),";
                 break;
             case 'object':
                 $as_array = $this->get_php_initializers((array) $value, $args);
                 $output[] = preg_replace("#(=>\\s+)(array\\()#", '=>$1(object) $2', $as_array);
                 break;
             default:
                 break;
         }
         $max_name_length = max(strlen($property_name), $max_name_length);
     }
     if ($extra = $max_name_length % 4) {
         $max_name_length += 4 - $extra;
     }
     foreach ($output as $index => $line) {
         /**
          * Calculate the padding were four spaces get a tab between the
          * single-quoted name and the => operator to align the values.
          */
         $whitespace = $max_name_length - strpos(trim($line), '=>') + self::TAB_WIDTH;
         $extra_tab = 0 !== $whitespace % self::TAB_WIDTH;
         $num_tabs = $whitespace >= self::TAB_WIDTH ? floor($whitespace / self::TAB_WIDTH) : 0;
         $padding = str_repeat("\t", $num_tabs) . ($extra_tab ? "\t" : '');
         $output[$index] = str_replace('=>', "{$padding}=>", $output[$index]);
     }
     $output = implode("\n", $output);
     $depth--;
     return 'trim' === $args['trim'] ? ltrim($output) : $output;
 }