Faker parte 3




Embaixo dessa aula vou deixar todo o código fonte criado nessa sequência de aulas sobre o faker, agora é só adaptar para seu projeto e usar a vontade. E qualquer dúvida é só deixar aqui embaixo no campo de dúvidas.

index.php

require '../vendor/autoload.php';
require '../App/Functions/helpers.php';

use App\Classes\FakerFactory;
use App\Models\Users;

$users = new Users;
$usersEncontrados = $users->all();

// FakerFactory::faker(App\Models\Users::class,30);

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Lista de Users</title>
   <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> 
</head>
<body>

    <div class="container-fluid">
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>Nome</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($usersEncontrados as $user): ?>
                <tr>
                    <td><?php echo $user->name;?></td>
                    <td><?php echo $user->email;?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>

</body>
</html>

Classes/FakerFactory.php

namespace App\Classes;

class FakerFactory{

    public static function faker($class,$value){

        if(!class_exists($class)){
            throw new \Exception("Essa classe não existe {$class}");
        }

        $model = new $class;

        $reflection = new \ReflectionClass($model);

        $method = strtolower($reflection->getShortname());

        if(!method_exists($model, $method)){
            throw new \Exception("Esse método não existe {$method}");
        }

        for ($i=1; $i<=$value;$i++) { 
            $model->create($model->$method());
        }
    }

}

Models/Connection.php

namespace App\Models;

use PDO;

class Connection{

    public static function connection(){
       $pdo = new PDO("mysql:host=localhost;dbname=faker;charset=utf8","root","root");
       $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
       return $pdo;  
    }

}

Models/Model.php

namespace App\Models;

use App\Models\Connection;
use App\Traits\Faker;
use App\Traits\FakerFactory;

class Model{

    use FakerFactory,Faker;

    protected $connection;

    public function __construct(){
        $this->connection = Connection::connection();
    }

    public function all(){
       $sql = "select * from {$this->table}";
       $list = $this->connection->prepare($sql);
       $list->execute();
       return $list->fetchAll(); 
    }

}

Models/Posts.php

namespace App\Models;

use App\Models\Model;

class Posts extends Model{
    public $table = 'posts';

    public function create(array $attributes){
        $sql = "insert into {$this->table}(titulo,texto) values(:titulo, :texto)";
        $create = $this->connection->prepare($sql);
        foreach ($attributes as $key => $value) {
            $create->bindValue(':'.$key,$value);
        }
        $create->execute();
    }

}

Models/Users.php

namespace App\Models;

use App\Models\Model;

class Users extends Model{
    public $table = 'users';

    public function create(array $attributes){
        $sql = "insert into {$this->table}(name,email) values(:name, :email)";
        $create = $this->connection->prepare($sql);
        foreach ($attributes as $key => $value) {
            $create->bindValue(':'.$key,$value);
        }
        $create->execute();
    }

}
``

*Traits/FakerFactory.php*
```php
namespace App\Traits;

trait FakerFactory{

    public function faker(){
        return \Faker\Factory::create();
    }

}

Traits/Faker.php

namespace App\Traits;

trait Faker {

    public function users(){
        return [
            'name' => $this->faker()->name,
            'email' => $this->faker()->email
        ];
    }

    public function posts(){
        return [
            'titulo' => $this->faker()->sentence(2),
            'texto' => $this->faker()->sentence(10)
        ];
    }

}