Skip to main content

Quick Start

Installation

composer require looqey/speca

Example Usage

Let's create a class that extends Data and define some public properties:

use Looqey\Speca\Data;

class User extends Data {
public string $name;
public ?string $surname = null;
public function __construct(
string $name,
public int $age
) {
$this->name = ucfirst($name);
}
}

Now, let's create an object of our class based on data:

$data = [
'name' => 'john',
'surname' => 'Doe',
'age' => '25',
];

$user = User::from($data);
/*
* $user->name: "John"
* $user->surname: "Doe"
* $user->age: 25
*/

Speca automatically casts types to the required format when possible. You decide whether a constructor is needed for the class — if it's present, it will be called with the necessary fields. Public fields that do not require transformation will be filled with the remaining data.