Skip to content

Instantly share code, notes, and snippets.

@JanMikes
Last active January 4, 2023 09:23
Show Gist options
  • Save JanMikes/1ceafba566b95536fd2d54442f417ae7 to your computer and use it in GitHub Desktop.
Save JanMikes/1ceafba566b95536fd2d54442f417ae7 to your computer and use it in GitHub Desktop.
Example of simple and stupid value object
doctrine:
types:
- OdbavTo\Infrastructure\Persistence\Doctrine\Types\Person\FirstNameType
Vygenerovat migrations, ktera smaze z commentu typu `(DC2Type:identity_uid)`
<?php declare (strict_types = 1);
namespace OdbavTo\Domain\Model\Person;
use OdbavTo\Domain\Model\SimpleString;
class FirstName extends SimpleString
{
}
<?php declare (strict_types=1);
namespace OdbavTo\Infrastructure\Persistence\Doctrine\Types\Person;
use OdbavTo\Domain\Model\Person\FirstName;
use OdbavTo\Infrastructure\Persistence\Doctrine\Types\StringType;
class FirstNameType extends StringType
{
public function getPropertyClassName(): string
{
return FirstName::class;
}
public function getName()
{
return 'person_first_name';
}
}
<?php declare (strict_types = 1);
namespace OdbavTo\Domain\Model\Person;
use OdbavTo\Domain\Model\ValueObject;
use OdbavTo\Domain\Model\ValueObjectEquals;
use OdbavTo\Domain\Model\ValueObjectToString;
class Name implements ValueObject
{
use ValueObjectToString;
use ValueObjectEquals;
/** @var FirstName */
private $firstName;
/** @var LastName */
private $lastName;
public function __construct(FirstName $firstName, LastName $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function value()
{
return $this->firstName->value() . ' ' . $this->lastName->value();
}
public function firstName(): FirstName
{
return $this->firstName;
}
public function lastName(): LastName
{
return $this->lastName;
}
public function isEmpty(): bool
{
return $this->firstName->isNull() && $this->lastName->isNull();
}
}
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
<embeddable name="OdbavTo\Domain\Model\Person\Name">
<field name="firstName" type="person_first_name" nullable="true"/>
<field name="lastName" type="person_last_name" nullable="true"/>
</embeddable>
</doctrine-mapping>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment