<?php
/**
* Contains the definition of the Person class.
*
* @category Lisantra
* @package Person
* @author Michael Gatto <mgatto@lisantra.com>
* @copyright
* @license
*/
/* Autoloading usually takes care of this, but let's be explicit here */
require_once 'PersonInterface.php';
/**
* Describes a person and their roles and attributes.
*
* This class is may be constructed by the factory: 'PersonFactory'
* to present a single interface to constructing it. This let's me
* someday change the constructor signature or otherwise refactror
* while only having to change the factory class and not edit each
* and every place.
*
* @category Lisantra
* @package Person
* @author Michael Gatto <mgatto@lisantra.com>
*/
class Person implements PersonInterface
{
/**
* The person's id.
*
* @var string in md5 format.
*/
private $_id;
/**
* The person's name.
*
* @var string
*/
private $_name;
/**
* The person's email.
*
* @var string
*/
private $_email;
/**
* Constructs a person object.
*
* We may make it a 'protected' function so that only
* concrete subclasses of a type of person can construct
* this Person object.
*
* @param string $id
* @see $_id
*/
public function __construct( $id )
{
$this->setId( $id );
$this->populateById( $this->getId() );
}
/**
* Populate this Person object with data per its id in the RDBMS.
*
* @param string $id
* @see $_id
*/
public function populateById( $id )
{
/* use the Doctrine ORM for interacting with the RDBMS */
try {
$person = Doctrine::getTable( 'People' )
->findOneById( $id )
->toArray();
}
catch ( Doctrine_Query_Exception $e ) {
throw new PersonException(
'Populating this Person with Doctrine failed: ' .
$e->getMessage()
);
}
$this->setName( $person['name'] );
$this->setEmail( $person['email'] );
/* make this a chainable method */
return $this;
}
/**
* Sets the person's id. This is a chainable method.
* <code> $p->setId('d41d8cd98f00b204e9800998ecf8427e')->getId();</code>
*
* @param string $id
* @see $_id
*/
public function setId( $id )
{
/* all processing will stop upon throwing an exception. */
if ( empty( $id ) ) {
throw new PersonException(
'Id cannot be empty.'
);
}
if ( ! preg_match( '/^[0-9a-f]{32}$/', $id ) ) {
throw new PersonException(
'Id must be a string in md5 format'
);
}
$this->_id = (string) $id;
/* implement it as a chainable method */
return $this;
}
/**
* Returns the person's id.
*
* @return string
* @see $_id
*/
public function getId()
{
return $this->_id;
}
/**
* Sets the person's name.
*
* @param string $name
* @return Person
* @see $_name
*/
public function setName( $name )
{
/* validate */
if ( empty( $name ) ) {
throw new PersonException(
'Name cannot be empty'
);
}
$this->_name = $name;
/* implement it as a chainable method */
return $this;
}
/**
* Returns the person's name.
*
* @return string
* @see $_name
*/
public function getName()
{
return $this->_name;
}
/**
* Sets the person's email.
*
* @param string $email
* @return Person
* @see $_email
*/
public function setEmail( $email )
{
/* validate */
$is_email = new Zend_Validate_Email( $email );
if ( ! $is_email ) {
throw new PersonException(
'Email was not valid'
);
}
$this->_email = $email;
/* implement it as a chainable method */
return $this;
}
}
?>