ludofleury
7/20/2012 - 10:13 AM

User.php

<?php

namespace Retentio\Mapper;

use Retentio\Document;
use Boomgo\Mapper\BaseMapper,
    Boomgo\Mapper\MapperInterface;

/**
 * UserMapper
 *
 * Auto generated Mapper class for/by Boomgo
 * Do not edit this file
 *
 * @author  Boomgo\Generator\MapperGenerator
 */
class UserMapper extends BaseMapper implements MapperInterface
{
    /**
     * Unserialize a MongoDB array to an object
     *
     * Return an hydrated object from a MongoDB array
     *
     * @param  array  $data MongoDB result array (a document)
     *
     * @return \Retentio\Document\User
     */
    public function unserialize(array $data)
    {
        $object = new \Retentio\Document\User();
        $this->hydrate($object, $data);

        return $object;
    }

    /**
     * Serialize an object to a MongoDB array
     *
     * Return an Mongo-able array from a Php object
     *
     * @param  \Retentio\Document\User $object
     *
     * @return array
     */
    public function serialize($object)
    {
        if (!$object instanceof \Retentio\Document\User) {
            throw new \InvalidArgumentException('Serialize expect an instance of "\Retentio\Document\User"');
        }

        $data = array();
        
        $value = $object->getId();
        if (null != $value && !empty($value)) {
            $data['_id'] = $value;
        }
        
        $value = $object->getEmail();
        if (null != $value && !empty($value)) {
            $data['email'] = $value;
        }
        
        $value = $object->getPassword();
        if (null != $value && !empty($value)) {
            $data['password'] = $value;
        }
        
        $value = $object->getSalt();
        if (null != $value && !empty($value)) {
            $data['salt'] = $value;
        }
        
        $value = $object->getConfirmed();
        if (null != $value && !empty($value)) {
            $data['confirmed'] = $value;
        }
        
        $value = $object->getLocked();
        if (null != $value && !empty($value)) {
            $data['locked'] = $value;
        }
        
        $value = $object->getRoles();
        if (null != $value && !empty($value)) {
            $data['roles'] = $this->normalize($value);
        }
        
        $value = $object->getPasswordRequest();
        if (null != $value && !empty($value)) {
            $mapper = $this->mapperProvider->get('\Retentio\Document\PasswordRequest');
            $data['passwordRequest'] = $mapper->serialize($value);
        }
        
        $value = $object->getCreatedAt();
        if (null != $value && !empty($value)) {
            $data['createdAt'] = $value;
        }
        
        return $data;
    }

    /**
     * Hydrate an object from a MongoDb array
     *
     * @param  \Retentio\Document\User $object
     * @param  array $data MongoDB result array (a document)
     */
    public function hydrate($object, array $data)
    {
        if (!$object instanceof \Retentio\Document\User) {
            throw new \InvalidArgumentException('Serialize expect an instance of "\Retentio\Document\User"');
        }
        
        if (isset($data['_id'])) {
            $object->setId($data['_id']);
        }
        
        if (isset($data['email'])) {
            $object->setEmail($data['email']);
        }
        
        if (isset($data['password'])) {
            $object->setPassword($data['password']);
        }
        
        if (isset($data['salt'])) {
            $object->setSalt($data['salt']);
        }
        
        if (isset($data['confirmed'])) {
            $object->setConfirmed($data['confirmed']);
        }
        
        if (isset($data['locked'])) {
            $object->setLocked($data['locked']);
        }
        
        if (isset($data['roles'])) {
            $object->setRoles($data['roles']);
        }
        
        if (isset($data['passwordRequest'])) {
            $mapper = $this->mapperProvider->get('\Retentio\Document\PasswordRequest');
            $embeddedObject = $mapper->unserialize($data['passwordRequest']);
            $object->setPasswordRequest($embeddedObject);
        }
        
        if (isset($data['createdAt'])) {
            $object->setCreatedAt($data['createdAt']);
        }
        
    }
}
<?php

namespace Retentio\Document;

use Retentio\Document\PasswordRequest;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * User
 *
 * @author Antoine Guiral
 * @author Ludovic Fleury <ludo.fleury@gmail.com>
 */
class User
{
    /**
     * @Persistent
     * @var \MongoId
     */
    private $id;

    /**
     * @Persistent
     * @var string
     */
    private $email;

    /**
     * @Persistent
     * @var string
     */
    private $password;

    /**
     * @Persistent
     * @var string
     */
    private $salt;

    /**
     * @Persistent
     * @var boolean
     */
    private $confirmed;

    /**
     * @Persistent
     * @var boolean
     */
    private $locked;

    /**
     * @Persistent
     * @var array
     */
    private $roles;

    /**
     * @Persistent
     * @var Retentio\Document\PasswordRequest
     */
    private $passwordRequest;

    /**
     * @Persistent
     * @var string
     */
    private $createdAt;

    public function __construct()
    {
        $this->id = new \MongoId();
        $this->enabled = false;
        $this->locked = false;
        $this->salt = \Boomstone\Utils\Toolbox::generateToken();
        $this->roles = array('ROLE_MEMBER');
        $this->createdAt = time();
    }

    public function __toString()
    {
        return $this->email;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function getSalt()
    {
        return $this->salt;
    }

    public function setSalt($salt)
    {
        $this->salt = $salt;
    }

    public function getConfirmed()
    {
        return $this->confirmed;
    }

    public function setConfirmed($confirmed)
    {
        $this->confirmed = $confirmed;
    }

    public function isConfirmed($confirmed)
    {
        return $this->$confirmed;
    }

    public function getLocked()
    {
        return $this->locked;
    }

    public function setLocked($locked)
    {
        $this->locked = $locked;
    }

    public function isLocked()
    {
        return $this->locked();
    }

    public function getPasswordRequest()
    {
        return $this->passwordRequest;
    }

    public function setPasswordRequest(PasswordRequest $passwordRequest)
    {
        $this->passwordRequest = $passwordRequest;
    }

    /**
     * Reset a password request
     *
     * @param  string $password Optional new encoded password
     */
    public function resetPasswordRequest($password = null)
    {
        if (null !== $password) {
            $this->password = $password;
        }
        $this->passwordRequest = null;
    }

    public function getRoles()
    {
        return $this->roles;
    }

    public function setRoles(array $roles)
    {
        $this->roles = $roles;
    }

    public function hasRole($role)
    {
        return in_array($role, $this->roles);
    }

    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    public function setCreatedAt($time)
    {
        $this->createdAt = $time;
    }
}