PHP code example of shureban / laravel-object-mapper
1. Go to this page and download the library: Download shureban/laravel-object-mapper 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/ */
class User extends MappableObject
{
public int $id;
}
$user1 = (new User())->mapFromJson('{"id": 10}');
$user2 = (new User())->mapFromArray(['id' => 10]);
$user3 = (new User())->mapFromRequest($formRequest);
class User
{
use MappableTrait;
public int $id;
}
$user1 = (new User())->mapFromJson('{"id": 10}');
$user2 = (new User())->mapFromArray(['id' => 10]);
$user3 = (new User())->mapFromRequest($formRequest);
class User {
public int $id;
}
$user1 = (new ObjectMapper(new User()))->mapFromJson('{"id": 10}');
$user2 = (new ObjectMapper(new User()))->mapFromArray(['id' => 10]);
$user3 = (new ObjectMapper(new User()))->mapFromRequest($formRequest);
class User
{
public int $id;
public function __construct(int $id) {
$this->id = $id;
}
}
class User
{
public int $id;
public string $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
}
class User
{
/**
* @var int
*/
public int $id;
/**
* @var DateTime
*/
public $dateOfBirthday;
/**
* @var Address[]
*/
public array $addresses;
}
class User
{
public string $id;
public DateTime $dateOfBirthday;
public function setId(int $id, array $rawData = []): void
{
$this->id = Hash::make($id);
}
public function setDateOfBirthday(string $dateOfBirthday, array $rawData = []): void
{
$this->dateOfBirthday = new DateTime($dateOfBirthday);
}
}
$user = (new ObjectMapper(new User()))->mapFromArray(['id' => 10, 'dateOfBirthday' => '1991-01-01']);
echo $user->id; // $2y$10$XqHrk0oXa7.9AihthdVxW.dd637zj9EhlTJX0eUEKiV61dbs7a7ZO
echo $user->dateOfBirthday->format('Y'); // 1991
class User
{
public readonly int $id;
}
$user = (new ObjectMapper(new User()))->mapFromArray(['id' => 10]);
echo $user->id; // 0