Example #1
0
 /**
  *
  * @param \AppBundle\Entity\Card $card
  * @param string $api
  * @return multitype:multitype: string number mixed NULL unknown
  */
 public function getCardInfo($card, $api = false)
 {
     $cardinfo = [];
     $metadata = $this->doctrine->getManager()->getClassMetadata('AppBundle:Card');
     $fieldNames = $metadata->getFieldNames();
     $associationMappings = $metadata->getAssociationMappings();
     foreach ($associationMappings as $fieldName => $associationMapping) {
         if ($associationMapping['isOwningSide']) {
             $getter = str_replace(' ', '', ucwords(str_replace('_', ' ', "get_{$fieldName}")));
             $associationEntity = $card->{$getter}();
             if (!$associationEntity) {
                 continue;
             }
             $cardinfo[$fieldName . '_code'] = $associationEntity->getCode();
             $cardinfo[$fieldName . '_name'] = $associationEntity->getName();
         }
     }
     foreach ($fieldNames as $fieldName) {
         $getter = str_replace(' ', '', ucwords(str_replace('_', ' ', "get_{$fieldName}")));
         $value = $card->{$getter}();
         switch ($metadata->getTypeOfField($fieldName)) {
             case 'datetime':
             case 'date':
                 continue 2;
                 break;
             case 'boolean':
                 $value = (bool) $value;
                 break;
         }
         $fieldName = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', $fieldName)), '_');
         $cardinfo[$fieldName] = $value;
     }
     $cardinfo['url'] = $this->router->generate('cards_zoom', array('card_code' => $card->getCode()), UrlGeneratorInterface::ABSOLUTE_URL);
     $imageurl = $this->assets_helper->getUrl('bundles/cards/' . $card->getCode() . '.png');
     $imagepath = $this->rootDir . '/../web' . preg_replace('/\\?.*/', '', $imageurl);
     if (file_exists($imagepath)) {
         $cardinfo['imagesrc'] = $imageurl;
     } else {
         $cardinfo['imagesrc'] = null;
     }
     // look for another card with the same name to set the label
     $homonyms = $this->doctrine->getRepository('AppBundle:Card')->findBy(['name' => $card->getName()]);
     if (count($homonyms) > 1) {
         $cardinfo['label'] = $card->getName() . ' (' . $card->getPack()->getCode() . ')';
     } else {
         $cardinfo['label'] = $card->getName();
     }
     if ($api) {
         unset($cardinfo['id']);
         $cardinfo['ci'] = $card->getCostIncome();
         $cardinfo['si'] = $card->getStrengthInitiative();
     } else {
         $cardinfo['text'] = $this->replaceSymbols($cardinfo['text']);
         $cardinfo['text'] = $this->addAbbrTags($cardinfo['text']);
         $cardinfo['text'] = $this->splitInParagraphs($cardinfo['text']);
         $cardinfo['flavor'] = $this->replaceSymbols($cardinfo['flavor']);
     }
     return $cardinfo;
 }