Skip to content

Instantly share code, notes, and snippets.

@agitso
Created August 26, 2013 18:17
Show Gist options
  • Save agitso/6344675 to your computer and use it in GitHub Desktop.
Save agitso/6344675 to your computer and use it in GitHub Desktop.
Example value object.
<?php
namespace Famly\Core\Value;
use Doctrine\ORM\Mapping as ORM;
use TYPO3\Flow\Annotations as Flow;
/**
* @Flow\ValueObject
*/
class Name {
/**
* @var string
* @ORM\Id
*/
protected $persistenceId;
/**
* @var string
* @Flow\Validate(type="NotEmpty")
*/
protected $firstName;
/**
* @var string
*/
protected $middleName;
/**
* @var string
*/
protected $lastName;
/**
* @param string $firstName
* @param string $middleName
* @param string $lastName
*/
public function __construct($firstName, $middleName = '', $lastName = '') {
$this->persistenceId = \TYPO3\Flow\Utility\Algorithms::generateUUID();
$this->firstName = $this->trimAndUppercaseNames($firstName);
$this->middleName = $this->trimAndUppercaseNames($middleName);
$this->lastName = $this->trimAndUppercaseNames($lastName);
}
/**
* @param $fullName
* @return \Famly\Core\Value\Name
*/
public static function createFromFullName($fullName) {
$name = trim($fullName);
$name = explode(' ', $name);
$firstName = array_shift($name);
$lastName = array_pop($name);
$middleName = implode(' ', $name);
return new Name($firstName, $middleName, $lastName);
}
/**
* @param string $name
* @return string
*/
protected function trimAndUppercaseNames($name) {
$name = '' . trim($name);
$name = explode(' ', $name);
foreach ($name as $key => $value) {
$name[$key] = ucfirst($value);
}
return trim(implode(' ', $name));
}
/**
* @return string
*/
public function getFirstName() {
return $this->firstName;
}
/**
* @return string
*/
public function getFirstAndMiddleName() {
return trim(implode(' ', array(
$this->getFirstName(),
$this->getMiddleName()
)));
}
/**
* @return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* @return string
*/
public function getMiddleName() {
return $this->middleName;
}
/**
* @return string
*/
public function getFullName() {
return str_replace(' ', ' ', implode(' ', array(
$this->getFirstName(),
$this->getMiddleName(),
$this->getLastName()
)));
}
/**
* @return Name
*/
public function cloneToNew() {
return new Name($this->getFirstName(), $this->getMiddleName(), $this->getLastName());
}
/**
* @param Name $name
* @return bool
*/
public function equals($name) {
return $this->getFullName() === $name->getFullName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment