Namaz Saati Class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * This class will get the times of pray of the current day
  4.  * All times are correct because we get them from Diyanet.gov.tr
  5.  *
  6.  * @author Serkan Yildiz <[email protected]>
  7.  * @version 1.0
  8.  * @copyright Serkan Yildiz (freelancer), 3 January, 2011
  9.  * @example
  10. * try{
  11. * $namaz = new NamazSaati('COUNTRY_NAME', 'CITY_NAME') // turkish names
  12. * }catch($e Exception){
  13. * $saatler = $namaz->getAllTimes();
  14. * }
  15.  **/
  16.  
  17.  
  18. Class NamazSaati {
  19.  
  20. /**
  21. * Country
  22. * @access protected
  23. */
  24. protected $country;
  25.  
  26. /**
  27. * City
  28. * @access protected
  29. */
  30. protected $city;
  31.  
  32. /**
  33. * Data URL
  34. * @access private
  35. */
  36. private $url = "http://diyanet8.diyanet.gov.tr/turkish/namazapi/Getir.ashx?country=%country%&cities=%city%&Backcolor=000000&forecolor=ffffff&lang=TR&fontsize=14";
  37.  
  38. /**
  39. * Instance for DOM stuff
  40. * @access private
  41. */
  42. private $xml;
  43.  
  44. /**
  45. * constructor, build url and load the DOM
  46. * @return void
  47. * @param string $country Country
  48. * @param string $city City
  49. */
  50. public function __construct($country, $city)
  51. {
  52. //redefine
  53. $country = strtoupper($country);
  54. $city = strtoupper($city);
  55. $this->setCountry($country);
  56. $this->setCity($city);
  57.  
  58. $this->dom = new DOMDocument('1.0', 'utf-8'); // an instance from DOMDocument
  59. $replace = array('%country%', '%city%');
  60. $replaceWith = array($country, $city);
  61. $dataUrl = str_replace($replace, $replaceWith, $this->url);
  62. $content = @file_get_contents($dataUrl); // trying to connect the API
  63.  
  64. if($content === false){ // If we can't connect the API catch it!
  65. throw new Exception('The API is unavailable')
  66. }
  67.  
  68. $content = "<html><head><title>Namaz Saati Class</title></head><body>".$content."</body></html>";
  69. $this->dom->loadHTML($content);
  70. }
  71.  
  72. /**
  73. * setter for Country
  74. * @return void
  75. * @param string $country Country
  76. */
  77. private function setCountry($country)
  78. {
  79. $this->country = $country;
  80. }
  81.  
  82. /**
  83. * setter for City
  84. * @return void
  85. * @param string $city city
  86. */
  87. private function setCity($city)
  88. {
  89. $this->city = $city;
  90. }
  91.  
  92. /**
  93. * getter for Country
  94. * @return string Country
  95. */
  96. public function getCountry()
  97. {
  98. return $this->country;
  99. }
  100.  
  101. /**
  102. * getter for City
  103. * @return string City
  104. */
  105. public function getCity()
  106. {
  107. return $this->city;
  108. }
  109.  
  110. /**
  111. * getter for Imsak time
  112. * @return string $timeSelector time of imsak
  113. */
  114. public function getImsakTime()
  115. {
  116. $timeSelector = $this->dom->getElementsByTagName('td')->item(9)->nodeValue;
  117. return $timeSelector;
  118. }
  119.  
  120. /**
  121. * getter for Gunes time
  122. * @return string $timeSelector time of Gunes
  123. */
  124. public function getGunesTime()
  125. {
  126. $timeSelector = $this->dom->getElementsByTagName('td')->item(10)->nodeValue;
  127. return $timeSelector;
  128. }
  129.  
  130. /**
  131. * getter for Ogle time
  132. * @return string $timeSelector time of Ogle
  133. */
  134. public function getOgleTime()
  135. {
  136. $timeSelector = $this->dom->getElementsByTagName('td')->item(11)->nodeValue;
  137. return $timeSelector;
  138. }
  139.  
  140. /**
  141. * getter for Ikindi time
  142. * @return string $timeSelector time of ikindi
  143. */
  144. public function getIkindiTime()
  145. {
  146. $timeSelector = $this->dom->getElementsByTagName('td')->item(12)->nodeValue;
  147. return $timeSelector;
  148. }
  149.  
  150. /**
  151. * getter for Aksam time
  152. * @return string $timeSelector time of Aksam
  153. */
  154. public function getAksamTime()
  155. {
  156. $timeSelector = $this->dom->getElementsByTagName('td')->item(13)->nodeValue;
  157. return $timeSelector;
  158. }
  159.  
  160. /**
  161. * getter for Yatsi time
  162. * @return string $timeSelector time of Yatsi
  163. */
  164. public function getYatsiTime()
  165. {
  166. $timeSelector = $this->dom->getElementsByTagName('td')->item(14)->nodeValue;
  167. return $timeSelector;
  168. }
  169.  
  170. /**
  171. * getter all times of the current day
  172. * @return array $times all times of current day
  173. */
  174. public function getAllTime()
  175. {
  176. $times = array(
  177. 'imsak' => $this->getImsakTime(),
  178. 'gunes' => $this->getGunesTime(),
  179. 'ogle' => $this->getOgleTime(),
  180. 'ikindi' => $this->getIkindiTime(),
  181. 'aksam' => $this->getAksamTime(),
  182. 'yatsi' => $this->getYatsiTime()
  183. );
  184. return $times;
  185. }
  186. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.