Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.51 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Person\Model;
  4.  
  5. use Core\Model\CommonEntity;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Nette\Security\IIdentity;
  9. use Nette\Security\Passwords;
  10. use Nette\Utils\Validators;
  11.  
  12. /**
  13.  * Class Person
  14.  *
  15.  * @ORM\Entity
  16.  * @ORM\Table(name="pe_person")
  17.  *
  18.  * @author Petr Blazicek 2018
  19.  */
  20. class Person extends CommonEntity implements IIdentity
  21. {
  22.  
  23.     const SEX_MALE = 1;
  24.     const SEX_FEMALE = 2;
  25.     const HASH_LENGTH = 40;
  26.  
  27.     /**
  28.      * @ORM\Column(type="string", length=32, nullable=false)
  29.      *
  30.      * @var string
  31.      */
  32.     private $name;
  33.  
  34.     /**
  35.      * @ORM\Column(type="string", length=32, nullable=false)
  36.      *
  37.      * @var string
  38.      */
  39.     private $surname;
  40.  
  41.     /**
  42.      * @ORM\Column(type="smallint", nullable=true)
  43.      *
  44.      * @var int
  45.      */
  46.     private $gender;
  47.  
  48.     /**
  49.      * @ORM\Column(type="string", length=64, unique=true, nullable=false)
  50.      *
  51.      * @var string
  52.      */
  53.     private $email;
  54.  
  55.     /**
  56.      * @ORM\Column(type="string", length=40, nullable=false)
  57.      *
  58.      * @var string
  59.      */
  60.     private $password;
  61.  
  62.     /**
  63.      * @Column(type="string", length=128, nullable=true)
  64.      * @var string
  65.      */
  66.     private $roles;
  67.  
  68.     /**
  69.      * Groups membership
  70.      *
  71.      * @ORM\ManyToMany(targetEntity="Group", inversedBy="members")
  72.      * @ORM\JoinTable(name="pe_members_groups")
  73.      *
  74.      * @var Group|ArrayCollection
  75.      */
  76.     private $groups;
  77.  
  78.     /**
  79.      * Person constructor
  80.      */
  81.     public function __construct()
  82.     {
  83.         $this->groups = new ArrayCollection();
  84.     }
  85.  
  86.     // <editor-fold defaultstate="collapsed" desc="getters & setters">
  87.     /**
  88.      * Name getter
  89.      *
  90.      * @return string
  91.      */
  92.     public function getName()
  93.     {
  94.         return $this->name;
  95.     }
  96.  
  97.     /**
  98.      * Name setter
  99.      *
  100.      * @param string $name
  101.      * @return self (fluent interface)
  102.      */
  103.     public function setName( $name )
  104.     {
  105.         Validators::assert( $name, 'string:2..32', 'name' );
  106.         $this->name = $name;
  107.         return $this;
  108.     }
  109.  
  110.     /**
  111.      * Surname getter
  112.      *
  113.      * @return string
  114.      */
  115.     public function getSurname()
  116.     {
  117.         return $this->surname;
  118.     }
  119.  
  120.     /**
  121.      * Surname setter
  122.      *
  123.      * @param string $surname
  124.      * @return self (fluent interface)
  125.      */
  126.     public function setSurname( $surname )
  127.     {
  128.         Validators::assert( $surname, 'string:2..32', 'surname' );
  129.         $this->surname = $surname;
  130.         return $this;
  131.     }
  132.  
  133.     /**
  134.      * Gender getter
  135.      *
  136.      * @return int
  137.      */
  138.     public function getGender()
  139.     {
  140.         return $this->gender;
  141.     }
  142.  
  143.     /**
  144.      * Gender setter
  145.      *
  146.      * @param int $gender
  147.      * @return self (fluent interface)
  148.      */
  149.     public function setGender( $gender )
  150.     {
  151.         Validators::assert( $gender, 'int:1..2', 'gender' );
  152.         $this->gender = $gender;
  153.         return $this;
  154.     }
  155.  
  156.     /**
  157.      * Email getter
  158.      *
  159.      * @return string
  160.      */
  161.     public function getEmail()
  162.     {
  163.         return $this->email;
  164.     }
  165.  
  166.     /**
  167.      * Email setter
  168.      *
  169.      * @param string $email
  170.      * @return self (fluent interface)
  171.      */
  172.     public function setEmail( $email )
  173.     {
  174.         Validators::assert( $email, 'email', 'email' );
  175.         $this->email = $email;
  176.         return $this;
  177.     }
  178.  
  179.     /**
  180.      * Password getter
  181.      *
  182.      * @return string
  183.      */
  184.     public function getPassword()
  185.     {
  186.         return $this->password;
  187.     }
  188.  
  189.     /**
  190.      * Password setter
  191.      *
  192.      * @param string $password
  193.      * @return self (fluent interface)
  194.      */
  195.     public function setPassword( $password )
  196.     {
  197.         Validators::assert( $password, 'string:6..12', 'password' );
  198.         $this->password = Passwords::hash( $password );
  199.         return $this;
  200.     }
  201.  
  202.     /**
  203.      * Roles getter
  204.      *
  205.      * @return string
  206.      */
  207.     public function getRoles()
  208.     {
  209.         return $this->roles;
  210.     }
  211.  
  212.     /**
  213.      * Roles setter
  214.      *
  215.      * @param string $roles
  216.      * @return self (fluent interface)
  217.      */
  218.     public function setRoles( $roles )
  219.     {
  220.         Validators::assert( $roles, 'string', 'roles' );
  221.         $this->roles = $roles;
  222.         return $this;
  223.     }
  224.  
  225.     /**
  226.      * Groups getter
  227.      *
  228.      * @param bool $idList
  229.      * @return Group[]|int[]
  230.      */
  231.     public function getGroups( $idList = FALSE )
  232.     {
  233.         return $idList ? $this->groups->getKeys() : $this->groups->toArray();
  234.     }
  235.  
  236.     /**
  237.      * Group adder
  238.      *
  239.      * @param Group $group
  240.      * @return self (fluent interface)
  241.      */
  242.     public function addGroup( Group $group )
  243.     {
  244.         if ( !$this->groups->elementExists( $group ) ) {
  245.             $this->groups->add( $group );
  246.             $group->addMember( $this );
  247.         }
  248.         return $this;
  249.     }
  250.  
  251.     /**
  252.      * Group remover
  253.      *
  254.      * @param mixed $group
  255.      * @return self (fluent interface)
  256.      */
  257.     public function removeGroup( $group )
  258.     {
  259.         if ( $group instanceof Group ) $this->groups->removeElement( $group );
  260.         else $this->groups->remove( $group );
  261.         $group->removeMember( $this );
  262.         return $this;
  263.     }
  264.  
  265.     /**
  266.      * Entity data getter
  267.      *
  268.      * @return []
  269.      */
  270.     public function provide()
  271.     {
  272.         return parent::provide() + [
  273.             'name' => $this->getName(),
  274.             'surname' => $this->getSurname(),
  275.             'gender' => $this->getGender(),
  276.             'email' => $this->getEmail(),
  277.             'roles' => $this->getRoles(),
  278.             'groups' => $this->getGroups(TRUE),
  279.         ];
  280.     }
  281.  
  282.     /**
  283.      * Entity data setter
  284.      *
  285.      * @param ArrayHash|array $data
  286.      * @return self (fluent interface)
  287.      */
  288.     public function load( $data )
  289.     {
  290.         parent::load( $data );
  291.         if ( isset( $data[ 'name' ] ) ) $this->setName( $data[ 'name' ] );
  292.         if ( isset( $data[ 'surname' ] ) ) $this->setSurname( $data[ 'surname' ] );
  293.         if ( isset( $data[ 'gender' ] ) ) $this->setGender( $data[ 'gender' ] );
  294.         if ( isset( $data[ 'email' ] ) ) $this->setEmail( $data[ 'email' ] );
  295.         if ( isset( $data[ 'password' ] ) ) $this->setPassword( $data[ 'password' ] );
  296.         if ( isset( $data[ 'roles' ] ) ) $this->setRoles( $data[ 'roles' ] );
  297.         return $this;
  298.     }
  299.  
  300.     // </editor-fold>
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement