Blame view

protected/extensions/feed/EFeedItemAtom.php 3.1 KB
a1684257   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  <?php
  /**
   * EFeedItemAtom Class file
   * @author Antonio Ramirez 
   * @link http://www.ramirezcobos.com 
   * 
   * 
   * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS "AS IS" AND
   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   */
  /**
   * EFeedItemAtom is an element of an ATOM Feed
   *
   * @author		Antonio Ramirez <ramirez.cobos@gmail.com>
   * @package   	rss
   * @uses 		CUrlValidator
   * @throws 		CException
   */
  class EFeedItemAtom extends EFeedItemAbstract {
  	/**
  	 * (non-PHPdoc)
  	 * @see EFeedItemAbstract::setDescription()
  	 */
  	public function setDescription( $description ){
  		$this->addTag('summary', $description );
  	}
  	/**
  	 * (non-PHPdoc)
  	 * @see EFeedItemAbstract::setDate()
  	 */
  	public function setDate( $date ){
  		if(!is_numeric($date))
  			$date = strtotime($date);
  		$date = date(DATE_ATOM,$date);
  		
  		$this->addTag('updated', $date);
  	}
  	/**
  	 * 
  	 * Property getter date
  	 */
  	public function getDate(){
  		return $this->tags->itemAt('updated');
  	}
  	/**
  	 * (non-PHPdoc)
  	 * @see EFeedItemAbstract::setLink()
  	 */
  	public function setLink( $link ){
  		$validator = new CUrlValidator();
  		if(!$validator->validateValue($link))
  			throw new CException( Yii::t('EFeed', $link. ' does not seem to be a valid URL') );
  			
  		$this->addTag('link','',array('href'=>$link));
  		$this->addTag('id', EFeed::uuid($link,'urn:uuid:'));
  	}
  	/**
  	 * (non-PHPdoc)
  	 * @see EFeedItemAbstract::getNode()
  	 */
  	public function getNode(){
  		
  		$node = CHtml::openTag('entry').PHP_EOL;
  		
  		foreach( $this->tags as $tag ){
  			$node .= $this->getElement($tag);
  		}
  
  		$node .= CHtml::closeTag('entry').PHP_EOL;
  		
  		return $node;
  	}
  	/**
  	 * 
  	 * @return a well formatted XML element 
  	 * @param EFeedTag $tag
  	 */
  	private function getElement( EFeedTag $tag ){
  		
  		$element = '';
  		
  		if(in_array($tag->name,$this->CDATAEncoded))
  		{
  			$tag->attributes['type']="html";
  			$element .= CHtml::openTag($tag->name,$tag->attributes);
  			$element .= '<![CDATA["'.PHP_EOL;
  			
  		}else 
  		{
  			$element .= CHtml::openTag($tag->name,$tag->attributes);
  		}
  			
  		if(is_array($tag->content))
  		{ 
  			foreach ($tag->content as $tag => $content) 
  			{
  				$tmpTag = new EFeedTag($tag, $content);
  				
  				$element .= $this->getElement( $tmpTag );
  			}
  		}
  		else
  		{
  			$element .= (in_array($tag->name, $this->CDATAEncoded))? $tag->content : CHtml::encode($tag->content);
  		}           
  			
  		$element .= (in_array($tag->name, $this->CDATAEncoded))? "]]>":"";
  		
  		$element .= CHtml::closeTag($tag->name).PHP_EOL;
  		
  		return $element;
  	}
  }