1 | <?php |
||
12 | class XMLRepository implements SourceInterface |
||
13 | { |
||
14 | private $config; |
||
15 | |||
16 | 78 | public function setConfig(Config $config) |
|
20 | |||
21 | 51 | public function surah($surah = null) |
|
55 | |||
56 | 48 | public function ayah($surah, $ayah, $translation = 'ar') |
|
57 | { |
||
58 | 48 | $xmlFile = $this->config->get('storage_path').'/'.$translation.'.xml'; |
|
59 | |||
60 | // If files not exist, get the first match |
||
61 | 48 | if (!file_exists($xmlFile)) { |
|
62 | 45 | $xmlFile = $this->firstMatchAvailableTranslation($translation); |
|
63 | 30 | } |
|
64 | |||
65 | 48 | if ($xmlFile === false) { |
|
66 | 3 | throw new TranslationNotExists('Translation '.$translation." didn't exists. Please check your config."); |
|
67 | } |
||
68 | |||
69 | 45 | $xml = new XML($xmlFile); |
|
70 | 45 | $result = []; |
|
71 | |||
72 | 45 | $max_ayah = intval($this->surah($surah)['ayas']); |
|
73 | $xpath = '//sura[@index='.$surah.']/aya['.implode(' or ', array_map(function ($a) use ($max_ayah) { |
||
74 | 42 | if ($a > $max_ayah) { |
|
75 | 3 | throw new AyahInvalid(); |
|
76 | } |
||
77 | |||
78 | 39 | return '@index='.$a; |
|
79 | 42 | }, $ayah)).']'; |
|
80 | |||
81 | 39 | $xpathResult = $xml->find($xpath); |
|
82 | |||
83 | 39 | while (list(, $node) = each($xpathResult)) { |
|
84 | 39 | $node = (array) $node; |
|
85 | 39 | $verse = current($node); |
|
86 | 39 | $result[$verse['index']] = $verse['text']; |
|
87 | 26 | } |
|
88 | |||
89 | 39 | return $result; |
|
90 | } |
||
91 | |||
92 | 78 | public function initialize() |
|
120 | |||
121 | /** |
||
122 | * Get the first match xml from translation. |
||
123 | * |
||
124 | * @param string $translation Translation prefix or short form |
||
125 | * |
||
126 | * @return string|false String of path to the translation if exists, or false otherwise |
||
127 | */ |
||
128 | 45 | private function firstMatchAvailableTranslation($translation) |
|
146 | |||
147 | |||
148 | public function translations() |
||
159 | |||
160 | public function addTranslation($translation) |
||
164 | } |
||
165 |