Download the PHP package lappcloud/php-code-generator without Composer

On this page you can find all versions of the php package lappcloud/php-code-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-code-generator

PHP常量代码生成

遇到的问题:

PM给了一个包含几百条记录的客户和客户代码的excel表,需要将其转成一个PHP的Map数组。一条一条复制很麻烦,所以有了这个库。

输入数据:json,excel文本

输出数据:

PHP二维数组代码 等同于 json的对象数组(第一行作为key)

PHP普通数组代码(只取某一列组成数组,默认第一列)

PHP关联数组代码(只取其中某两列,一列作为key,一列作为value)

json对象数组转php数组(原样输出)

json对象数组转php数组(通过模板过滤)

json字符串美化(可以为每行设置输出前缀)

开发思路

Image text

安装

推荐通过composer扩展的方式进行安装

composer require lappcloud/php-code-generator

或者

"lappcloud/php-code-generator": "*"

独立使用

git clone https://github.com/lappcloud/php-code-generator.git && cd php-code-generator && composer update

用待处理字符串特换掉$string,然后执行对应的php文件,如:

php test/1.字符串生成对象数组.php

使用示例

1.生成对象数组,json的概念,这里用起来比较形象(每行一个对象,取第一行作为key)

   use lappcloud\codegen\GenPHPCode;

   // 生成对象数组,json的概念,这里用起来比较形象(每行一个对象,取第一行作为key)
   $string = <<<STR
   id   name    sex age
   1    张三  man 18
   2    李四  man 17
   STR;

   // 或者 形如 $string 的 json
   $json = <<<JSON
   [["id","name","sex","age"],["1","\u5f20\u4e09","man","18"],["2","\u674e\u56db","man","17"]]
   JSON;
   try {
       $obj = new GenPHPCode();

       $obj->intInputType = GenPHPCode::INPUT_TYPE_STRING; // 输入类型为字符串
       //$obj->intInputType = GenPHPCode::INPUT_TYPE_JSON; // 输入类型为字符串

       $obj->strColDelimiter = "\t";  // 列分割符
       $obj->strRowDelimiter = "\n";  // 输入数据列数

       $obj->keys = '1-3';
       $obj->intFormatType = GenPHPCode::FORMAT_TYPE_OBJECT_ARRAY;

       $res = $obj->str2Code($string);
       //$res = $obj->str2Code($json);
   } catch (\Exception $e) {
       echo 'Error:' . $e->getMessage() . "\n"; exit;
   }

   ob_clean();
   echo $res;

   // 输出:
   [
       [
           'id'   => '1',
           'name' => '张三',
           'sex'  => 'man',
           'age'  => '18',
       ],
       [
           'id'   => '2',
           'name' => '李四',
           'sex'  => 'man',
           'age'  => '17',
       ],
   ];

2.生成普通数组(只取某一列组成数组,默认第一列)

include 'vendor/autoload.php';

// 生成普通数组(只取某一列组成数组,默认第一列)
$string = <<<STR
1   张三  man 18
2   李四  man 17
STR;

// 或者 形如 $string 的 json
$json = <<<JSON
[["1","\u5f20\u4e09","man","18"],["2","\u674e\u56db","man","17"]]
JSON;

try {
    $obj = new GenPHPCode();

    $obj->intInputType = GenPHPCode::INPUT_TYPE_STRING; // 输入类型为字符串
    $obj->intInputType = GenPHPCode::INPUT_TYPE_JSON; // 输入类型为字符串

    $obj->strColDelimiter = "\t";  // 列分割符
    $obj->strRowDelimiter = "\n";  // 输入数据列数

    $obj->keys = '1-3';
    $obj->intFormatType = GenPHPCode::FORMAT_TYPE_ORDINARY_ARRAY;

    $res = $obj->str2Code($json);
} catch (\Exception $e) {
    echo 'Error:' . $e->getMessage() . "\n"; exit;
}

ob_clean();
echo $res;

// 输出:
[
    '张三',
    '李四',
];

