clubs = new \Doctrine\Common\Collections\ArrayCollection(); $this->offers = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add a new ClubStock to this Stock Item. * @param Damage $damage * @return ClubStock */ public function addClubStock(ClubStock $clubStock) { $this->clubs[] = $clubStock; 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->company = \Utility\Registry::resolveCompanyContext($this->company); $this->createdBy = \Utility\Registry::resolveProfileContext($this->createdBy); $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['company'])) && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers) && !is_null($this->company) ? (!$showIdentifiers ? $this->company->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->company->id) : null; ($includeAll || isset($intersect['stock'])) && $data['stock'] = (in_array('stock', $expand) || $expandAll || $showIdentifiers) && !is_null($this->stock) ? (!$showIdentifiers ? $this->stock->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->stock->id) : null; ($includeAll || isset($intersect['clubs'])) && $data['clubs'] = (in_array('clubs', $expand) || $expandAll || $showIdentifiers) && !is_null($this->clubs) ? $this->clubsToArray($showIdentifiers) : null; (($includeAll || isset($intersect['clubs'])) && in_array('club-ids', $expand)) && $data['clubs'] = (in_array('club-ids', $expand) || $expandAll || $showIdentifiers) && !is_null($this->clubs) ? $this->clubsToArray(true) : null; ($includeAll || isset($intersect['offers'])) && $data['offers'] = (in_array('offers', $expand) || $expandAll || $showIdentifiers) && !is_null($this->offers) ? $this->offersToArray($showIdentifiers) : null; ($includeAll || isset($intersect['jobState'])) && $data['jobState'] = $this->jobState; ($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 clubs collection into array. * @param boolean $showIdentifiers * @return array */ protected function clubsToArray($showIdentifiers = false) { $data = array(); $iterator = $this->clubs->getIterator(); foreach ($iterator as $club) { $data[] = $showIdentifiers ? array('id' => $club->id) : $club->toArray(); } return $data; } /** * Internal utility to change stock offers collection into array. * @param boolean $showIdentifiers * @return array */ protected function offersToArray($showIdentifiers = false) { $data = array(); $iterator = $this->offers->getIterator(); foreach ($iterator as $offer) { $data[] = $showIdentifiers ? array('id' => $offer->id) : $offer->toArray(); } return $data; } /** * Populate from an array. * @param array $data */ public function fromArray($data = array()) { isset($data['id']) && $this->id = $data['id']; isset($data['company']) && $this->company = $data['company']; isset($data['valuation']) && $this->valuation = $data['valuation']; isset($data['stock']) && $this->stock = $data['stock']; if (isset($data['clubs'])) { $em = \Utility\Registry::getEntityManager(); foreach ($data['clubs'] as $cl) { if (!is_numeric($cl['id'])) { continue; } $clubStock = new \PriceGuide\Entity\ClubStock(); $clubStock->club = $em->getReference('\PriceGuide\Entity\Club', $cl['id']); if (is_null($clubStock->club)) { continue; } $clubStock->priceGuideStock = $this; $em->persist($clubStock); $this->clubs->add($clubStock); } } } }