makes = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add a new Make to this Stock Item. * @param \Stock\Entity\Make $make * @return \PriceGuide\Entity\Member */ public function addMake(\Stock\Entity\Make $make) { $this->makes[] = $make; return $this; } /** * Magic getter to expose protected properties. * * @param string $property * @return mixed */ public function __get($property) { return $this->$property; } /** * Magic setter to save protected properties. * * @param string $property * @param mixed $value */ public function __set($property, $value) { $this->$property = $value; } /** * @ORM\PrePersist */ public function setCreateTime() { $this->created = new \DateTime("now"); } /** * @ORM\PreUpdate */ public function setUpdateTime() { $this->updated = new \DateTime("now"); } /** * Convert the object to an array. * @param array $expand * @param array $intersect * @param boolean $showIdentifiers * @param integer $expandAll * @return array */ public function toArray( array $expand = array(), array $intersect = array(), $showIdentifiers = false, $expandAll = 0 ) { $intersect = array_flip($intersect); $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat'); $includeAll = empty($intersect); $data = array(); ($includeAll || isset($intersect['id'])) && $data['id'] = $this->id; ($includeAll || isset($intersect['club'])) && $data['club'] = (in_array('club', $expand) || $expandAll || $showIdentifiers) && !is_null($this->club) ? (!$showIdentifiers ? $this->club->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->club->id) : null; ($includeAll || isset($intersect['company'])) && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers) && !is_null($this->company) ? (!$showIdentifiers || in_array('company', $expand) ? $this->company->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->company->id) : null; ($includeAll || isset($intersect['profile'])) && $data['profile'] = (in_array('profile', $expand) || $expandAll || $showIdentifiers) && !is_null($this->profile) ? (!$showIdentifiers || in_array('profile', $expand) ? $this->profile->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->profile->id) : null; ($includeAll || isset($intersect['allowedMember'])) && $data['allowedMember'] = (in_array('allowedMember', $expand) || $expandAll || $showIdentifiers) && !is_null($this->allowedMember) ? (!$showIdentifiers ? $this->allowedMember->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->allowedMember->id) : null; ($includeAll || isset($intersect['makes'])) && $data['makes'] = (in_array('makes', $expand) || $expandAll || $showIdentifiers) && !is_null($this->makes) ? $this->makesToArray($showIdentifiers) : null; ($includeAll || isset($intersect['allMakes'])) && $data['allMakes'] = $this->allMakes; ($includeAll || isset($intersect['fromYear'])) && $data['fromYear'] = (in_array('fromYear', $expand) || $expandAll || $showIdentifiers) && !is_null($this->fromYear) ? (!$showIdentifiers ? $this->fromYear->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->fromYear->id) : null; ($includeAll || isset($intersect['toYear'])) && $data['toYear'] = (in_array('toYear', $expand) || $expandAll || $showIdentifiers) && !is_null($this->toYear) ? (!$showIdentifiers ? $this->toYear->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->toYear->id) : null; ($includeAll || isset($intersect['fromKm'])) && $data['fromKm'] = $this->fromKm; ($includeAll || isset($intersect['toKm'])) && $data['toKm'] = $this->toKm; ($includeAll || isset($intersect['emailNotification'])) && $data['emailNotification'] = $this->emailNotification; ($includeAll || isset($intersect['smsNotification'])) && $data['smsNotification'] = $this->smsNotification; ($includeAll || isset($intersect['status'])) && $data['status'] = $this->status; ($includeAll || isset($intersect['created'])) && $data['created'] = !is_null($this->created) ? $this->created->format($dateTimeFormat) : null; ($includeAll || isset($intersect['updated'])) && $data['updated'] = !is_null($this->updated) ? $this->updated->format($dateTimeFormat) : null; return $data; } /** * Internal utility to change makes collection into array. * @param boolean $showIdentifiers * @return array */ protected function makesToArray($showIdentifiers = false) { $data = array(); $iterator = $this->makes->getIterator(); if (2 == $showIdentifiers) { foreach ($iterator as $make) { $data[] = $make->make->id; } } else { foreach ($iterator as $make) { $data[] = $showIdentifiers ? array('id' => $make->make->id) : $make->toArray(); } } return $data; } /** * Populate from an array. * @param array $data */ public function fromArray($data = array()) { isset($data['id']) && $this->id = $data['id']; isset($data['club']) && $this->club = $data['club']; isset($data['company']) && $this->company = $data['company']; isset($data['profile']) && $this->profile = $data['profile']; isset($data['allowedMember']) && $this->allowedMember = $data['allowedMember']; isset($data['status']) && $this->status = $data['status']; isset($data['allMakes']) && $this->allMakes = $data['allMakes']; isset($data['fromYear']) ? $this->fromYear = $data['fromYear'] : $this->fromYear = null; isset($data['toYear']) ? $this->toYear = $data['toYear'] : $this->toYear = null; isset($data['fromKm']) ? $this->fromKm = $data['fromKm'] : $this->fromKm = null; isset($data['toKm']) ? $this->toKm = $data['toKm'] : $this->toKm = null; isset($data['emailNotification']) && $this->emailNotification = $data['emailNotification']; isset($data['smsNotification']) && $this->smsNotification = $data['smsNotification']; if (isset($data['applyToAll']) && $data['applyToAll']) { $em = \Utility\Registry::getEntityManager(); $members = $em->getRepository('\PriceGuide\Entity\Member') ->findBy(array('company' => $this->company->id)); foreach ($members as $member) { $iterator = $member->makes->getIterator(); foreach ($iterator as $mk) { $em->remove($mk); } $em->flush(); if (isset($data['allMakes']) && $data['allMakes']) { $member->allMakes = true; } else { $member->allMakes = false; foreach ($data['makes'] as $make) { $memberMake = new MemberMake(); $memberMake->make = $em->getReference('\Stock\Entity\Make', $make['id']); $memberMake->member = $member; $member->makes->add($memberMake); } } isset($data['emailNotification']) ? $member->emailNotification = $data['emailNotification'] : $member->emailNotification = false; isset($data['smsNotification']) ? $member->smsNotification = $data['smsNotification'] : $member->smsNotification = false; isset($data['fromYear']) ? $member->fromYear = $data['fromYear'] : $member->fromYear = null; isset($data['toYear']) ? $member->toYear = $data['toYear'] : $member->toYear = null; isset($data['fromKm']) ? $member->fromKm = $data['fromKm'] : $member->fromKm = null; isset($data['toKm']) ? $member->toKm = $data['toKm'] : $member->toKm = null; $em->flush($member); } } elseif (isset($data['makes'])) { $em = \Utility\Registry::getEntityManager(); $newMakes = array(); foreach ($data['makes'] as $make) { $newMakes[$make['id']] = $make['id']; } $currentMakes = array(); $iterator = $this->makes->getIterator(); foreach ($iterator as $make) { $currentMakes[$make->make->id] = $make->make->id; !isset($newMakes[$make->make->id]) && $em->remove($make); } foreach ($data['makes'] as $make) { if (!isset($currentMakes[$make['id']])) { $memberMake = new MemberMake(); $memberMake->make = $em->getReference('\Stock\Entity\Make', $make['id']); $memberMake->member = $this; $this->makes->add($memberMake); } } } } }