コード例 #1
0
ファイル: PurArray.php プロジェクト: rintaun/himawari
	/**
	 * Read the content of a file and deserialize it as an array. Unless provided, 
	 * deserialization format is derived from the file path extension.
	 * 
	 * Options include:
	 * - format Deserialization method (js and json accepted);
	 * - from_encoding Encoding of the file being read
	 * - to_encoding Encoding of the returned array
	 * 
	 * @return boolean True on success
	 * @param object $path
	 * @param object $options[optional]
	 */
	public static function read($path,array $options=array()){
		if(!isset($options['format'])){
			$dot = strrpos(basename($path),'.');
			if($dot===false) throw new Exception('Format Undertermined From Path: "'.basename($path).'"');
			$format = substr(basename($path),$dot+1);
		}
		switch($format){
			case 'js':
			case 'json':
				$content = trim(PurFile::read($path));
				try{
					return PurJson::decode($content,$options);
				}catch(Exception $e){
					throw new Exception($e->getMessage().': '.basename($path));
				}
			default:
				throw new Exception('Unsupported Format: "'.$format.'"');
		}
	}