0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

composerを用いてライブラリ(Faker)を使ってみた

Last updated at Posted at 2021-08-07

今回はcomposerを使って便利なライブラリ「Faker」を使ってみたいと思います。

#前提条件
composerのインストールまでは完了している事とします。

composerバージョンチェック
composer --version

Composer version 2.0.8 2020-12-03 17:20:38

任意のディレクトリに移動
cd <任意のディレクトリ>

composer.jsonを作成する
composer require fakerphp/faker

Using version ^1.15 for fakerphp/faker
./composer.json has been created
Running composer update fakerphp/faker
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
  - Locking fakerphp/faker (v1.15.0)
  - Locking psr/container (1.1.1)
  - Locking symfony/deprecation-contracts (v2.4.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Downloading fakerphp/faker (v1.15.0)
  - Installing symfony/deprecation-contracts (v2.4.0): Extracting archive
  - Installing psr/container (1.1.1): Extracting archive
  - Installing fakerphp/faker (v1.15.0): Extracting archive
Generating autoload files

composer.jsonの中はこんな感じになっています。

composer.json
{
    "require": {
        "fakerphp/faker": "^1.15"
    }
}

検証用プログラム

faker.php

<?php

    require_once '../vendor/autoload.php';

    $faker = Faker\Factory::create('ja_JP');

    $gender = ['男性','女性', 'その他'];
    $createCnt = 2;

    $result = array();
    for($i=0; $i<$createCnt; $i++)
    {
        $result[$i]["name"]           = $faker->name;
        $result[$i]["homePostcode"]   = "〒". preg_replace("/^.{0,3}+\K/us", '-', $faker->postcode);
        $result[$i]["homeAddress"]    = $faker->prefecture. " ". $faker->city. " ". $faker->streetAddress;
        $result[$i]["userName"]       = $faker->userName;
        $result[$i]["password"]       = $faker->password;
        $result[$i]["email"]          = $faker->email;
        $result[$i]["phoneNumber"]    = $faker->phoneNumber;
        $result[$i]["birthDay"]       = $faker->date($format='Y-m-d',$max='now');
        $result[$i]["companyName"]    = $faker->company;
        $result[$i]["companyPostcode"]   = "〒". preg_replace("/^.{0,3}+\K/us", '-', $faker->postcode);
        $result[$i]["companyNumber"]     = $faker->phoneNumber;
        $result[$i]["creditCardNumber"]  = $faker->creditCardNumber;
        $result[$i]["randomElement"]     = $faker->randomElement($gender);        
    }

    echo '<pre>';
    var_dump($result);

検証プログラム出力値

array(2) {
  [0]=>
  array(13) {
    ["name"]=>
    string(10) "山田 浩"
    ["homePostcode"]=>
    string(11) "〒110-5962"
    ["homeAddress"]=>
    string(43) "神奈川県 廣川市 桐山町井上4-3-9"
    ["userName"]=>
    string(12) "kaori.sasaki"
    ["password"]=>
    string(12) ",E9T[]+CZR+q"
    ["email"]=>
    string(23) "ryoshida@mail.goo.ne.jp"
    ["phoneNumber"]=>
    string(12) "04566-5-7271"
    ["birthDay"]=>
    string(10) "2002-07-03"
    ["companyName"]=>
    string(19) "株式会社 原田"
    ["companyPostcode"]=>
    string(11) "〒345-2205"
    ["companyNumber"]=>
    string(12) "0170-478-697"
    ["creditCardNumber"]=>
    string(16) "5336417868933482"
    ["randomElement"]=>
    string(6) "男性"
  }
  [1]=>
  array(13) {
    ["name"]=>
    string(13) "渚 美加子"
    ["homePostcode"]=>
    string(11) "〒501-3212"
    ["homeAddress"]=>
    string(40) "徳島県 田辺市 井高町宇野5-2-3"
    ["userName"]=>
    string(13) "aoyama.kumiko"
    ["password"]=>
    string(20) "gjp}?y'N=?6;^iEg,]hv"
    ["email"]=>
    string(19) "tsubasa61@miyake.jp"
    ["phoneNumber"]=>
    string(12) "02-7506-5825"
    ["birthDay"]=>
    string(10) "1974-08-05"
    ["companyName"]=>
    string(19) "株式会社 加納"
    ["companyPostcode"]=>
    string(11) "〒305-4982"
    ["companyNumber"]=>
    string(12) "030-717-5832"
    ["creditCardNumber"]=>
    string(16) "4556485707981075"
    ["randomElement"]=>
    string(6) "女性"
  }
}

データの精度は高くないものの、
すぐにダミーデータが必要な場合は重宝しそうですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?