Mapping an API to entities with the help of Doctrine Annotations

Moritz V.
Moritz V.
Jul 15 · 6 min read
// dumb array / stdclass 😞
$title = $product['ptitle_v2'];
$title = $product->ptitle_v2;
// custom entity 🙂
$title = $product->getTitle();
class Product {

/** @var string */
private $title;
/** @var string */
private $description;
// ... public static function createFromApiResponse(array $data): ?self
{
$product = new self();
$product->title = $data['ptitle_v2'];
$product->description = $data['pdescription'];
// ...
return $product;
}
}
/**
* @ORM\Entity()
* @ORM\Table()
*/
class Product {

/**
*
@ORM\Column(name="product_title")
*
*
@var string
*/
private $title;
/**
*
@ORM\Column(name="description", nullable=true)
*
*
@var ?string
*/
private $description;
// ...
}

Annotations FTW

/**
* @ORM\Entity()
* @ORM\Table()
*/
class Product
{

/**
*
@ORM\Column(name="title")
* @
MySpecialAPI(name="ptitle_v2")
*
*
@var string
*/
private $title;
/**
*
@ORM\Column(name="description", nullable=true)
* @
MySpecialAPI(name="pdescription")
*
*
@var ?string
*/
private $description;
// ...
}

Now for the plumbing.

use Doctrine\Common\Annotations\Annotation;

/**
*
@Annotation
* @Target({"PROPERTY"})
*/
class MySpecialAPI extends Annotation
{
/** @var string */
public $name;
}
public function boot(): void
{
// load custom annotations
AnnotationRegistry::registerLoader('class_exists');
}
public static function createFromApiResponse(array $data): ?self
{
$product = new self();
self::autoMap($product, $data);
return $product;
}
trait MySpecialApiMapperTrait
{
protected static function autoMap($object, array $apiData): void
{
$reader = new AnnotationReader();
$reflectionObject = new \ReflectionObject($object);

foreach ($reflectionObject->getProperties() as $reflectionProperty)
{
/** @var MySpecialAPI $annotationData */
$annotationData = $reader->getPropertyAnnotation(
$reflectionProperty,
MySpecialAPI::class
);
if (null === $annotationData) {
continue;
}
if (!\array_key_exists($annotationData->name, $apiData)) {
continue;
}

$propertyName = $reflectionProperty->getName();
$object->$propertyName = $apiData[$annotationData->name];
}
}
$reader = new AnnotationReader();
$reflectionObject = new \ReflectionObject($object);
/** @var MySpecialAPI $annotationData */
$annotationData = $reader->getPropertyAnnotation(
$reflectionProperty,
MySpecialAPI::class
);
if (!\array_key_exists($annotationData->name, $apiData)) {
continue;
}

$propertyName = $reflectionProperty->getName();
$object->$propertyName = $apiData[$annotationData->name];

But, what if my values are more complex?

/**
*
@MySpecialAPI(name="batteries", enum={
* "YES" = true,
* "NO" = false,
* "NOT_APPLICABLE" = null
* })
*
*
@var ?bool
*/
private $includesBatteries;
public const STATUS_INACTIVE = 0;
public const STATUS_ACTIVE = 1;
public const STATUS_GONE = 2;
/**
*
@MySpecialAPI(name="status", enum={
* "INACTIVE" = Product::STATUS_INACTIVE,
* "ACTIVE" = Product::
STATUS_ACTIVE,
* "GONE" = Product::STATUS_GONE
* })
*
*
@var ?int
*/
private $status = self::STATUS_INACTIVE;
/**
*
@MySpecialAPI(name="@id", mandatory=true)
*
*
@var int
*/
private $id;
/**
*
@MySpecialAPI(name="attachments.media.image")
*
*
@var ?string
*/
private $imageUrl;
// API data expected to look like this:
// $data['attachments']['media']['image'] --> 'http://...'

Conclusion

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade