AlternateFormats::alternateFormats()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 9.5555
1
<?php
2
3
namespace mQueue\View\Helper;
4
5
use Zend_Locale;
6
use Zend_Registry;
7
use Zend_View_Helper_Abstract;
8
9
class AlternateFormats extends Zend_View_Helper_Abstract
10
{
11
    protected static $supportedFormats = [
12
        'rss' => [
13
            'name' => 'RSS',
14
        ],
15
        'csv' => [
16
            'name' => 'CSV',
17
        ],
18
    ];
19
20
    /**
21
     * Returns a string of HTML links for end-user, and also append 'alternate' links to HTML header
22
     *
23
     * @param array $formats
24
     * @param string $title
25
     *
26
     * @return string
27
     */
28 4
    public function alternateFormats(array $formats, $title = null)
29
    {
30 4
        $formatLinks = [];
31 4
        foreach ($formats as $format => $url) {
32
            // Inject format and locale parameters
33 4
            if (mb_strpos($url, '?') === false) {
34 3
                $url .= '?';
35
            } else {
36 1
                $url .= '&';
37
            }
38
39 4
            $url .= 'format=' . $format . '&lang=' . Zend_Registry::get(Zend_Locale::class)->getLanguage();
40
41 4
            $formatLinks[] = '<a class="' . $format . '" href="' . $url . '">' . self::$supportedFormats[$format]['name'] . '</a>';
42 4
            if ($title && isset(self::$supportedFormats[$format]['mime'])) {
43
                $this->view->headLink()->appendAlternate($url, self::$supportedFormats[$format]['mime'], $title);
44
            }
45
        }
46
47 4
        $result = '<p class="alternateFormats">' . $this->view->translate('Also available in:') . ' ' . implode(' | ', $formatLinks) . '</p>';
48
49 4
        return $result;
50
    }
51
}
52