/**
  * 
  * @param ReferenceInterface $reference
  * @return string
  */
 protected function formatAuthors($reference)
 {
     $authors = ApaPersonFormatter::formatPersons($reference->getAuthor(), 'author');
     if ($reference->getYear()) {
         $authors .= sprintf(' (<span itemprop="datePublished">%s</span>)', $reference->getYear());
     }
     return $authors;
 }
 /**
  * @param ReferenceInterface $reference
  */
 public function format($reference)
 {
     $parts = [];
     $parts[] = $this->formatAuthors($reference);
     $parts[] = sprintf('<span itemprop="name">%s</span>', $reference->getTitle());
     $pages = sprintf('(%s <span itemprop="pagination">%s</span>)', $this->labels['p'], $reference->getPages());
     $eds = ApaPersonFormatter::parse($reference->getEditor());
     $ed = $eds > 1 ? $this->labels['eds'] : $this->labels['ed'];
     $book = sprintf('%s %s (%s)', ucwords($this->labels['in']), ApaPersonFormatter::formatPersons($reference->getEditor(), 'editor'), $ed);
     $book .= sprintf(', <cite itemprop="name">%s</cite>', $reference->getBooktitle());
     $parts[] = sprintf('<span itemprop="isPartOf" itemscope itemtype="http://schema.org/Book">%s</span> %s', $book, $pages);
     $parts[] = $this->formatPublisher($reference);
     return sprintf('<span itemprop="citation" itemscope itemtype="http://schema.org/Article">%s</span>.', implode('. ', $parts));
 }
 public function sort($references)
 {
     usort($references, function ($a, $b) {
         $personsA = ApaPersonFormatter::parse($a->getAuthor());
         $personsB = ApaPersonFormatter::parse($b->getAuthor());
         $personsACount = count($personsA);
         $personsBCount = count($personsB);
         if ($personsACount == $personsBCount) {
             $amountCmp = 0;
         } else {
             $amountCmp = $personsACount > $personsBCount ? 1 : -1;
         }
         $getName = function ($person) {
             if (isset($person['name'])) {
                 return $person['name'];
             }
             return $person['family'];
         };
         $getSecondName = function ($person) {
             if (isset($person['name'])) {
                 return $person['name'];
             }
             return $person['given'];
         };
         $cmp = 0;
         $i = 0;
         do {
             $nameA = $getName($personsA[$i]);
             $nameB = $getName($personsB[$i]);
             $cmp = strnatcasecmp($nameA, $nameB);
             if ($cmp == 0) {
                 $nameA = $getSecondName($personsA[$i]);
                 $nameB = $getSecondName($personsB[$i]);
                 $cmp = strnatcasecmp($nameA, $nameB);
                 if ($cmp == 0) {
                     $cmp = $amountCmp;
                 }
             }
             $i++;
         } while ($cmp == 0 && $i < $personsACount && $i < $personsBCount);
         return $cmp;
     });
     return $references;
 }