Код: Выделить всё
class SomeDTO
{
public function __construct(
#[SerializedName('some_property')]
private string $someProperty,
#[SerializedName('some_other_property')]
private int $someOtherProperty,
) {
}
public function getSomeProperty(): string
{
return $this->someProperty;
}
public function setSomeProperty(string $someProperty): self
{
$this->someProperty = $someProperty;
return $this;
}
public function getSomeOtherProperty(): int
{
return $this->someOtherProperty;
}
public function setSomeOtherProperty(int $someOtherProperty): self
{
$this->someOtherProperty = $someOtherProperty;
return $this;
}
}
Код: Выделить всё
class ArrDTO
{
/**
* @param SomeDTO[] $arr
*/
public function __construct(
private array $arr
) {
}
/**
* @return SomeDTO[]
*/
public function getArr(): array
{
return $this->arr;
}
/**
* @param SomeDTO[] $arr
* @return $this
*/
public function setArr(array $arr): self
{
$this->arr = $arr;
return $this;
}
}
И у меня есть такой json, который имеет ту же структуру, что и ArrDTO:
Код: Выделить всё
$json = '{"arr":[{"some_property":"str","some_other_property":1},{"some_property":"str2","some_other_property":2}]}';
Этот код
Код: Выделить всё
use Symfony\Component\Serializer\SerializerInterface;
...
$arrDTO = $serializer->deserialize($json, ArrDTO::class, 'json');
Подробнее здесь: https://stackoverflow.com/questions/790 ... f-entities