/** * Index all glossary terms by their first letter. * * @return array */ private function indexEntries() { if ($this->indexedEntries === null) { $alphabet = $this->getAlphabet(); $index = []; $misc = []; foreach ($this->entryRepository->findAllOrderedByTerm()->toArray() as $entry) { $l = strtoupper($entry->getTerm()[0]); // If first letter is not in the alphabet, we put the term in // the '#' category if (!in_array($l, $alphabet)) { $misc[] = $entry; } else { if (!isset($index[$l])) { $index[$l] = []; } $index[$l][] = $entry; } } if (count($misc)) { $index['#'] = $misc; } $this->indexedEntries = $index; } return $this->indexedEntries; }
/** * Return all glossary entries as JSON. * * @return string */ public function indexJsonAction() { $data = []; foreach ($this->entryRepository->findAll()->toArray() as $entry) { $data[] = ['term' => $entry->getTerm(), 'definition' => $entry->getDefinition()]; } return json_encode($data); }