Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Add a mapTo(class) function for map ES|QL response into objects #1398

Closed
ezimuel opened this issue Mar 25, 2024 · 1 comment
Closed
Assignees
Milestone

Comments

@ezimuel
Copy link
Contributor

ezimuel commented Mar 25, 2024

This is a proposal for adding a Response\Elasticsearch::mapTo($class = 'stdClass') for mapping an ES|QL response into an object of a class $class.

Here an example:

class Employee
{
    public string $name;
    public DateTime $birthdate;
    public float $avg_salary;
    public string $country;
}

$client = ClientBuilder::create()
    ->build();

$result = $client->esql()->query([
    'body' => [
        'query' => 'FROM employees | STATS avg_salary = AVG(salary) by country'
    ]
]);
$employees = $result->mapTo(Employee::class); // array of Employee

printf("Number of employees: %d\n", count($employees));
printf("First employee\n");
printf("Name: %s\n", $employees[0]->name);
printf("Birthdate: %s\n",  $employees[0]->birthday->format('Y-m-d'));
printf("Avg salary: %.2f\n", $employees[0]->avg_salary);
printf("Country: %s\n", $employees[0]->country);

If $class is not specified the default class is stdClass.

@ezimuel ezimuel added this to the 8.13.0 milestone Mar 25, 2024
@ezimuel ezimuel self-assigned this Mar 25, 2024
ezimuel added a commit that referenced this issue Mar 25, 2024
ezimuel added a commit that referenced this issue Mar 26, 2024
* Added mapTo feature, see #1398

* Fixed php-http/message-factory issue and extended memory to 256M for phpstan
@ezimuel
Copy link
Contributor Author

ezimuel commented Mar 26, 2024

Done in #1399

@ezimuel ezimuel closed this as completed Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant