Get support for mnapoli/Transform
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
mnapoli/Transform
Transform
Work in progress
Transform data from a format to another, e.g. models, arrays, DTOs…
Usage
Using the transformer is pretty simple:
$result = $transformer->transform($from, $to);
$from
can be:
- an object
- an array
$to
can be:
- an object
-
'array'
: a new array will be created - a class name: a new instance of the class will be created
You can transform from and to arrays and objects.
Mapping
Use configuration to define how to map objects to arrays:
$transformer->addMapping([
User::class => [
'fields' => [
'name',
'password',
],
],
]);
$data = $transformer->transform($user, 'array');
Properties
class User
{
public $name;
private $password; // works with private properties too
// Getters and setters will be called if they exist
private $email;
public function getEmail() { ... }
public function setEmail($email) { ... }
}
// ...
User::class => [
'fields' => [
'name',
'password',
'email',
],
],
Getters and setters
You can configure specific methods to be called where reading or setting the value:
class User
{
public function someMethod() { ... }
public function someOtherMethod($value) { ... }
}
// ...
User::class => [
'fields' => [
'name' => [
'get' => 'someMethod()',
'set' => 'someOtherMethod()',
],
],
],
Accessors
You can use PHP callables to define how to set and get a field:
class User
{
private $login;
}
// ...
User::class => [
'fields' => [
'name' => [
'get' => function () {
return $this->login;
},
'set' => function ($value) {
$this->login = $value
},
],
],
],
Closures are rebound to the object: that means $this
represents the object being transformed. You can access private properties and methods just like if you were inside the class.
You can mark a field as read-only or write-only by setting a null
accessor:
User::class => [
'fields' => [
'name' => [
'get' => function () { ... },
'set' => null, // read-only
],
],
],
Field types
You can declare the types of each field using the type
index:
class Product
{
/**
* @var Price
*/
private $price;
}
// ...
Product::class => [
'fields' => [
'price' => [
'type' => Price::class,
],
],
],
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2