PHP code example of pelmered / fake-car

1. Go to this page and download the library: Download pelmered/fake-car library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pelmered / fake-car example snippets


$faker = (new \Faker\Factory())::create();
$faker->addProvider(new \Faker\Provider\FakeCar($faker));


// generate matching automobile brand and model of a car as a string
echo $faker->vehicle(); // 'Volvo 740'

// generate matching automobile brand and model of a car as an array
echo $faker->vehicleArray(); // [ 'brand' => 'Hummer', 'model' => 'H1' ]

// generate only automobile brand
echo $faker->vehicleBrand(); // 'Ford'

// generate automobile manufacturer and model of car
echo $faker->vehicleModel(); // '488 Spider'

// generate Vehicle Identification Number(VIN) - https://en.wikipedia.org/wiki/Vehicle_identification_number
echo $faker->vin(); // 'd0vcddxpXAcz1utgz'

// generate automobile registration number
echo $faker->vehicleRegistration(); // 'ABC-123'

// generate automobile registration number with custom format
echo $faker->vehicleRegistration('[A-Z]{2}-[0-9]{5}'); // AB-12345

// generate automobile model type
echo $faker->vehicleType(); // 'hatchback'

// generate automobile fuel type
echo $faker->vehicleFuelType(); // 'diesel'
echo $faker->vehicleFuelType(2); // ['diesel', 'gasoline']

// generate automobile door count
echo $faker->vehicleDoorCount(); // 4

// generate automobile seat count
echo $faker->vehicleSeatCount(); // 5

// generate automobile properties
echo $faker->vehicleProperties(); // ['Towbar','Aircondition','GPS', 'Leather seats']

// generate automobile gear type (manual or automatic)
echo $faker->vehicleGearBoxType(); // manual

// generate automobile engine power
echo $faker->vehicleEnginePower(); // '250 hp'

// generate automobile engine power without a unit
echo $faker->vehicleEnginePowerValue(); // 175

// generate automobile engine torque
echo $faker->vehicleEngineTorque(); // '300 nm'

// generate automobile engine power without a unit
echo $faker->vehicleEngineTorqueValue(); // 450

// generate automobile engine displacement
echo $faker->vehicleEngineDisplacement(); // '2.0 l'

// generate automobile engine displacement without unit
echo $faker->vehicleEngineDisplacementValue(); // 2.0

// generate automobile engine fuel consumption
echo $faker->vehicleFuelConsumption(); // '5.0 l/100km'

// generate automobile engine fuel consumption without unit
echo $faker->vehicleFuelConsumptionValue(); // 5.0

// generate automobile engine fuel consumption without unit
echo $faker->vehicleEngineCylinders(); // 4


namespace Database\Factories;

use App\Models\Vehicle;
use Faker\Provider\FakeCar;
use Illuminate\Database\Eloquent\Factories\Factory;

class VehicleFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Vehicle::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $this->faker->addProvider(new FakeCar($this->faker));
        $vehicle = $this->faker->vehicleArray();

        return [
            'vehicle_type'    => 'car',
            'vin'             => $this->faker->vin,
            'registration_no' => $this->faker->vehicleRegistration,
            'chassis_type'    => str_replace(' ', '_', $this->faker->vehicleType),
            'fuel'            => $this->faker->vehicleFuelType,
            'brand'           => $vehicle['brand'],
            'model'           => $vehicle['model'],
            'year'            => $this->faker->biasedNumberBetween(1990, date('Y'), 'sqrt'),
        ];
    }
}


class BMWFakeCarData extends \Faker\Provider\FakeCarData
{
    public static $brandsWithModels = [
        'BMW' => [
            '8 Series', 'M1', 'X5', 'Z1', 'Z3', 'Z4', 'Z8', 'Alpina', 'E', 'X3', 'M', 'X6', '1 Series', '5 Series',
            'X5 M', 'M5', '750', '6 Series', '3 Series', 'M3', 'X6 M', 'M6', 'X1', '7 Series', '325', '324', '316',
            '320', '318', '328', '523', '740', '520', '728', '525', 'Isetta', '530', '528', '545', '535', 'Dixi',
            '730', '745', '518', '524', '540', '116', '118', '120', '123', '125', '130', '135', '323', '330', '335',
            '550', '628', '630', '633', '635', '645', '650', '640', '760', '735', '732', '725', 'X series', 'X8',
            '340', 'RR', '1 Series М', '321', '315', '6 Series Gran Coupe', 'X2', '4 Series', '428', '435', '420',
            '2 Series', '3 Series GT', 'X4', '4 Series Gran Coupe', '326', 'I8', '5 Series GT', 'I3', 'M2', 'M4',
            'Neue Klasse', '1602', 'Active Hybrid 7', '2002', '2000', 'F10', 'X7', '128', '6 Series GT'
        ],
    ];

    public static $vehicleTypes = [
        'hatchback', 'sedan', 'convertible', 'SUV', 'coupe',
    ];

    public static $vehicleFuelTypes = [
        'gasoline' => 40,
        'electric' => 10,
        'diesel' => 20,
    ];
}

$fakeCarDataProvider = new \Faker\Provider\FakeCarDataProvider(new BMWFakeCarData);

$faker = (new \Faker\Factory())::create();
$fakeCar = new \Faker\Provider\FakeCar($faker);
$fakeCar->setDataProvider($fakeCarDataProvider);
$faker->addProvider($fakeCar);

echo $faker->vehicleBrand; // BMW


namespace FakeCar\Tests\TestProviders;

use Faker\Provider\FakeCarDataProviderInterface;
use Faker\Provider\FakeCarHelper;

class FerrariEnzoTestProvider implements FakeCarDataProviderInterface
{

    public function getVehicleBrand(): string
    {
        return 'Ferrari';
    }

    public function getVehicleModel(): string
    {
        return 'Enzo';
    }

    public function getBrandsWithModels(): array
    {
        return [
            'brand' => $this->getVehicleBrand(),
            'model' => $this->getVehicleModel(),
        ];
    }

    public function getVehicleType(): string
    {
        return 'coupe';
    }

    public function getVehicleFuelType(): string|array
    {
        return 'gasoline';
    }

    public function getVehicleDoorCount(): int
    {
        return 2;
    }

    public function getVehicleSeatCount(): int
    {
        return 2;
    }

    public function getVehicleProperties(int $count = 0): array
    {
        return [
            'Air condition',
            'GPS',
            'Leather seats',
        ];
    }

    public function getVehicleGearBoxType(): string
    {
        return FakeCarHelper::getWeighted([
            'manual'    => 70,
            'automatic' => 30,
        ]);
    }

}


$fakeCarDataProvider = new FerrariEnzoTestProvider();

$faker = (new \Faker\Factory())::create();
$fakeCar = new \Faker\Provider\FakeCar($faker);
$fakeCar->setDataProvider($fakeCarDataProvider);
$faker->addProvider($fakeCar);

echo $faker->vehicleBrand; // Ferrari
echo $faker->vehicleModel; // Enzo