<?php
/**
* Yahoo Weather
* @package WebService :: YWeather , Cache :: CacheContent
* @author ntf
* @version 1.0
* @copyright Creative Commons Attribution-Noncommercial 3.0 Unported http://creativecommons.org/licenses/by-nc/3.0/
* Please read Terms of Use http://developer.yahoo.com/weather/ before using this class
*/
/**
* @Author ntf
* @package Cache :: Cache Content
* @version 1.0
* Last modified: 6/2/2009 17:51
* You must not remove or modify this copyright anyway unless you get the permission from the authors.
* @license Creative Commons Attribution-Noncommercial 3.0 Unported http://creativecommons.org/licenses/by-nc/3.0/
*/
/**
* Cache Content
* @package Cache
*/
class CacheContent{
protected $cache_file;
protected $life_time=30;
/**
* Constructer
*
* @param string $cache_file
* @return CacheContent
*/
function __construct($cache_file){
$this->cache_file = $cache_file;
$this->load($this->cache_file);
return $this;
}
/**
* Set Expire Time
*
* @param int $time
*/
function SetExpireTime($time){
$this->life_time=(int) $time;
}
/**
* Check the Cache is expired
*
* @return boolean
*/
function is_expired(){
if($this->exists()){
if(time() - $this->time() < $this->life_time){
return false;
}
}
return true;
}
protected $_resource;
protected $_filename;
protected $_mode;
public $content;
/**
* Create New File
*
* @param string $File
*/
function new_file($File){
touch($File);
}
/**
* Load Exists File
*
* @param string $handleFile
* @return boolean
*/
function load($handleFile=null){
$this->_filename=$handleFile;
if(empty($handleFile) or !file_exists($handleFile) or is_dir($handleFile)){ //File must be exists , not a dir
return false;
}
$this->content=file_get_contents($this->_filename);
return true;
}
function __deconstruct(){
$this->close();
}
/**
* Delete the file
*
* @return boolean
*/
function delete(){
return unlink($this->_filename);
}
/**
* Exists Check
*
* @return boolean
*/
function exists(){
return file_exists($this->_filename);
}
/**
* Save the file
*
* @param string $newfilename=null
* @return File
*/
function save($newfilename=null){
file_put_contents( !empty($newfilename) && $newfilename != $this->_filename ? $newfilename : $this->_filename ,$this->content);
return $this;
}
/**
* Close The File Contoller
*/
final function close(){
}
/**
* Get the file size
*
* @return float
*/
function size(){
return filesize($this->_filename);
}
/**
* Get the file Time (UNIX Time)
*
* @return int
*/
function time(){
return filemtime($this->_filename);
}
/**
* Get the file last access time (UNIX Time)
*
* @return int
*/
function last_access_time(){
return fileatime($this->_filename);
}
/**
* Get the file owner
*
* @return string
*/
function owner(){
return fileowner($this->_filename);
}
/**
* Get the file permissions
*
* @return string
*/
function permissions(){
return fileperms($this->_filename);
}
/**
* IS file readable?
*
* @return boolean
*/
function readable(){
return is_readable($this->_filename);
}
/**
* IS file writable?
*
* @return boolean
*/
function writable(){
return is_writable($this->_filename);
}
/**
* Get Extention of the File
*
* @return string
*/
function file_extention(){
return end(explode('.',$this->_filename));
}
}
/**
* Yahoo Weather
* @package WebService :: YWeather
* @author ntf
* @version 1.0
* @copyright Creative Commons Attribution-Noncommercial 3.0 Unported http://creativecommons.org/licenses/by-nc/3.0/
*/
class YWeather extends CacheContent{
protected $position;
protected $units='c';
protected $cachefile='YWeather.{position}.{units}.cache';
const API = 'http://weather.yahooapis.com/forecastrss?p={position}&u={units}';
/**
* Constructer
*
* @param string $position
* @param string $units f: Fahrenheit c: Celsius
*/
function __construct($position,$units='c'){
if(!class_exists('SimpleXMLElement')){
throw new Exception('YWeather require SimpleXML');
return false;
}
$this->position = $position;
$this->units = $units=='c' ? 'c' : 'f';
$this->SetExpireTime(1800);
}
/**
* Set Cache File Life Time
*
* @param int $lifetime
* @return YWeather
*/
function SetLifetime($lifetime){
$this->SetExpireTime( (int) $lifetime);
return $this;
}
/**
* Set The Cache File (Default : YWeather.{position}.{units}.cache)
*
* @param string $cachefile
* @return YWeather
*/
function SetCacheFile($cachefile){
$this->cachefile = $cachefile;
return $this;
}
/**
* Query Weather Info From Cache / Yahoo Api
*
* @return array
*/
function Fetch(){
parent::__construct( $this->_variable_replace( $this->cachefile) );
if(parent::is_expired() or empty($this->content)){
$result = $this->_FetchApi();
$this->content= serialize($result);
parent::save();
return $result;
}else{
return unserialize( $this->content );
}
}
/**
* Fetch The Yahoo Weather WebService
*
* @return array
* @access protected
*/
protected function _FetchApi(){
$api = $this->_variable_replace( self::API );
$rss = simplexml_load_file($api);
$currentCondition = $rss->xpath("//yweather:condition");
$forecast = $rss->xpath("//yweather:forecast");
$currentConditionImg = 'http://l.yimg.com/a/i/us/we/52/'.$currentCondition[0]['code'].'.gif';
$currentCondition = (array) $currentCondition[0];
$forecast1 = (array) $forecast[0];
$forecast2 = (array) $forecast[1];
$yahoo_link = $rss->xpath("//image");
$yahoo_link = (array) $yahoo_link[0];
return array('Condition'=>(array) $currentCondition['@attributes'],'forecast'=>array((array) $forecast1['@attributes'],(array) $forecast2['@attributes']),'ConditionImg'=>$currentConditionImg,'Img'=>'http://l.yimg.com/a/i/us/we/52/{code}.gif','units'=>$this->units,'yahoo_link'=>$yahoo_link);
}
protected function _variable_replace($text){
return str_replace(array('{position}','{units}'),array($this->position,$this->units) , $text);
}
}