Overview
  • Namespace
  • Class
  • Tree
  • Todo

Namespaces

  • jp3cki
    • gimei
      • address
      • name

Classes

  • jp3cki\gimei\address\Address
  • jp3cki\gimei\address\AddressUnit
  • jp3cki\gimei\address\Dictionary
  • jp3cki\gimei\address\Factory
  • jp3cki\gimei\DataUnit
  • jp3cki\gimei\Gimei
  • jp3cki\gimei\name\Dictionary
  • jp3cki\gimei\name\Factory
  • jp3cki\gimei\name\Gender
  • jp3cki\gimei\name\NameUnit
  • jp3cki\gimei\name\Person

Exceptions

  • jp3cki\gimei\Exception
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 
<?php
/**
 * @author AIZAWA Hina <hina@bouhime.com>
 * @copyright 2015 AIZAWA Hina <hina@bouhime.com>
 * @license https://github.com/fetus-hina/gimei-php/blob/master/LICENSE MIT
 */

namespace jp3cki\gimei\name;

use jp3cki\gimei\Exception;

/**
 * データを読み込みランダムに選択する
 */
class Factory
{
    /**
     * データファイルへの相対パス
     */
    const JSON_REL_PATH = '../../data/names.json';
    
    /**
     * データファイルを保持する
     * @var Dictionary
     */
    private static $dictionary = null;

    /**
     * 名前をランダムに選択して返す
     *
     * @param mixed $maleRate 男性名が生成される確率(0.0~1.0)
     * @return Person
     */
    public static function generate($maleRate = 0.5)
    {
        if (self::$dictionary === null) {
            self::loadData();
        }
        switch (true) {
            // Gender::* is obsoluted in v1.1.0
            case $maleRate === Gender::MALE:
                $gender = Gender::MALE;
                break;

            case $maleRate === Gender::FEMALE:
                $gender = Gender::FEMALE;
                break;

            case $maleRate === Gender::BOTH:
            case !is_float($maleRate):
                $maleRate = 0.5;
                // fall through
            default:
                $gender = (mt_rand() / mt_getrandmax() <= $maleRate)
                    ? Gender::MALE
                    : Gender::FEMALE;
        }

        $dict = self::$dictionary;

        return new Person(
            new NameUnit($dict->getOneOfFirstName($gender)),
            new NameUnit($dict->getOneOfLastName()),
            $gender === Gender::MALE
        );
    }

    /**
     * データファイルを読み込む
     */
    private static function loadData()
    {
        $jsonPath = __DIR__ . '/' . self::JSON_REL_PATH;
        self::$dictionary = new Dictionary($jsonPath);
    }
}
API documentation generated by ApiGen