/**
  * Generates the PHP array string for an array of records. Will use
  * var_export() and PHPEncoder for more sophisticated types.
  *
  * @param array $records Array of seed records
  * @return string PHP Code
  */
 public function stringifyRecords(array $records)
 {
     $out = "[\n";
     $out = '';
     $encoder = new PHPEncoder();
     foreach ($records as $record) {
         $values = [];
         foreach ($record as $field => $value) {
             if ($value instanceof \DateTime) {
                 $value = $value->format('Y-m-d H:i:s');
             }
             if (is_array($value)) {
                 // FIXME: the encoder will forget precisions of floats
                 $val = $encoder->encode($value, ['array.inline' => false, 'array.omit' => false, 'array.indent' => 4, 'boolean.capitalize' => false, 'null.capitalize' => false, 'string.escape' => false, 'array.base' => 12, 'float.integers' => "all", 'float.precision' => false]);
             } else {
                 $val = var_export($value, true);
             }
             if ($val === 'NULL') {
                 $val = 'null';
             }
             $values[] = "            '{$field}' => {$val}";
         }
         $out .= "        [\n";
         $out .= implode(",\n", $values);
         $out .= "\n        ],\n";
     }
     #$out .= "    ]";
     return $out;
 }
Example #2
0
    /**
     * 响应json数据
     *
     * @param int $baseTab tab数
     * @param array $data 输出json数据
     * @return array
     */
    public function generateResponseJson($baseTab = 2, $data)
    {
        $encoder = new PHPEncoder();
        $dataCode = $encoder->encode($data, ['string.escape' => false, 'array.base' => $baseTab * 4, 'array.inline' => 120]);
        $dataCode = str_replace('\'$data\'', '$data', $dataCode);
        $dataCode = str_replace('\'$message\'', '$message', $dataCode);
        $space = str_repeat(' ', $baseTab * 4);
        $code = '$response = Yii::$app->response;
' . $space . '$response->format = Response::FORMAT_JSON;
' . $space . '$response->data = ' . $dataCode . ';
' . $space . 'return $response;';
        return $code;
    }