3.取excel的第2列数据作为key,第3列作为value,第三列数据作为value生成关联数组

  include 'vendor/autoload.php';

  // 取excel的第2列数据作为key,第三列数据作为value生成关联数组

  $string = <<<STR
  1 Q010Y   客户A
  1 Q011Y   客户B
  STR;

  // 或者 形如 $string 的 json
  $json = <<<JSON
  [[1,"Q010Y","\u5ba2\u6237A"],[2,"Q011Y","\u5ba2\u6237B"]]
  JSON;
  try {
      $obj = new GenPHPCode();

      $obj->intInputType = GenPHPCode::INPUT_TYPE_STRING; // 输入类型为字符串
      //$obj->intInputType = \GenPHPCode\GenPHPCode::INPUT_TYPE_JSON; // 输入类型为字符串

      $obj->strColDelimiter = "\t";  // 列分割符
      $obj->strRowDelimiter = "\n";  // 输入数据列数

      $obj->keys = '1-2';
      $obj->intFormatType = GenPHPCode::FORMAT_TYPE_LIST_ARRAY;

      $res = $obj->str2Code($string);
  } catch (\Exception $e) {
      echo 'Error:' . $e->getMessage() . "\n"; exit;
  }

  ob_clean();
  echo $res;

  // 输出:
  [
      'Q010Y' => '客户A',
      'Q011Y' => '客户B',
  ];

4.json对象数组转php数组(原样输出)

include 'vendor/autoload.php';

use lappcloud\codegen\GenPHPCode;

$json = <<<JSON
[{"name":"Google","url":{"0":"value","1":"value","2":"value","field":"value"},"tag":["search","tag1"]},{"name":"Baidu","url":"http://xxx"}]
JSON;

try {
    $obj = new GenPHPCode();
    $obj->intInputType = GenPHPCode::INPUT_TYPE_JSON;
    $obj->intFormatType = GenPHPCode::FORMAT_TYPE_NO;

    $res = $obj->str2Code($json);
} catch (\Exception $e) {
    echo 'Error:' . $e->getMessage() . "\n"; exit;
}

ob_clean();
echo $res;

// 输出:
[
    [
        'name' => 'Google',
        'url'  => [
            '0'     => 'value',
            '1'     => 'value',
            '2'     => 'value',
            'field' => 'value',
        ],
        'tag'  => [
            'search',
            'tag1',
        ],
    ],
    [
        'name' => 'Baidu',
        'url'  => 'http://xxx',
    ],
];

5.json对象数组转php数组(通过模板过滤)

include 'vendor/autoload.php';

use lappcloud\codegen\GenPHPCode;

$json = <<<JSON
[{"name":"Google","url":{"0":"value","1":"value","2":"value","field":"value"},"tag":["search","tag1"]},{"name":"Baidu","url":"http://xxx"}]
JSON;

$template = [
    'name' => true,
    'url'  => [
        '0'     => true,
        'field' => true,
    ],
    'tag'  => true,
];

try {
    $obj = new GenPHPCode();
    $obj->intInputType = GenPHPCode::INPUT_TYPE_JSON;
    $obj->intFormatType = GenPHPCode::FORMAT_TYPE_OBJECT_TEMPLATE;
    $obj->template = $template;

    $res = $obj->str2Code($json);
} catch (\Exception $e) {
    echo 'Error:' . $e->getMessage() . "\n"; exit;
}

ob_clean();
echo $res;

// 输出:
[
    [
        'name' => 'Google',
        'url'  => [
            '0'     => 'value',
            'field' => 'value',
        ],
        'tag'  => [
            'search',
            'tag1',
        ],
    ],
    [
        'name' => 'Baidu',
        'url'  => [
            '0'     => [
            ],
            'field' => [
            ],
        ],
        'tag'  => [
        ],
    ],
];

6.json对象数组转php数组(通过模板校验)

include 'vendor/autoload.php';

use lappcloud\codegen\format\FormatByTemplate;

$json = <<<JSON
[{"name":"Google","url":{"0":"value","1":"value","2":"value","field":"value"},"tag":["search","tag1"]},{"name":"Baidu","url":"http://xxx"}]
JSON;

$template = [
    'name' => true,
    'url'  => [
        '0'     => true,
        'field' => true,
    ],
    'tag'  => true,
];

print_r(FormatByTemplate::validate(json_decode($json, true), $template));

// 输出:

//Array
//(
//    [0] => Array
//        (
//            [error_data] => Array
//                (
//                    [name] => Baidu
//                    [url] => http://xxx
//                )
//
//            [error_msg] => url是字符串类型
//        )
//
//)

License

MIT


All versions of php-code-generator with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package lappcloud/php-code-generator contains the following files

Loading the files please wait ....