PHP code example of sizuhiko / fabricate
1. Go to this page and download the library: Download sizuhiko/fabricate 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/ */
sizuhiko / fabricate example snippets
use Fabricate\Fabricate;
use CakeFabricate\Adaptor\CakeFabricateAdaptor;
Fabricate::config(function($config) {
$config->adaptor = new CakeFabricateAdaptor();
});
Fabricate::create('Post')
Fabricate::create('Post', ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"])
Fabricate::create('Post', 10, function($data){
return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});
$results = Fabricate::attributes_for('Post', 10, function($data){
return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});
// $results is followings :
array (
0 =>
array (
'id' => 1,
'title' => 'Lorem ipsum dolor sit amet',
'body' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida, phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit, feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.',
'created' => '2013-10-09 12:40:28',
'updated' => '2013-10-09 12:40:28',
),
1 =>
array (
....
$result = Fabricate::build('Post', function($data){
return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});
// $results are depends adaptor result.
......
Fabricate::create('Post', 10, function($data){
return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});
Fabricate::define('Post', ['published'=>'1']);
// or using callback block
Fabricate::define('Post', function($data, $world) {
return ['published'=>'1']
});
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::create('PublishedPost');
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::define(['Author5PublishedPost', 'parent'=>'PublishedPost'], ['author_id'=>'5']);
Fabricate::create('Author5PublishedPost');
Fabricate::create('User', function($data, $world) {
return [
'user' => 'taro',
'Post' => $world->association('Post', 3),
];
});
// or can overwritten by array or callback block.
Fabricate::create('User', function($data, $world) {
return [
'user' => 'taro',
'Post' => $world->association('Post', 3, function($data, $world) {
return ['title'=>$world->sequence('Post.title',function($i){ return "Title-${i}"; })];
}),
];
});
// can use defined onbject.
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::create('User', function($data, $world) {
return [
'user' => 'taro',
'Post' => $world->association(['PublishedPost', 'association'=>'Post'], 3),
];
});
// can use association alias (Post belongs to Author of User class)
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::create('PublishedPost', 3, function($data, $world) {
return [
'Author' => $world->association(['User', 'association'=>'Author'], ['id'=>1,'user'=>'taro']),
];
});
Fabricate::config(function($config) {
$config->sequence_start = 100;
});
Fabricate::config(function($config) {
$config->sequence_start = 100;
});
$results = Fabricate::attributes_for('Post', 10, function($data, $world){
return [
'id'=> $world->sequence('id'),
'title'=> $world->sequence('title', 1, function($i){ return "Title {$i}"; })
];
});
// $results is followings :
array (
0 =>
array (
'id' => 100, // starting configure sequence
'title' => 'Title 1', // closure function returned
...
),
1 =>
array (
'id' => 101, // starting configure sequence
'title' => 'Title 2', // closure function returned
...
$world->sequence('id')
$world->sequence('id', 10)
$world->sequence('title', function($i){ return "Title {$i}"; }
// or with start number
$world->sequence('title', 1, function($i){ return "Title {$i}"; }
Fabricate::define(['trait'=>'published'], ['published'=>'1']);
Fabricate::create('Post', function($data, $world) {
$world->traits('published');
return ['author_id'=>5];
});
Fabricate::define(['trait'=>'published'], ['published'=>'1']);
Fabricate::define(['trait'=>'author5'], function($data, $world) { return ['author_id'=>5]; });
Fabricate::create('Post', function($data, $world) {
$world->traits(['published','author5']);
return [];
});
Fabricate::config(function($config) {
$config->faker = Faker\Factory::create('ja_JP');
});
Fabricate::config(function($config) {
$config->faker = Faker\Factory::create('ja_JP'); // this is optional
});
$results = Fabricate::attributes_for('User', function($data, $world){
return [
'user'=> $world->faker()->name
];
});
Fabricate::clear();
Fabricate::config(function($config) {
$config->sequence_start = 1;
$config->adaptor = new Fabricate\Adaptor\CakePHPAdaptor();
$config->faker = \Faker\Factory::create('ja_JP');
});