Source for file atom.php

Documentation is available at atom.php

  1. <?php
  2. /**
  3.  * Atom Helper
  4.  *
  5.  * PHP Version 5.3.1とCakePHP 1.3.11で動作を確認。
  6.  *
  7.  * @author http://purl.org/meta/me/
  8.  * @version Atom Helper 1.0 2011-08-17
  9.  * @license Creative Commons License CC BY (http://creativecommons.org/licenses/by/3.0/)
  10.  */
  11.  
  12. App::import('Helper''Xml');
  13.  
  14. /**
  15.  * AtomHelperクラスはAtomの作成を手助けするヘルパーです。
  16.  *
  17.  * @link http://www.ietf.org/rfc/rfc4287.txt
  18.  */
  19. class AtomHelper extends XmlHelper {
  20.  
  21.     /**
  22.      * entry要素以下を一時的に溜めておくメンバー変数
  23.      *
  24.      * @var array 
  25.      * @access protected
  26.      * @see getEntry(), setEntry()
  27.      */
  28.     var $_entry = array();
  29.  
  30.     /**
  31.      * feed要素以下を一時的に溜めておくメンバー変数
  32.      *
  33.      * @var array 
  34.      * @access protected
  35.       * @see getFeed(), setFeed()
  36.      */
  37.     var $_feed = array();
  38.  
  39.     /**
  40.      * author要素を生成します。(ja)
  41.      *
  42.      * Generate author element.(en)
  43.      *
  44.      * <code>
  45.      * $atom->author(array(
  46.      *     'name' => 'Mark Pilgrim',
  47.      *     'uri' => 'http://example.org/',
  48.      *     'email' => 'f8dy@example.com'
  49.      * ));
  50.      *
  51.      * $atom->author('
  52.      *     <name>Mark Pilgrim</name>
  53.      *     <uri>http://example.org/</uri>
  54.      *     <email>f8dy@example.com</email>
  55.      * ');
  56.      *
  57.      * # 出力すると以下のようになります。
  58.      * # <author>
  59.      * #     <name>Mark Pilgrim</name>
  60.      * #     <uri>http://example.org/</uri>
  61.      * #     <email>f8dy@example.com</email>
  62.      * # </author>
  63.      * </code>
  64.      *
  65.      * @param mixed $content author要素の内容。配列または文字列(ja) author element content. array or string(en)
  66.      * @param array $attributes author要素の属性(ja) The attributes of the author element(en)
  67.      * @return string author要素(ja) author element(en)
  68.      * @access public
  69.      */
  70.     function author($content array()$attributes array()) {
  71.         $out $this->personConstruct('author'$content$attributes);
  72.  
  73.         return $out;
  74.     }
  75.  
  76.     /**
  77.      * category要素を生成します。(ja)
  78.      *
  79.      * Generate category element.(en)
  80.      *
  81.      * @param array $attributes category要素の属性(ja) The attributes of the category element(en)
  82.      * @return string category要素(ja) category element(en)
  83.      * @access public
  84.      */
  85.     function category($attributes array()) {
  86.         $attributes += array('#urn' => false);
  87.  
  88.         if(isset($attributes['scheme']&& !$attributes['#urn']{
  89.             $attributes['scheme'$this->url($attributes['scheme']true);
  90.         }
  91.  
  92.         unset($attributes['#urn']);
  93.  
  94.         return $this->tag('generator'null$attributes);
  95.     }
  96.  
  97.     /**
  98.      * content要素を生成します。(ja)
  99.      *
  100.      * Generate content element.(en)
  101.      *
  102.      * <code>
  103.      * $atom->content(
  104.      *     '<p><i>[Update: The Atom draft is finished.]</i></p>',
  105.      *     array('type' => 'xhtml', 'xml:lang' => 'en', 'xml:base' => 'http://diveintomark.org/')
  106.      * );
  107.      *
  108.      * # 出力すると以下のようになります。
  109.      * # <content type="xhtml" xml:lang="en" xml:base="http://diveintomark.org/">
  110.      * #    <div xmlns="http://www.w3.org/1999/xhtml">
  111.      * #        <p><i>[Update: The Atom draft is finished.]</i></p>
  112.      * #    </div>
  113.      * # </content>
  114.      * </code>
  115.      *
  116.      * @param string $content content要素の内容(ja) content element content(en)
  117.      * @param array $attributes content要素の属性(ja) The attributes of the content element(en)
  118.      * @return string content要素(ja) content element(en)
  119.      * @access public
  120.      */
  121.     function content($content null$attributes array()) {
  122.         $attributes += array('#escape' => true);
  123.  
  124.         if(isset($attributes['src'])) {
  125.             $attributes['src'$this->url($attributes['src']true);
  126.         else {
  127.             if(!isset($attributes['type'])) {
  128.                 $attributes['type''text';
  129.             }
  130.  
  131.             if($attributes['type'=== 'xhtml'{
  132.                 // type属性が"xhtml"の場合の処理
  133.  
  134.                 $content $this->tag('div'$contentarray('xmlns' => 'http://www.w3.org/1999/xhtml'));
  135.  
  136.                 $attributes['#escape'false;
  137.             elseif(preg_match('/^.+?(\+xml|\/xml)$/i'$attributes['type'])) {
  138.                 // type属性の末尾が"+xml"か"/xml"の場合の処理
  139.  
  140.                 $attributes['#escape'false;
  141.             elseif($attributes['type'!== 'text' && $attributes['type'!== 'html' && stripos($attributes['type']'text/'=== 0{
  142.                 // type属性が規定のものではない場合の処理
  143.  
  144.                 $content base64_encode($content);
  145.             }
  146.         }
  147.  
  148.         return $this->tag('content'$content$attributes);
  149.     }
  150.  
  151.     /**
  152.      * contributor要素を生成します。(ja)
  153.      *
  154.      * Generate contributor element.(en)
  155.      *
  156.      * <code>
  157.      * $atom->contributor(array(
  158.      *     'name' => 'Sam Ruby'
  159.      * ));
  160.      *
  161.      * $atom->contributor('
  162.      *     <name>Sam Ruby</name>
  163.      * ');
  164.      *
  165.      * # 出力すると以下のようになります。
  166.      * # <contributor>
  167.      * #     <name>Sam Ruby</name>
  168.      * # </contributor>
  169.      * </code>
  170.      *
  171.      * @param mixed $content contributor要素の内容(ja) contributor element content(en) 配列または文字列
  172.      * @param array $attributes contributor要素の属性(ja) The attributes of the contributor element(en)
  173.      * @return string contributor要素(ja) contributor element(en)
  174.      * @access public
  175.      */
  176.     function contributor($content array()$attributes array()) {
  177.         $out $this->personConstruct('contributor'$content$attributes);
  178.  
  179.         return $out;
  180.     }
  181.  
  182.     /**
  183.      * 現在のURLを返します。(ja)
  184.      *
  185.      * Returns the current URL.(en)
  186.      *
  187.      * <code>
  188.      * $atom->currentUrl();
  189.      * </code>
  190.      *
  191.      * @return string 現在のURL(ja) current URL(en)
  192.      * @access public
  193.      */
  194.     function currentUrl({
  195.         $xmlBase env('HTTP_HOST'env('REQUEST_URI');
  196.  
  197.         if(env('HTTPS')) {
  198.             return 'https://' $xmlBase;
  199.         }
  200.  
  201.         return 'http://' $xmlBase;
  202.     }
  203.  
  204.     /**
  205.      * タイムスタンプをAtomの日付形式に変換します。
  206.      *
  207.      * <code>
  208.      * $atom->date(1071340202);
  209.      *
  210.      * # 出力すると以下のようになります。
  211.      * # 2003-12-13T18:30:02+00:00
  212.      * </code>
  213.      *
  214.      * @param integer $timestamp タイムスタンプ(ja) Timestamp(en)
  215.      * @return string date-time形式(ja) date-time format(en)
  216.      * @access public
  217.      */
  218.     function date($timestamp null{
  219.         if(!$timestamp{
  220.             $timestamp time();
  221.         }
  222.  
  223.         if(ctype_digit((string) $timestamp)) {
  224.             return date(DATE_ATOM$timestamp);
  225.         }
  226.  
  227.         return $timestamp;
  228.     }
  229.  
  230.     /**
  231.      * email要素を生成します。(ja)
  232.      *
  233.      * Generate email element.(en)
  234.      *
  235.      * <code>
  236.      * $atom->email('f8dy@example.com');
  237.      *
  238.      * # 出力すると以下のようになります。
  239.      * # <email>f8dy@example.com</email>
  240.      * </code>
  241.      *
  242.      * @param string $content email要素の内容(ja) email element content(en)
  243.      * @param array $attributes email要素の属性(ja) The attributes of the email element(en)
  244.      * @return string email要素(ja) email element(en)
  245.      * @access public
  246.      */
  247.     function email($content$attributes array()) {
  248.         $attributes += array('#escape' => true);
  249.         return $this->tag('email'$content$attributes);
  250.     }
  251.  
  252.     /**
  253.      * 終了タグを生成します。(ja)
  254.      *
  255.      * Generate end tag.(en)
  256.      *
  257.      * <code>
  258.      * $atom->endTag('entry');
  259.      *
  260.      * $atom->endTag('feed');
  261.      *
  262.      * # 出力すると以下のようになります。
  263.      * # </entry>
  264.      * #
  265.      * # </feed>
  266.      * </code>
  267.      *
  268.      * @param string $name 要素の名前(ja) The name of the end tag(en)
  269.      * @return string 終了タグ(ja) End tag(en)
  270.      * @access public
  271.      */
  272.     function endTag($name{
  273.         $out '</' $name '>';
  274.  
  275.         return $out;
  276.     }
  277.  
  278.     /**
  279.      * entry要素を生成します。(ja)
  280.      *
  281.      * Generate entry element.(en)
  282.      *
  283.      * <code>
  284.      * $atom->entry('<title>Example</title>...');
  285.      *
  286.      * // atom::setEntryで溜めたものを吐き出します。
  287.      * $atom->entry();
  288.      * </code>
  289.      *
  290.      * @param string $content entry要素の内容(ja) entry element content(en)
  291.      * @param array $attributes entry要素の属性(ja) The attributes of the entry element(en)
  292.      * @return string entry要素(ja) entry element(en)
  293.      * @access public
  294.      */
  295.     function entry($content null$attributes array()) {
  296.         if(!$content{
  297.             $content $this->getEntry();
  298.         }
  299.  
  300.         return $this->tag('entry'$content$attributes);
  301.     }
  302.  
  303.     /**
  304.      * feed要素を生成します。(ja)
  305.      *
  306.      * Generate feed element.(en)
  307.      *
  308.      * <code>
  309.      * $atom->feed('<title>Example</title>...');
  310.      *
  311.      * // atom::setFeedで溜めたものを吐き出します。
  312.      * $atom->feed();
  313.      * </code>
  314.      *
  315.      * @param string $content feed要素の内容(ja) feed element content(en)
  316.      * @param array $attributes feed要素の属性(ja) The attributes of the feed element(en)
  317.      * @return string feed要素(ja) feed element(en)
  318.      * @access public
  319.      */
  320.     function feed($content null$attributes array()) {
  321.         $attributes $attributes array('xmlns' => 'http://www.w3.org/2005/Atom');
  322.  
  323.         if(Configure::read('Config.language')) {
  324.             $attributes['xml:lang'Configure::read('Config.language');
  325.         }
  326.  
  327.         if(!isset($attributes['xml:base'])) {
  328.             $attributes['xml:base'$this->currentUrl();
  329.         }
  330.  
  331.         if(!$content{
  332.             $content $this->getFeed();
  333.         }
  334.  
  335.         return $this->tag('feed'$content$attributes);
  336.     }
  337.  
  338.     /**
  339.      * generator要素を生成します。(ja)
  340.      *
  341.      * Generate generator element.(en)
  342.      *
  343.      * <code>
  344.      * $atom->generator('Example Toolkit', array('uri' => 'http://www.example.com/', 'version' => '1.0'));
  345.      *
  346.      * # 出力すると以下のようになります。
  347.      * # <generator uri="http://www.example.com/" version="1.0">Example Toolkit</generator>
  348.      * </code>
  349.      *
  350.      * @param string $content generator要素の内容(ja) generator element content(en)
  351.      * @param array $attributes generator要素の属性(ja) The attributes of the generator element(en)
  352.      * @return string generator要素(ja) generator element(en)
  353.      * @access public
  354.      */
  355.     function generator($content$attributes array()) {
  356.         $attributes += array('#escape' => true'#urn' => false);
  357.  
  358.         if(isset($attributes['uri']&& !$attributes['#urn']{
  359.             $attributes['uri'$this->url($attributes['uri']true);
  360.         }
  361.  
  362.         unset($attributes['#urn']);
  363.  
  364.         return $this->tag('generator'$content$attributes);
  365.     }
  366.  
  367.     /**
  368.      * このヘルパー内に溜められているentry要素以下を吐き出します。
  369.      *
  370.      * <code>
  371.      * $atom->getEntry();
  372.      * </code>
  373.      *
  374.      * @param boolean $clean 溜められているものを削除するか
  375.      * @return string entry要素内のXML要素
  376.      * @access public
  377.        * @see setEntry()
  378.      */
  379.     function getEntry($clean true{
  380.         return $this->__getContent('_entry'$clean);
  381.     }
  382.  
  383.     /**
  384.      * このヘルパー内に溜められているfeed要素以下を吐き出します。
  385.      *
  386.      * <code>
  387.      * $atom->getFeed();
  388.      * </code>
  389.      *
  390.      * @param boolean $clean 溜められているものを削除するか
  391.      * @return string feed要素内のXML要素
  392.      * @access public
  393.        * @see setFeed()
  394.      */
  395.     function getFeed($clean true{
  396.         return $this->__getContent('_feed'$clean);
  397.     }
  398.  
  399.     /**
  400.      * icon要素を生成します。(ja)
  401.      *
  402.      * Generate icon element.(en)
  403.      *
  404.      * <code>
  405.      * $atom->icon('/favicon.ico');
  406.      *
  407.      * # 出力すると以下のようになります。
  408.      * # <icon>http://example.com/favicon.ico</icon>
  409.      * </code>
  410.      *
  411.      * @param mixed $url アイコンへのURL(ja) Icon URL(en)
  412.      * @param array $attributes icon要素の属性(ja) The attributes of the icon element(en)
  413.      * @return string icon要素(ja) icon element(en)
  414.      * @access public
  415.      */
  416.     function icon($url$attributes array()) {
  417.         $attributes += array('#escape' => true'#urn' => false);
  418.  
  419.         if(!$attributes['#urn']{
  420.             $url $this->url($urltrue);
  421.         }
  422.  
  423.         unset($attributes['#urn']);
  424.  
  425.         return $this->tag('icon'$url$attributes);
  426.     }
  427.  
  428.     /**
  429.      * id要素を生成します。(ja)
  430.      *
  431.      * Generate id element.(en)
  432.      *
  433.      * <code>
  434.      * $atom->id('tag:example.org,2003:3', array('#urn' => true));
  435.      *
  436.      * # 出力すると以下のようになります。
  437.      * # <id>tag:example.org,2003:3</id>
  438.      * </code>
  439.      *
  440.      * @param mixed $id 識別子(ja) Identifier(en)
  441.      * @param array $attributes id要素の属性(ja) The attributes of the id element(en)
  442.      * @return string id要素(ja) id element(en)
  443.      * @access public
  444.      */
  445.     function id($id array()$attributes array()) {
  446.         $attributes += array('#escape' => true'#urn' => false);
  447.  
  448.         if(!$attributes['#urn']{
  449.             $id $this->url($idtrue);
  450.         }
  451.  
  452.         unset($attributes['#urn']);
  453.  
  454.         return $this->tag('id'$id$attributes);
  455.     }
  456.  
  457.     /**
  458.      * link要素を生成します。(ja)
  459.      *
  460.      * Generate link element.(en)
  461.      *
  462.      * <code>
  463.      * $atom->link(array('controller' => 'posts', 'action' => 'index'));
  464.      *
  465.      * $atom->link(null, array('rel' => 'self'));
  466.      *
  467.      * $atom->link(
  468.      *     'http://example.org/audio/ph34r_my_podcast.mp3',
  469.      *     array('rel' => 'enclosure', 'type' => 'audio/mpeg', 'length' => 1337)
  470.      * );
  471.      *
  472.      * # 出力すると以下のようになります。
  473.      * # <link rel="alternate" href="http://example.com/posts" />
  474.      * #
  475.      * # <link rel="self" href="http://example.com/" />
  476.      * #
  477.      * # <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3" />
  478.      * </code>
  479.      *
  480.      * @param mixed $url URL リンク先へのURL
  481.      * @param array $attributes link要素の属性(ja) The attributes of the link element(en)
  482.      * @return string link要素(ja) link element(en)
  483.      * @access public
  484.      */
  485.     function link($url null$attributes array()) {
  486.         $attributes += array('#urn' => false);
  487.  
  488.         if(!isset($attributes['rel'])) {
  489.             $attributes['rel''alternate';
  490.         }
  491.  
  492.         if(!$url && $attributes['rel'=== 'self'{
  493.             $url $this->currentUrl();
  494.         }
  495.  
  496.         if(!$url && isset($attributes['href'])) {
  497.             $url $attributes['href'];
  498.         }
  499.  
  500.         if(!$attributes['#urn']{
  501.             $url $this->url($urltrue);
  502.         }
  503.  
  504.         unset($attributes['#urn']);
  505.  
  506.         $attributes['href'$url;
  507.  
  508.         return $this->tag('link'null$attributes);
  509.     }
  510.  
  511.     /**
  512.      * logo要素を生成します。(ja)
  513.      *
  514.      * Generate logo element.(en)
  515.      *
  516.      * <code>
  517.      * $atom->logo('/img/logo.png');
  518.      *
  519.      * # 出力すると以下のようになります。
  520.      * # <logo>http://example.com/img/logo.png</logo>
  521.      * </code>
  522.      *
  523.      * @param mixed $url ロゴのURL(ja) Logo URL(en)
  524.      * @param array $attributes logo要素の属性(ja) The attributes of the logo element(en)
  525.      * @return string logo要素(ja) logo element(en)
  526.      * @access public
  527.      */
  528.     function logo($url$attributes array()) {
  529.         $attributes += array('#escape' => true'#urn' => false);
  530.  
  531.         if(!$attributes['#urn']{
  532.             $url $this->url($urltrue);
  533.         }
  534.  
  535.         unset($attributes['#urn']);
  536.  
  537.         return $this->tag('logo'$url$attributes);
  538.     }
  539.  
  540.     /**
  541.      * name要素を生成します。(ja)
  542.      *
  543.      * Generate name element.(en)
  544.      *
  545.      * <code>
  546.      * $atom->name('Mark Pilgrim');
  547.      *
  548.      * # 出力すると以下のようになります。
  549.      * # <name>Mark Pilgrim</name>
  550.      * </code>
  551.      *
  552.      * @param string $content name要素の内容(ja) name element content(en)
  553.      * @param array $attributes name要素の属性(ja) The attributes of the name element(en)
  554.      * @return string name要素(ja) name element(en)
  555.      * @access public
  556.      */
  557.     function name($content$attributes array()) {
  558.         $attributes += array('#escape' => true);
  559.         return $this->tag('name'$content$attributes);
  560.     }
  561.  
  562.     /**
  563.      * atomPersonConstructとして処理し任意の要素を生成します。(ja)
  564.      *
  565.      * <code>
  566.      * $atom->personConstruct('author',array(
  567.      *     'name' => 'Sam Ruby'
  568.      * ));
  569.      *
  570.      * # 出力すると以下のようになります。
  571.      * # <author>
  572.      * #     <name>Sam Ruby</name>
  573.      * # </author>
  574.      * </code>
  575.      *
  576.      * @param mixed $name 要素の名前(ja) The name of the element(en)
  577.      * @param mixed $content 要素の内容 配列または文字列(ja) element content. array or string(en)
  578.      * @param array $attributes 要素の属性(ja) The attributes of the element(en)
  579.      * @return string XMLの要素(ja) XML elements(en)
  580.      * @access public
  581.      */
  582.     function personConstruct($name$content array()$attributes array()) {
  583.         $result '';
  584.  
  585.         if(is_array($content)) {
  586.             foreach($content as $key => $value{
  587.                 $result .= $this->{$key}($value);
  588.             }
  589.         else {
  590.             $result $content;
  591.         }
  592.  
  593.         $out $this->tag($name$result$attributes);
  594.  
  595.         return $out;
  596.     }
  597.  
  598.     /**
  599.      * published要素を生成します。(ja)
  600.      *
  601.      * Generate published element.(en)
  602.      *
  603.      * <code>
  604.      * $atom->published(1071318569);
  605.      *
  606.      * $atom->published('2003-12-13T08:29:29-04:00');
  607.      *
  608.      * # 出力すると以下のようになります。
  609.      * # <published>2003-12-13T08:29:29-04:00</published>
  610.      * </code>
  611.      *
  612.      * @param mixed $timestamp published要素の内容。タイムスタンプまたはAtomの日付形式(ja) published element content. timestamp or Atom date format(en)
  613.      * @param array $attributes published要素の属性(ja) The attributes of the published element(en)
  614.      * @return string published要素(ja) published element(en)
  615.      * @access public
  616.      */
  617.     function published($timestamp null$attributes array()) {
  618.         return $this->tag('published'$this->date($timestamp)$attributes);
  619.     }
  620.  
  621.     /**
  622.      * rights要素を生成します。(ja)
  623.      *
  624.      * Generate rights element.(en)
  625.      *
  626.      * <code>
  627.      * $atom->rights('Copyright (c) 2003, Mark Pilgrim');
  628.      *
  629.      * # 出力すると以下のようになります。
  630.      * # <rights type="text">Copyright (c) 2003, Mark Pilgrim</rights>
  631.      * </code>
  632.      *
  633.      * @param string $content rights要素の内容(ja) rights element content(en)
  634.      * @param array $attributes rights要素の属性(ja) The attributes of the rights element(en)
  635.      * @return string rights要素(ja) rights element(en)
  636.      * @access public
  637.      */
  638.     function rights($content$attributes array()) {
  639.         return $this->textConstruct('rights'$content$attributes);
  640.     }
  641.  
  642.     /**
  643.      * 多次元配列をXMLに変形します。(ja)
  644.      *
  645.      * The multidimensional array is transformed into XML.(en)
  646.      *
  647.      * <code>
  648.      * $atom->serialize(array(
  649.      *     array('header'),
  650.      *     array('stylesheet', '/xsl/atom.xsl', array('charset' => 'UTF-8', 'type' => 'text/xsl')),
  651.      *     array('feed', array(
  652.      *         array('title', 'dive into mark'),
  653.      *         array('subtitle', 'A <em>lot</em> of effort went into making this effortless', array('type' => 'html')),
  654.      *         array('updated', '2003-12-13T18:30:02Z'),
  655.      *         array('id', 'tag:example.org,2003:3', array('#urn' => true)),
  656.      *         array('link', '/', array('type' => 'text/html')),
  657.      *         array('link', null, array('rel' => 'self', 'type' => 'application/atom+xml')),
  658.      *         array('rights', 'Copyright (c) 2003, Mark Pilgrim'),
  659.      *         array('generator', 'Example Toolkit', array('uri' => 'http://www.example.com/', 'version' => '1.0')),
  660.      *         array('dc:description', 'A lot of effort went into making this effortless'),
  661.      *         '<dc:format>application/atom+xml</dc:format>',
  662.      *         '<dc:date>2003-12-13T18:30:02Z</dc:date>',
  663.      *         array('entry', array(
  664.      *             array('title', 'Atom draft-07 snapshot'),
  665.      *             array('link', 'http://example.org/2003/12/13/atom03', array('type' => 'text/html')),
  666.      *             array('link', 'http://example.org/audio/ph34r_my_podcast.mp3', array('rel' => 'enclosure', 'type' => 'audio/mpeg', 'length' => '1337')),
  667.      *             array('id', 'tag:example.org,2003:3.2397', array('#urn' => true)),
  668.      *             array('updated', '2005-07-31T12:29:29Z'),
  669.      *             array('published', 1071318569),
  670.      *             array('author', array('name' => 'John Doe', 'uri' => 'http://example.org/', 'email' => 'f8dy@example.com')),
  671.      *             array('contributor', array('name' => 'Sam Ruby')),
  672.      *             array('contributor', array('name' => 'Joe Gregorio')),
  673.      *             array('content', '<p><i>[Update: The Atom draft is finished.]</i></p>', array('type' => 'xhtml', 'xml:lang' => 'en', 'xml:base' => 'http://diveintomark.org/'))
  674.      *         ))
  675.      *     ),
  676.      *         array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/')
  677.      *     )
  678.      * ));
  679.      *
  680.      * # 出力すると以下のようになります。
  681.      * # <?xml version="1.0" encoding="UTF-8" ?>
  682.      * # <?xml-stylesheet charset="UTF-8" type="text/xsl" href="http://example.com/xsl/atom.xsl" ?>
  683.      * # <feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://www.w3.org/2005/Atom" xml:lang="en" xml:base="http://example.com/atom">
  684.      * #     <title type="text">dive into mark</title>
  685.      * #     <subtitle type="html">A &lt;em&gt;lot&lt;/em&gt; of effort went into making this effortless</subtitle>
  686.      * #     <updated>2003-12-13T18:30:02Z</updated>
  687.      * #     <id>tag:example.org,2003:3</id>
  688.      * #     <link type="text/html" rel="alternate" href="http://example.com/" />
  689.      * #     <link rel="self" type="application/atom+xml" href="http://example.com/atom" />
  690.      * #     <rights type="text">Copyright (c) 2003, Mark Pilgrim</rights>
  691.      * #     <generator uri="http://www.example.com/" version="1.0">Example Toolkit</generator>
  692.      * #     <dc:description>A lot of effort went into making this effortless</dc:description>
  693.      * #     <dc:format>application/atom+xml</dc:format>
  694.      * #     <dc:date>2003-12-13T18:30:02Z</dc:date>
  695.      * #     <entry>
  696.      * #         <title type="text">Atom draft-07 snapshot</title>
  697.      * #         <link type="text/html" rel="alternate" href="http://example.org/2003/12/13/atom03" />
  698.      * #         <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3" />
  699.      * #         <id>tag:example.org,2003:3.2397</id>
  700.      * #         <updated>2005-07-31T12:29:29Z</updated>
  701.      * #         <published>2003-12-13T12:29:29+00:00</published>
  702.      * #         <author>
  703.      * #             <name>John Doe</name>
  704.      * #             <uri>http://example.org/</uri>
  705.      * #             <email>f8dy@example.com</email>
  706.      * #         </author>
  707.      * #         <contributor>
  708.      * #             <name>Sam Ruby</name>
  709.      * #         </contributor>
  710.      * #         <contributor>
  711.      * #             <name>Joe Gregorio</name>
  712.      * #         </contributor>
  713.      * #         <content type="xhtml" xml:lang="en" xml:base="http://diveintomark.org/">
  714.      * #             <div xmlns="http://www.w3.org/1999/xhtml">
  715.      * #                 <p><i>[Update: The Atom draft is finished.]</i></p>
  716.      * #             </div>
  717.      * #         </content>
  718.      * #     </entry>
  719.      * # </feed>
  720.      * </code>
  721.      *
  722.      * @param array $data 多次元配列(ja) Multidimensional array(en)
  723.      * @return string XMLの要素(ja) XML elements(en)
  724.      * @access public
  725.      */
  726.     function serialize($data{
  727.         $out '';
  728.  
  729.         foreach($data as $key => $content{
  730.             if(is_array($content)) {
  731.                 $name array_shift($content);
  732.  
  733.                 unset($content['name']);
  734.  
  735.                 if(method_exists($this$name)) {
  736.                     $out .= call_user_func_array(array($this$name)$content);
  737.                 else {
  738.                     array_unshift($content$name);
  739.  
  740.                     $out .= call_user_func_array(array($this'tag')$content);
  741.                 }
  742.             else {
  743.                 $out .= $content;
  744.             }
  745.         }
  746.  
  747.         return $out;
  748.     }
  749.  
  750.     /**
  751.      * ヘルパー内にentry要素以下の要素を溜めます。
  752.      *
  753.      * <code>
  754.      * $atom->setEntry('title', 'Example Page');
  755.      *
  756.      * $atom->setEntry('link', array('controller' => 'posts', 'action' => 'index'));
  757.      *
  758.      * //以下のような書き方も可能です。
  759.      * $atom->setEntryTitle('Example Page');
  760.      *
  761.      * $atom->setEntryLink(array('controller' => 'posts', 'action' => 'index'));
  762.      * </code>
  763.      *
  764.      * @return void 
  765.      * @access public
  766.      * @see getEntry()
  767.      */
  768.     function setEntry({
  769.         $arguments func_get_args();
  770.         $name array_shift($arguments);
  771.         $this->_entry[compact('name''arguments');
  772.     }
  773.  
  774.     /**
  775.      * ヘルパー内にfeed要素以下の要素を溜めます。
  776.      *
  777.      * <code>
  778.      * $atom->setFeed('title', 'Example Feed');
  779.      *
  780.      * $atom->setFeed('link', array('controller' => 'posts', 'action' => 'index'));
  781.      *
  782.      * //以下のような書き方も可能です。
  783.      * $atom->setFeedTitle('Example Feed');
  784.      *
  785.      * $atom->setFeedLink(array('controller' => 'posts', 'action' => 'index'));
  786.      * </code>
  787.      *
  788.      * @return void 
  789.      * @access public
  790.      * @see getFeed()
  791.      */
  792.     function setFeed({
  793.         $arguments func_get_args();
  794.         $name array_shift($arguments);
  795.         $this->_feed[compact('name''arguments');
  796.     }
  797.  
  798.     /**
  799.      * 開始タグを生成します。(ja)
  800.      *
  801.      * Generate start tag.(en)
  802.      *
  803.      * <code>
  804.      * $atom->startTag('feed');
  805.      *
  806.      * $atom->startTag('entry');
  807.      *
  808.      * # 出力すると以下のようになります。
  809.      * # <feed>
  810.      * #
  811.      * # <entry>
  812.      * </code>
  813.      *
  814.      * @param string $name 要素の名前(ja) The name of the start tag(en)
  815.      * @param array $attributes 開始タグの属性(ja) The attributes of the start tag(en)
  816.      * @return string 開始タグ(ja) start tag(en)
  817.      * @access public
  818.      */
  819.     function startTag($name$attributes array()) {
  820.         $out '<';
  821.  
  822.         $out .= $name;
  823.  
  824.         if($attributes{
  825.             $out .= ' ' $this->_attributes($attributes);
  826.         }
  827.  
  828.         $out .= '>';
  829.  
  830.         return $out;
  831.     }
  832.  
  833.     /**
  834.      * xml-stylesheetを生成します。(ja)
  835.      *
  836.      * Generate xml-stylesheet.(en)
  837.      *
  838.      * <code>
  839.      * $atom->stylesheet('/xsl/atom.xsl', array('charset' => 'UTF-8', 'type' => 'text/xsl'));
  840.      *
  841.      * # 出力すると以下のようになります。
  842.      * # <?xml-stylesheet charset="UTF-8" type="text/xsl" href="http://example.com/xsl/atom.xsl" ?>
  843.      * </code>
  844.      *
  845.      * @param string $url スタイルシートへのURL(ja) Stylesheet URL(en)
  846.      * @param array $attributes xml-stylesheetの属性(ja) The attributes of the xml-stylesheet(en)
  847.      * @return string xml-stylesheet
  848.      * @access public
  849.      */
  850.     function stylesheet($url null$attributes array()) {
  851.         if(!$url && isset($attributes['href'])) {
  852.             $url $attributes['href'];
  853.         }
  854.  
  855.         $attributes['href'$this->url($urltrue);
  856.  
  857.         return $this->header('xml-stylesheet' $this->_attributes($attributes));
  858.     }
  859.  
  860.     /**
  861.      * subtitle要素を生成します。(ja)
  862.      *
  863.      * Generate subtitle element.(en)
  864.      *
  865.      * <code>
  866.      * $atom->subtitle('A <em>lot</em> of effort', array('type' => 'html'));]
  867.      *
  868.      * # 出力すると以下のようになります。
  869.      * # <subtitle type="html">A &lt;em&gt;lot&lt;/em&gt; of effort</subtitle>
  870.      * </code>
  871.      *
  872.      * @param string $content subtitle要素の内容(ja) subtitle element content(en)
  873.      * @param array $attributes subtitle要素の属性(ja) The attributes of the subtitle element(en)
  874.      * @return string subtitle要素(ja) subtitle element(en)
  875.      * @access public
  876.      */
  877.     function subtitle($content$attributes array()) {
  878.         return $this->textConstruct('subtitle'$content$attributes);
  879.     }
  880.  
  881.     /**
  882.      * summary要素を生成します。(ja)
  883.      *
  884.      * Generate summary element.(en)
  885.      *
  886.      * <code>
  887.      * $atom->summary('Some text.');
  888.      *
  889.      * # 出力すると以下のようになります。
  890.      * # <summary type="text">Some text.</summary>
  891.      * </code>
  892.      *
  893.      * @param string $content summary要素の内容(ja) summary element content(en)
  894.      * @param array $attributes summary要素の属性(ja) The attributes of the summary element(en)
  895.      * @return string summary要素(ja) summary element(en)
  896.      * @access public
  897.      */
  898.     function summary($content$attributes array()) {
  899.         return $this->textConstruct('summary'$content$attributes);
  900.     }
  901.  
  902.     /**
  903.      * XMLの要素を生成します。(ja)
  904.      *
  905.      * Generate XML element.(en)
  906.      *
  907.      * <code>
  908.      * $atom->tag('dc:description', 'Example Page', array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'));
  909.      *
  910.      * //以下のようにも記述できます。
  911.      * $atom->description('Example Page', array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'), 'dc');
  912.      *
  913.      * # 出力すると以下のようになります。
  914.      * # <dc:description xmlns:dc="http://purl.org/dc/elements/1.1/">Example Page</dc:description>
  915.      * </code>
  916.      *
  917.      * @param string $name 要素の名前(ja) The name of the XML element(en)
  918.      * @param string $content 要素の内容(ja) The attributes of the XML element(en)
  919.      * @param array $attributes 要素の属性(ja) XML element content(en)
  920.      * @param string $namespace 要素の名前空間(ja) The namespace of the XML element(en)
  921.      * @return string XMLの要素(ja) XML element(en)
  922.      * @access public
  923.      */
  924.     function tag($name null$content null$attributes array()$namespace false{
  925.         $attributes $attributes array('#escape' => false'#cdata' => false);
  926.  
  927.         if(is_array($content)) {
  928.             $content $this->serialize($content);
  929.         }
  930.  
  931.         if($content && $attributes['#escape']{
  932.             $content h($content);
  933.         }
  934.         unset($attributes['#escape']);
  935.  
  936.         $cdata false;
  937.         if(isset($attributes['#cdata'])) {
  938.             $cdata $attributes['#cdata'];
  939.             unset($attributes['#cdata']);
  940.         }
  941.  
  942.         if($name{
  943.             $node $this->Xml->createElement($name$contentarray()$namespace);
  944.  
  945.             foreach($attributes as $attKey => $attValue{
  946.                 $node->attributes[$attKey$attValue;
  947.             }
  948.         else {
  949.             $node $this->Xml->createTextNode($content);
  950.         }
  951.  
  952.         return $node->toString(array('cdata' => $cdata));
  953.     }
  954.  
  955.     /**
  956.      * atomTextConstructとして処理し任意の要素を生成します
  957.      *
  958.      * <code>
  959.      * $atom->textConstruct(
  960.      *     'subtitle',
  961.      *     'A <em>lot</em> of effort went into making this effortless',
  962.      *     array('type' => 'html')
  963.      * );
  964.      *
  965.      * # 出力すると以下のようになります。
  966.      * # <subtitle type="html">A &lt;em&gt;lot&lt;/em&gt; of effort went into making this effortless</subtitle>
  967.      * </code>
  968.      *
  969.      * @param string $name 要素の名前(ja) The name of the element(en)
  970.      * @param string $content 要素の内容(ja) element content(en)
  971.      * @param array $attributes 要素の属性(ja) The attributes of the element(en)
  972.      * @return string XMLの要素(ja) XML elements(en)
  973.      * @access public
  974.      */
  975.     function textConstruct($name$content$attributes array()) {
  976.         $attributes += array('#escape' => true);
  977.  
  978.         if(!isset($attributes['type'])) {
  979.             $attributes['type''text';
  980.         elseif($attributes['type'=== 'xhtml'{
  981.             $content $this->tag('div'$contentarray('xmlns' => 'http://www.w3.org/1999/xhtml'));
  982.             $attributes['#escape'false;
  983.         }
  984.  
  985.         return $this->tag($name$content$attributes);
  986.     }
  987.  
  988.     /**
  989.      * title要素を生成します。(ja)
  990.      *
  991.      * Generate title element.(en)
  992.      *
  993.      * <code>
  994.      * $atom->title('Example Feed');
  995.      *
  996.      * # 出力すると以下のようになります。
  997.      * # <title type="text">Example Feed</title>
  998.      * </code>
  999.      *
  1000.      * @param string $content title要素の内容(ja) title element content(en)
  1001.      * @param array $attributes title要素の属性(ja) The attributes of the title element(en)
  1002.      * @return string title要素(ja) title element(en)
  1003.      * @access public
  1004.      */
  1005.     function title($content$attributes array()) {
  1006.         return $this->textConstruct('title'$content$attributes);
  1007.     }
  1008.  
  1009.     /**
  1010.      * updated要素を生成します。(ja)
  1011.      *
  1012.      * Generate updated element.(en)
  1013.      *
  1014.      * <code>
  1015.      * $atom->updated(1071340202);
  1016.      *
  1017.      * $atom->updated('2003-12-13T18:30:02Z');
  1018.      *
  1019.      * # 出力すると以下のようになります。
  1020.      * # <updated>2003-12-13T18:30:02Z</updated>
  1021.      * </code>
  1022.      *
  1023.      * @param mixed $timestamp updated要素の内容。タイムスタンプまたはAtomの日付形式(ja) updated element content. timestamp or Atom date format(en)
  1024.      * @param array $attributes updated要素の属性(ja) The attributes of the updated element(en)
  1025.      * @return string updated要素(ja) updated element(en)
  1026.      * @access public
  1027.      */
  1028.     function updated($timestamp null$attributes array()) {
  1029.         return $this->tag('updated'$this->date($timestamp)$attributes);
  1030.     }
  1031.  
  1032.     /**
  1033.      * uri要素を生成します。(ja)
  1034.      *
  1035.      * Generate uri element.(en)
  1036.      *
  1037.      * <code>
  1038.      * $atom->uri('http://example.org/');
  1039.      *
  1040.      * # 出力すると以下のようになります。
  1041.      * # <uri>http://example.org/</uri>
  1042.      * </code>
  1043.      *
  1044.      * @param mixed $url uri要素の内容(ja) uri element content(en)
  1045.      * @param array $attributes uri要素の属性(ja) The attributes of the uri element(en)
  1046.      * @return string uri要素(ja) uri element(en)
  1047.      * @access public
  1048.      */
  1049.     function uri($url$attributes array()) {
  1050.         $attributes += array('#escape' => true'#urn' => false);
  1051.  
  1052.         if(!$attributes['#urn']{
  1053.             $url $this->url($urltrue);
  1054.         }
  1055.  
  1056.         unset($attributes['#urn']);
  1057.  
  1058.         return $this->tag('uri'$url$attributes);
  1059.     }
  1060.  
  1061.     /**
  1062.      * Finds URL for specified action.
  1063.      *
  1064.      * Returns a URL pointing at the provided parameters.
  1065.      *
  1066.      * @param mixed $url Either a relative string url like `/products/view/23` or
  1067.      *  an array of url parameters. Using an array for urls will allow you to leverage
  1068.      *  the reverse routing features of CakePHP.
  1069.      * @param boolean $full If true, the full base URL will be prepended to the result
  1070.      * @return string Full translated URL with base path.
  1071.      * @access public
  1072.      * @link http://book.cakephp.org/view/1448/url
  1073.      */
  1074.     function url($url null$full false{
  1075.         return html_entity_decode(parent::url($url$full)ENT_QUOTESConfigure::read('App.encoding'));
  1076.     }
  1077.  
  1078.     /**
  1079.      * 配列の属性を文字列化します。
  1080.      *
  1081.      * @param array $attribute 属性(ja) attributes(en)
  1082.      * @return string 属性(ja) attributes(en)
  1083.      * @access protected
  1084.      */
  1085.     function _attributes($attributes array()) {
  1086.         $out '';
  1087.  
  1088.         $attributes array_filter($attributes);
  1089.  
  1090.         foreach($attributes as $key => $content{
  1091.             $out .= ' ' $key '="' .  h($content'"';
  1092.         }
  1093.  
  1094.         return $out;
  1095.     }
  1096.  
  1097.     function __call($name$arguments{
  1098.         if(preg_match('/^(setFeed|setEntry)([A-Z].+?)$/'$name$matches)) {
  1099.             array_unshift($argumentslcfirst($matches[2]));
  1100.  
  1101.             return call_user_func_array(array($this$matches[1])$arguments);
  1102.         }
  1103.     
  1104.         array_unshift($arguments$name);
  1105.  
  1106.         return call_user_func_array(array($this'tag')$arguments);
  1107.     }
  1108.  
  1109.     /**
  1110.      * ヘルパー内に溜めてあるものを取り出します。
  1111.      *
  1112.      * @param string $name 取り出すメンバー変数
  1113.       * @param boolean $clean 溜めてあるものを消去するか
  1114.      * @return string XMLの要素
  1115.      * @access private
  1116.      */
  1117.     function __getContent($name$clean false{
  1118.         $out '';
  1119.  
  1120.         foreach($this->{$nameas $key => $content{
  1121.             if($content['name'=== '#text'{
  1122.                 $out .= implode($content['arguments']);
  1123.             else {
  1124.                 $out .= call_user_func_array(array($this$content['name'])$content['arguments']);
  1125.             }
  1126.         }
  1127.  
  1128.         if($clean{
  1129.             $this->{$namearray();
  1130.         }
  1131.  
  1132.         return $out;
  1133.     }
  1134. }
  1135. ?>

Documentation generated on Sat, 27 Aug 2011 09:04:27 +0000 by phpDocumentor 1.4.3