1<?php
2
3/**
4 * Class ilBiblTypeFactory
5 *
6 * @author Fabian Schmid <fs@studer-raimann.ch>
7 */
8class ilBiblTypeFactory implements ilBiblTypeFactoryInterface
9{
10
11    /**
12     * @inheritDoc
13     */
14    public function getInstanceForType(int $type) : ilBiblTypeInterface
15    {
16        assert(is_int($type));
17        switch ($type) {
18        case ilBiblTypeFactoryInterface::DATA_TYPE_BIBTEX:
19            return new ilBibTex();
20        case ilBiblTypeFactoryInterface::DATA_TYPE_RIS:
21            return new ilRis();
22        default:
23            throw new ilException("bibliografic type not found");
24        }
25    }
26
27
28    /**
29     * @inheritDoc
30     */
31    public function getInstanceForFileName(string $filename) : ilBiblTypeInterface
32    {
33        //return bib for filetype .bibtex:
34        if (strtolower(substr($filename, -6)) == "bibtex"
35            || strtolower(substr($filename, -3)) == "bib"
36        ) {
37            return $this->getInstanceForType(ilBiblTypeFactoryInterface::DATA_TYPE_BIBTEX);
38        }
39
40        //else return its true filetype
41        return $this->getInstanceForType(ilBiblTypeFactoryInterface::DATA_TYPE_RIS);
42    }
43
44
45    /**
46     * @inheritDoc
47     */
48    public function getInstanceForString(string $string) : ilBiblTypeInterface
49    {
50        switch ($string) {
51        case "bib":
52            return new ilBibTex();
53        case "ris":
54            return new ilRis();
55        default:
56            throw new ilException("bibliografic type not found");
57        }
58    }
59
60
61    /**
62     * @inheritDoc
63     */
64    public function convertFileEndingToDataType(string $file_ending) : int
65    {
66        switch ($file_ending) {
67        case "ris":
68            return ilBiblTypeFactoryInterface::DATA_TYPE_RIS;
69            break;
70        case "bib":
71            return ilBiblTypeFactoryInterface::DATA_TYPE_BIBTEX;
72            break;
73        default:
74            throw new ilException("no data type found for this file ending");
75        }
76    }
77
78
79    /**
80     * @inheritDoc
81     */
82    public function getDataTypeIdentifierByInstance(ilBiblTypeInterface $type_inst) : int
83    {
84        if ($type_inst instanceof ilRis) {
85            return ilBiblTypeFactoryInterface::DATA_TYPE_RIS;
86        } elseif ($type_inst instanceof ilBibTex) {
87            return ilBiblTypeFactoryInterface::DATA_TYPE_BIBTEX;
88        }
89    }
90}
91