PHP code example of yaknet / mock-engine

1. Go to this page and download the library: Download yaknet/mock-engine 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/ */

    

yaknet / mock-engine example snippets


use YakNet\MockEngine\QueryBuilder;

$users = [
    ['id' => 1, 'name' => 'John Doe', 'role' => 'admin', 'profile' => ['city' => 'Istanbul']],
    ['id' => 2, 'name' => 'Jane Smith', 'role' => 'user', 'profile' => ['city' => 'Ankara']],
    ['id' => 3, 'name' => 'Bob Johnson', 'role' => 'user', 'profile' => ['city' => 'Izmir']],
];

// Query active admins in Istanbul
$results = QueryBuilder::from($users)
    ->select('id', 'name', 'profile.city as city')
    ->where('role', '=', 'user')
    ->where('profile.city', '!=', 'Ankara')
    ->get();

// Returns a Collection containing:
// [ ['id' => 3, 'name' => 'Bob Johnson', 'city' => 'Izmir'] ]

$results = QueryBuilder::from($users)
    ->where('status', '=', 'active')
    ->where(function (QueryBuilder $query) {
        $query->where('role', '=', 'admin')
              ->orWhere('profile.verified', '=', true);
    })
    ->get();

$posts = [
    ['id' => 101, 'user_id' => 1, 'title' => 'First Post'],
    ['id' => 102, 'user_id' => 1, 'title' => 'Second Post'],
    ['id' => 103, 'user_id' => 2, 'title' => 'Hello World'],
];

// Perform INNER JOIN
$results = QueryBuilder::from($users)
    ->select('name', 'title')
    ->join($posts, 'id', '=', 'user_id')
    ->get();

$grouped = QueryBuilder::from($users)
    ->groupBy('profile.city')
    ->get();

// Get the average age of users in Istanbul
$istanbulUsers = $grouped['Istanbul']; // This is a Collection!
$averageAge = $istanbulUsers->avg('age');
$maxAge = $istanbulUsers->max('age');
$names = $istanbulUsers->pluck('name'); // ['John Doe', ...]