library mapper
Simply map JSON structures onto PHP classes Edit
kanel/mapper
Simply map JSON structures onto PHP classes Edit
- Tuesday, May 16, 2017
- by elkaadka
- Repository
- 0 Watchers
- 1 Stars
- 16 Installations
- PHP
- 1 Dependents
- 0 Suggesters
- 0 Forks
- 0 Open issues
- 3 Versions
- 7 % Grown
mapper
Simply map JSON structures onto PHP classes, (*1)
, (*2)
How it works
-
Map an array, an object or a json string into an object :
```
$data = ['testOne' => 1, 'testTwo' => 2, 'testThree' => 3]
$myClass = new MyClass();
$mapper = new Mapper();
$mapper->map($data, $myClass);
print_r($myClass);, (*3)
Where MyClass is:
class MyClass {
protected $testOne;
public $testTwo;
private $testThree;
}
The result is :
Object
(
[testOne] => 1
[testTwo] => 2
[testThree] => 3
)
```, (*4)
- the first parameter ($from) can be either an array, an object or a string
- if the first parameter is a string but isn't a valid json, an InvalidDataException is raised
- if the second parameter isn't an object, an InvalidDataException is raised
- if a property (when $from is an object) or key (when $from is an array) does not exist, it will not be created.
-
To create a property in the target class when it does not exist:, (*5)
$options = new Options();
$options->createNewProperty(true);
$mapper = new Mapper($options);
$mapper->map($from, $myClass);, (*6)
-
Transform properties:, (*7)
-
You can transform array keys or object properties during mapping,, (*8)
-
example :, (*9)
-
Map the array :, (*10)
['test_one' => 1]
to the object :, (*11)
class Test {
protected $testOne;
}
where the array key 'test_one' corresponds to the property $testOne;, (*12)
-
Or Map the object :, (*13)
class A {
protected $testA;
}
to the object :, (*14)
class B {
protected $testB;
}
where the property $testA of the class A corresponds to the property $testB of class B, (*15)
To do so you need to use Transformers :, (*16)
There are 4 built in Transformers :, (*17)
-
CamelCaseTransformer : snake or pascal case to camel case, (*18)
attribute-name => attributeName
attribute_name => attributeName
attribute name => attributeName
Attribute_NAME => attributeName
AttributeName => attributeName
attributeName => attributeName
attribute_firstName => attributeFirstname
-
SnakeCaseTransformer: pascal or camel case to snake case, (*19)
attributeName => attribute_name
attribute => attribute
-
PascalCaseTransformer: snake or camel case to pascal case, (*20)
attribute-name => AttributeName
attribute_name => AttributeName
attribute name => AttributeName
Attribute_NAME => AttributeName
AttributeName => AttributeName
attributeName => AttributeName
attribute_firstName => AttributeFirstname
-
CustomTransformer : allows to add your own transformer, (*21)
You can specify a transformer for all the properties/keys or one for every property/key, (*22)
Examples :, (*23)
-
Apply a CamelCase Transformer to all the properties/keys, (*24)
$options = new Options();
$options->setTransformer(new CamelCaseTransformer());
$mapper = new Mapper($options);
$class = new MyClassExample(); //Contains only one propery named $testOne;
$data = ['test_one' => 'Hellow World'];
$mapper->map($data, $class);
Result :
print_r($class);
Object
(
[testOne] => 'Hellow World'
)
-
Apply a Custom Transformer to all the properties/keys, (*25)
$customTransformer = new CustomTransformer();
$customTransformer->setTransformer(function($property) {
if ($property == 'test_one') {
return 'name';
}
return $property; //very important or else only test_one will be mapped
});
$options = new Options();
$options->setTransformer($customTransformer);
$mapper = new Mapper($options);
$class = new MyClassExample(); //Having two Properties $name and $phone
$data = ['test_one' => 'Jhon doe', 'phone' => 1234];
$mapper->map($data, $class);
Result :
print_r($class);
Object
(
[name] => 'Jhon doe',
[phone] => 1234
)
-
Apply different Transformers, (*26)
$customTransformer = new CustomTransformer();
$customTransformer->setTransformer(function($property) {
return 'name';
});
$options = new Options();
$options->addPropertyTransformer('test_one', $customTransformer);
$options->addPropertyTransformer('phone_number', new CamelCaseTransformer());
$mapper = new Mapper($options);
$class = new MyClassExample(); //Having two Properties $name, $phoneNumber
$data = ['test_one' => 'Jhon doe', 'phone_number' => 1234];
$mapper->map($data, $class);
Result :
print_r($class);
Object
(
[name] => 'Jhon doe',
[phoneNumber] => 1234
)
dev-master
9999999-dev
Simply map JSON structures onto PHP classes Edit
Sources
Download
The Requires
The Development Requires
by
Adil El Kanabi
1.1
1.1.0.0
Simply map JSON structures onto PHP classes Edit
Sources
Download
The Requires
The Development Requires
by
Adil El Kanabi
1.0
1.0.0.0
Simply map JSON structures onto PHP classes Edit
Sources
Download
The Requires
The Development Requires
by
Adil El Kanabi