1. Go to this page and download the library: Download gmulti/morphism-php 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/ */
gmulti / morphism-php example snippets
use Morphism\Morphism;
// Target type you want to have
class User {
public function __construct($firstName, $lastName, $phoneNumber){
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->phoneNumber = $phoneNumber;
$this->city = null;
}
}
// Data source you want to map
$data = array(
"name" => "Iron Man",
"firstName" => "Tony",
"lastName" => "Stark",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "212 555-1234"
),
array(
"type" => "mobile",
"number" => "646 555-4567"
)
)
);
// Mapping Schema ( see more examples below )
$schema = array(
"city" => "address.city",
"name" => function($data){
return strtoupper($data["name"]);
}
);
Morphism::setMapper("User", $schema);
// Map using the registered type and the registry
$result = Morphism::map("User", $data);
/// *** OUTPUT *** ///
class User {
public $city // string(13) "New York City"
public $name // string(8) "iron man"
}
// Target type you want to have
class User {
}
// Data source you want to map
$data = array(
array(
"name" => "Iron Man",
"firstName" => "Tony",
"lastName" => "Stark",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "212 555-1234"
),
array(
"type" => "mobile",
"number" => "646 555-4567"
)
)
),
array(
"name" => "Spiderman",
"firstName" => "Peter",
"lastName" => "Parker",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "999 999-9999"
)
)
)
);
// Mapping Schema ( see more examples below )
$schema = array(
"city" => "address.city",
"name" => function($data){
return strtoupper($data["name"]);
}
);
Morphism::setMapper("User", $schema);
// Map using the registered type and the registry
$result = Morphism::map("User", $data);
/// *** OUTPUT *** ///
array(
class User {
public $city // string(13) "New York City"
public $name // string(8) "iron man"
},
class User {
public $city // string(13) "New York City"
public $name // string(8) "spiderman"
}
)
$data = array(
"name" => "Iron Man",
"firstName" => "Tony",
"lastName" => "Stark",
"address" => array(
"city" => "New York City",
"country" => "USA"
),
"phoneNumber" => array(
array(
"type" => "home",
"number" => "212 555-1234"
),
array(
"type" => "mobile",
"number" => "646 555-4567"
)
)
);
// Target type you want to have
class User {
}
// Schema
$schema = array(
"fullName" => array("firstName", "lastName")
);
Morphism::setMapper("User", $schema);
// Map using the registered type and the registry
$result = Morphism::map("User", $data);
/// *** OUTPUT *** ///
class User {
public $fullName // "Tony Stark"
}
// Schema
$schema = array(
"city" => (object) array(
"path" => "address.city",
"fn" => function($city) {
return strtolower($city);
}
),
"nbContacts" => function($data){
return count($data["phoneNumber"]);
}
);
Morphism::setMapper("User", $schema);
// Map using the registered type and the registry
$result = Morphism::map("User", $data);
/// *** OUTPUT *** ///
class User {
public $city // "new york city" <= strtolower
public $nbContacts // 2 <= computed from the object
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.