1. Go to this page and download the library: Download radebatz/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/ */
radebatz / object-mapper example snippets
use Radebatz\ObjectMapper\ObjectMapper;
class MyClass
{
protected $foo;
public function getFoo()
{
return $this->foo;
}
public function setFoo($foo)
{
$this->foo = $foo;
}
}
$objectMapper = new ObjectMapper();
$json = '{"foo":"bar"}';
/** @var \MyClass $obj */
$obj = $objectMapper->map($json, \MyClass::class);
echo $obj->getFoo(); // 'bar'
use Radebatz\ObjectMapper\ObjectMapper;
use Radebatz\ObjectMapper\ValueTypeResolverInterface;
interface ValueInterface
{
public function getType(): string;
}
class Tree implements ValueInterface
{
public function getType(): string
{
return "tree";
}
}
class Flower implements ValueInterface
{
public function getType(): string
{
return "flower";
}
}
$objectMapper = new ObjectMapper();
$objectMapper->addValueTypeResolver(
new class() implements ValueTypeResolverInterface {
public function resolve($className, $json): ?string
{
if (is_object($json) && \ValueInterface::class == $className) {
if (property_exists($json, 'type')) {
switch ($json->type) {
case 'tree':
return \Tree::class;
case 'flower':
return \Flower::class;
}
}
}
return null;
}
}
);
$json = '{"type": "flower"}';
/** @var \ValueInterface $obj */
$obj = $objectMapper->map($json, \ValueInterface::class);
echo get_class($obj); // '\Flower'
function ($obj, $key, $value) {}
class Model {
private $list = [];
public function setList(ListModel ... $listModels) {
$this->list = $listModels;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.