Source for file atom.php
Documentation is available at atom.php
* PHP Version 5.3.1とCakePHP 1.3.11で動作を確認。
* @author http://purl.org/meta/me/
* @version Atom Helper 1.0 2011-08-17
* @license Creative Commons License CC BY (http://creativecommons.org/licenses/by/3.0/)
App::import('Helper', 'Xml');
* AtomHelperクラスはAtomの作成を手助けするヘルパーです。
* @link http://www.ietf.org/rfc/rfc4287.txt
* entry要素以下を一時的に溜めておくメンバー変数
* @see getEntry(), setEntry()
* feed要素以下を一時的に溜めておくメンバー変数
* @see getFeed(), setFeed()
* Generate author element.(en)
* 'name' => 'Mark Pilgrim',
* 'uri' => 'http://example.org/',
* 'email' => 'f8dy@example.com'
* <name>Mark Pilgrim</name>
* <uri>http://example.org/</uri>
* <email>f8dy@example.com</email>
* # <name>Mark Pilgrim</name>
* # <uri>http://example.org/</uri>
* # <email>f8dy@example.com</email>
* @param mixed $content author要素の内容。配列または文字列(ja) author element content. array or string(en)
* @param array $attributes author要素の属性(ja) The attributes of the author element(en)
* @return string author要素(ja) author element(en)
function author($content =
array(), $attributes =
array()) {
* Generate category element.(en)
* @param array $attributes category要素の属性(ja) The attributes of the category element(en)
* @return string category要素(ja) category element(en)
function category($attributes =
array()) {
$attributes +=
array('#urn' =>
false);
if(isset
($attributes['scheme']) &&
!$attributes['#urn']) {
$attributes['scheme'] =
$this->url($attributes['scheme'], true);
unset
($attributes['#urn']);
return $this->tag('generator', null, $attributes);
* Generate content element.(en)
* '<p><i>[Update: The Atom draft is finished.]</i></p>',
* array('type' => 'xhtml', 'xml:lang' => 'en', 'xml:base' => 'http://diveintomark.org/')
* # <content type="xhtml" xml:lang="en" xml:base="http://diveintomark.org/">
* # <div xmlns="http://www.w3.org/1999/xhtml">
* # <p><i>[Update: The Atom draft is finished.]</i></p>
* @param string $content content要素の内容(ja) content element content(en)
* @param array $attributes content要素の属性(ja) The attributes of the content element(en)
* @return string content要素(ja) content element(en)
function content($content =
null, $attributes =
array()) {
$attributes +=
array('#escape' =>
true);
if(isset
($attributes['src'])) {
$attributes['src'] =
$this->url($attributes['src'], true);
if(!isset
($attributes['type'])) {
$attributes['type'] =
'text';
if($attributes['type'] ===
'xhtml') {
$content =
$this->tag('div', $content, array('xmlns' =>
'http://www.w3.org/1999/xhtml'));
$attributes['#escape'] =
false;
} elseif(preg_match('/^.+?(\+xml|\/xml)$/i', $attributes['type'])) {
// type属性の末尾が"+xml"か"/xml"の場合の処理
$attributes['#escape'] =
false;
} elseif($attributes['type'] !==
'text' &&
$attributes['type'] !==
'html' &&
stripos($attributes['type'], 'text/') ===
0) {
return $this->tag('content', $content, $attributes);
* contributor要素を生成します。(ja)
* Generate contributor element.(en)
* $atom->contributor(array(
* # <name>Sam Ruby</name>
* @param mixed $content contributor要素の内容(ja) contributor element content(en) 配列または文字列
* @param array $attributes contributor要素の属性(ja) The attributes of the contributor element(en)
* @return string contributor要素(ja) contributor element(en)
function contributor($content =
array(), $attributes =
array()) {
* Returns the current URL.(en)
* @return string 現在のURL(ja) current URL(en)
$xmlBase =
env('HTTP_HOST') .
env('REQUEST_URI');
return 'https://' .
$xmlBase;
return 'http://' .
$xmlBase;
* タイムスタンプをAtomの日付形式に変換します。
* $atom->date(1071340202);
* # 2003-12-13T18:30:02+00:00
* @param integer $timestamp タイムスタンプ(ja) Timestamp(en)
* @return string date-time形式(ja) date-time format(en)
function date($timestamp =
null) {
return date(DATE_ATOM, $timestamp);
* Generate email element.(en)
* $atom->email('f8dy@example.com');
* # <email>f8dy@example.com</email>
* @param string $content email要素の内容(ja) email element content(en)
* @param array $attributes email要素の属性(ja) The attributes of the email element(en)
* @return string email要素(ja) email element(en)
function email($content, $attributes =
array()) {
$attributes +=
array('#escape' =>
true);
return $this->tag('email', $content, $attributes);
* $atom->endTag('entry');
* @param string $name 要素の名前(ja) The name of the end tag(en)
* @return string 終了タグ(ja) End tag(en)
$out =
'</' .
$name .
'>';
* Generate entry element.(en)
* $atom->entry('<title>Example</title>...');
* // atom::setEntryで溜めたものを吐き出します。
* @param string $content entry要素の内容(ja) entry element content(en)
* @param array $attributes entry要素の属性(ja) The attributes of the entry element(en)
* @return string entry要素(ja) entry element(en)
function entry($content =
null, $attributes =
array()) {
return $this->tag('entry', $content, $attributes);
* Generate feed element.(en)
* $atom->feed('<title>Example</title>...');
* // atom::setFeedで溜めたものを吐き出します。
* @param string $content feed要素の内容(ja) feed element content(en)
* @param array $attributes feed要素の属性(ja) The attributes of the feed element(en)
* @return string feed要素(ja) feed element(en)
function feed($content =
null, $attributes =
array()) {
$attributes =
$attributes +
array('xmlns' =>
'http://www.w3.org/2005/Atom');
if(Configure::read('Config.language')) {
$attributes['xml:lang'] =
Configure::read('Config.language');
if(!isset
($attributes['xml:base'])) {
return $this->tag('feed', $content, $attributes);
* Generate generator element.(en)
* $atom->generator('Example Toolkit', array('uri' => 'http://www.example.com/', 'version' => '1.0'));
* # <generator uri="http://www.example.com/" version="1.0">Example Toolkit</generator>
* @param string $content generator要素の内容(ja) generator element content(en)
* @param array $attributes generator要素の属性(ja) The attributes of the generator element(en)
* @return string generator要素(ja) generator element(en)
function generator($content, $attributes =
array()) {
$attributes +=
array('#escape' =>
true, '#urn' =>
false);
if(isset
($attributes['uri']) &&
!$attributes['#urn']) {
$attributes['uri'] =
$this->url($attributes['uri'], true);
unset
($attributes['#urn']);
return $this->tag('generator', $content, $attributes);
* このヘルパー内に溜められているentry要素以下を吐き出します。
* @param boolean $clean 溜められているものを削除するか
* @return string entry要素内のXML要素
return $this->__getContent('_entry', $clean);
* このヘルパー内に溜められているfeed要素以下を吐き出します。
* @param boolean $clean 溜められているものを削除するか
* @return string feed要素内のXML要素
return $this->__getContent('_feed', $clean);
* Generate icon element.(en)
* $atom->icon('/favicon.ico');
* # <icon>http://example.com/favicon.ico</icon>
* @param mixed $url アイコンへのURL(ja) Icon URL(en)
* @param array $attributes icon要素の属性(ja) The attributes of the icon element(en)
* @return string icon要素(ja) icon element(en)
function icon($url, $attributes =
array()) {
$attributes +=
array('#escape' =>
true, '#urn' =>
false);
if(!$attributes['#urn']) {
$url =
$this->url($url, true);
unset
($attributes['#urn']);
return $this->tag('icon', $url, $attributes);
* Generate id element.(en)
* $atom->id('tag:example.org,2003:3', array('#urn' => true));
* # <id>tag:example.org,2003:3</id>
* @param mixed $id 識別子(ja) Identifier(en)
* @param array $attributes id要素の属性(ja) The attributes of the id element(en)
* @return string id要素(ja) id element(en)
function id($id =
array(), $attributes =
array()) {
$attributes +=
array('#escape' =>
true, '#urn' =>
false);
if(!$attributes['#urn']) {
$id =
$this->url($id, true);
unset
($attributes['#urn']);
return $this->tag('id', $id, $attributes);
* Generate link element.(en)
* $atom->link(array('controller' => 'posts', 'action' => 'index'));
* $atom->link(null, array('rel' => 'self'));
* 'http://example.org/audio/ph34r_my_podcast.mp3',
* array('rel' => 'enclosure', 'type' => 'audio/mpeg', 'length' => 1337)
* # <link rel="alternate" href="http://example.com/posts" />
* # <link rel="self" href="http://example.com/" />
* # <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3" />
* @param mixed $url URL リンク先へのURL
* @param array $attributes link要素の属性(ja) The attributes of the link element(en)
* @return string link要素(ja) link element(en)
function link($url =
null, $attributes =
array()) {
$attributes +=
array('#urn' =>
false);
if(!isset
($attributes['rel'])) {
$attributes['rel'] =
'alternate';
if(!$url &&
$attributes['rel'] ===
'self') {
if(!$url && isset
($attributes['href'])) {
$url =
$attributes['href'];
if(!$attributes['#urn']) {
$url =
$this->url($url, true);
unset
($attributes['#urn']);
$attributes['href'] =
$url;
return $this->tag('link', null, $attributes);
* Generate logo element.(en)
* $atom->logo('/img/logo.png');
* # <logo>http://example.com/img/logo.png</logo>
* @param mixed $url ロゴのURL(ja) Logo URL(en)
* @param array $attributes logo要素の属性(ja) The attributes of the logo element(en)
* @return string logo要素(ja) logo element(en)
function logo($url, $attributes =
array()) {
$attributes +=
array('#escape' =>
true, '#urn' =>
false);
if(!$attributes['#urn']) {
$url =
$this->url($url, true);
unset
($attributes['#urn']);
return $this->tag('logo', $url, $attributes);
* Generate name element.(en)
* $atom->name('Mark Pilgrim');
* # <name>Mark Pilgrim</name>
* @param string $content name要素の内容(ja) name element content(en)
* @param array $attributes name要素の属性(ja) The attributes of the name element(en)
* @return string name要素(ja) name element(en)
function name($content, $attributes =
array()) {
$attributes +=
array('#escape' =>
true);
return $this->tag('name', $content, $attributes);
* atomPersonConstructとして処理し任意の要素を生成します。(ja)
* $atom->personConstruct('author',array(
* # <name>Sam Ruby</name>
* @param mixed $name 要素の名前(ja) The name of the element(en)
* @param mixed $content 要素の内容 配列または文字列(ja) element content. array or string(en)
* @param array $attributes 要素の属性(ja) The attributes of the element(en)
* @return string XMLの要素(ja) XML elements(en)
function personConstruct($name, $content =
array(), $attributes =
array()) {
foreach($content as $key =>
$value) {
$result .=
$this->{$key}($value);
$out =
$this->tag($name, $result, $attributes);
* Generate published element.(en)
* $atom->published(1071318569);
* $atom->published('2003-12-13T08:29:29-04:00');
* # <published>2003-12-13T08:29:29-04:00</published>
* @param mixed $timestamp published要素の内容。タイムスタンプまたはAtomの日付形式(ja) published element content. timestamp or Atom date format(en)
* @param array $attributes published要素の属性(ja) The attributes of the published element(en)
* @return string published要素(ja) published element(en)
function published($timestamp =
null, $attributes =
array()) {
return $this->tag('published', $this->date($timestamp), $attributes);
* Generate rights element.(en)
* $atom->rights('Copyright (c) 2003, Mark Pilgrim');
* # <rights type="text">Copyright (c) 2003, Mark Pilgrim</rights>
* @param string $content rights要素の内容(ja) rights element content(en)
* @param array $attributes rights要素の属性(ja) The attributes of the rights element(en)
* @return string rights要素(ja) rights element(en)
function rights($content, $attributes =
array()) {
* The multidimensional array is transformed into XML.(en)
* $atom->serialize(array(
* array('stylesheet', '/xsl/atom.xsl', array('charset' => 'UTF-8', 'type' => 'text/xsl')),
* array('title', 'dive into mark'),
* array('subtitle', 'A <em>lot</em> of effort went into making this effortless', array('type' => 'html')),
* array('updated', '2003-12-13T18:30:02Z'),
* array('id', 'tag:example.org,2003:3', array('#urn' => true)),
* array('link', '/', array('type' => 'text/html')),
* array('link', null, array('rel' => 'self', 'type' => 'application/atom+xml')),
* array('rights', 'Copyright (c) 2003, Mark Pilgrim'),
* array('generator', 'Example Toolkit', array('uri' => 'http://www.example.com/', 'version' => '1.0')),
* array('dc:description', 'A lot of effort went into making this effortless'),
* '<dc:format>application/atom+xml</dc:format>',
* '<dc:date>2003-12-13T18:30:02Z</dc:date>',
* array('title', 'Atom draft-07 snapshot'),
* array('link', 'http://example.org/2003/12/13/atom03', array('type' => 'text/html')),
* array('link', 'http://example.org/audio/ph34r_my_podcast.mp3', array('rel' => 'enclosure', 'type' => 'audio/mpeg', 'length' => '1337')),
* array('id', 'tag:example.org,2003:3.2397', array('#urn' => true)),
* array('updated', '2005-07-31T12:29:29Z'),
* array('published', 1071318569),
* array('author', array('name' => 'John Doe', 'uri' => 'http://example.org/', 'email' => 'f8dy@example.com')),
* array('contributor', array('name' => 'Sam Ruby')),
* array('contributor', array('name' => 'Joe Gregorio')),
* array('content', '<p><i>[Update: The Atom draft is finished.]</i></p>', array('type' => 'xhtml', 'xml:lang' => 'en', 'xml:base' => 'http://diveintomark.org/'))
* array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/')
* # <?xml version="1.0" encoding="UTF-8" ?>
* # <?xml-stylesheet charset="UTF-8" type="text/xsl" href="http://example.com/xsl/atom.xsl" ?>
* # <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">
* # <title type="text">dive into mark</title>
* # <subtitle type="html">A <em>lot</em> of effort went into making this effortless</subtitle>
* # <updated>2003-12-13T18:30:02Z</updated>
* # <id>tag:example.org,2003:3</id>
* # <link type="text/html" rel="alternate" href="http://example.com/" />
* # <link rel="self" type="application/atom+xml" href="http://example.com/atom" />
* # <rights type="text">Copyright (c) 2003, Mark Pilgrim</rights>
* # <generator uri="http://www.example.com/" version="1.0">Example Toolkit</generator>
* # <dc:description>A lot of effort went into making this effortless</dc:description>
* # <dc:format>application/atom+xml</dc:format>
* # <dc:date>2003-12-13T18:30:02Z</dc:date>
* # <title type="text">Atom draft-07 snapshot</title>
* # <link type="text/html" rel="alternate" href="http://example.org/2003/12/13/atom03" />
* # <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3" />
* # <id>tag:example.org,2003:3.2397</id>
* # <updated>2005-07-31T12:29:29Z</updated>
* # <published>2003-12-13T12:29:29+00:00</published>
* # <name>John Doe</name>
* # <uri>http://example.org/</uri>
* # <email>f8dy@example.com</email>
* # <name>Sam Ruby</name>
* # <name>Joe Gregorio</name>
* # <content type="xhtml" xml:lang="en" xml:base="http://diveintomark.org/">
* # <div xmlns="http://www.w3.org/1999/xhtml">
* # <p><i>[Update: The Atom draft is finished.]</i></p>
* @param array $data 多次元配列(ja) Multidimensional array(en)
* @return string XMLの要素(ja) XML elements(en)
foreach($data as $key =>
$content) {
* ヘルパー内にentry要素以下の要素を溜めます。
* $atom->setEntry('title', 'Example Page');
* $atom->setEntry('link', array('controller' => 'posts', 'action' => 'index'));
* $atom->setEntryTitle('Example Page');
* $atom->setEntryLink(array('controller' => 'posts', 'action' => 'index'));
* ヘルパー内にfeed要素以下の要素を溜めます。
* $atom->setFeed('title', 'Example Feed');
* $atom->setFeed('link', array('controller' => 'posts', 'action' => 'index'));
* $atom->setFeedTitle('Example Feed');
* $atom->setFeedLink(array('controller' => 'posts', 'action' => 'index'));
* Generate start tag.(en)
* $atom->startTag('feed');
* $atom->startTag('entry');
* @param string $name 要素の名前(ja) The name of the start tag(en)
* @param array $attributes 開始タグの属性(ja) The attributes of the start tag(en)
* @return string 開始タグ(ja) start tag(en)
function startTag($name, $attributes =
array()) {
* xml-stylesheetを生成します。(ja)
* Generate xml-stylesheet.(en)
* $atom->stylesheet('/xsl/atom.xsl', array('charset' => 'UTF-8', 'type' => 'text/xsl'));
* # <?xml-stylesheet charset="UTF-8" type="text/xsl" href="http://example.com/xsl/atom.xsl" ?>
* @param string $url スタイルシートへのURL(ja) Stylesheet URL(en)
* @param array $attributes xml-stylesheetの属性(ja) The attributes of the xml-stylesheet(en)
* @return string xml-stylesheet
function stylesheet($url =
null, $attributes =
array()) {
if(!$url && isset
($attributes['href'])) {
$url =
$attributes['href'];
$attributes['href'] =
$this->url($url, true);
* Generate subtitle element.(en)
* $atom->subtitle('A <em>lot</em> of effort', array('type' => 'html'));]
* # <subtitle type="html">A <em>lot</em> of effort</subtitle>
* @param string $content subtitle要素の内容(ja) subtitle element content(en)
* @param array $attributes subtitle要素の属性(ja) The attributes of the subtitle element(en)
* @return string subtitle要素(ja) subtitle element(en)
function subtitle($content, $attributes =
array()) {
* Generate summary element.(en)
* $atom->summary('Some text.');
* # <summary type="text">Some text.</summary>
* @param string $content summary要素の内容(ja) summary element content(en)
* @param array $attributes summary要素の属性(ja) The attributes of the summary element(en)
* @return string summary要素(ja) summary element(en)
function summary($content, $attributes =
array()) {
* Generate XML element.(en)
* $atom->tag('dc:description', 'Example Page', array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'));
* $atom->description('Example Page', array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'), 'dc');
* # <dc:description xmlns:dc="http://purl.org/dc/elements/1.1/">Example Page</dc:description>
* @param string $name 要素の名前(ja) The name of the XML element(en)
* @param string $content 要素の内容(ja) The attributes of the XML element(en)
* @param array $attributes 要素の属性(ja) XML element content(en)
* @param string $namespace 要素の名前空間(ja) The namespace of the XML element(en)
* @return string XMLの要素(ja) XML element(en)
function tag($name =
null, $content =
null, $attributes =
array(), $namespace =
false) {
$attributes =
$attributes +
array('#escape' =>
false, '#cdata' =>
false);
if($content &&
$attributes['#escape']) {
unset
($attributes['#escape']);
if(isset
($attributes['#cdata'])) {
$cdata =
$attributes['#cdata'];
unset
($attributes['#cdata']);
$node =
$this->Xml->createElement($name, $content, array(), $namespace);
foreach($attributes as $attKey =>
$attValue) {
$node->attributes[$attKey] =
$attValue;
$node =
$this->Xml->createTextNode($content);
return $node->toString(array('cdata' =>
$cdata));
* atomTextConstructとして処理し任意の要素を生成します
* 'A <em>lot</em> of effort went into making this effortless',
* array('type' => 'html')
* # <subtitle type="html">A <em>lot</em> of effort went into making this effortless</subtitle>
* @param string $name 要素の名前(ja) The name of the element(en)
* @param string $content 要素の内容(ja) element content(en)
* @param array $attributes 要素の属性(ja) The attributes of the element(en)
* @return string XMLの要素(ja) XML elements(en)
$attributes +=
array('#escape' =>
true);
if(!isset
($attributes['type'])) {
$attributes['type'] =
'text';
} elseif($attributes['type'] ===
'xhtml') {
$content =
$this->tag('div', $content, array('xmlns' =>
'http://www.w3.org/1999/xhtml'));
$attributes['#escape'] =
false;
return $this->tag($name, $content, $attributes);
* Generate title element.(en)
* $atom->title('Example Feed');
* # <title type="text">Example Feed</title>
* @param string $content title要素の内容(ja) title element content(en)
* @param array $attributes title要素の属性(ja) The attributes of the title element(en)
* @return string title要素(ja) title element(en)
function title($content, $attributes =
array()) {
* Generate updated element.(en)
* $atom->updated(1071340202);
* $atom->updated('2003-12-13T18:30:02Z');
* # <updated>2003-12-13T18:30:02Z</updated>
* @param mixed $timestamp updated要素の内容。タイムスタンプまたはAtomの日付形式(ja) updated element content. timestamp or Atom date format(en)
* @param array $attributes updated要素の属性(ja) The attributes of the updated element(en)
* @return string updated要素(ja) updated element(en)
function updated($timestamp =
null, $attributes =
array()) {
return $this->tag('updated', $this->date($timestamp), $attributes);
* Generate uri element.(en)
* $atom->uri('http://example.org/');
* # <uri>http://example.org/</uri>
* @param mixed $url uri要素の内容(ja) uri element content(en)
* @param array $attributes uri要素の属性(ja) The attributes of the uri element(en)
* @return string uri要素(ja) uri element(en)
function uri($url, $attributes =
array()) {
$attributes +=
array('#escape' =>
true, '#urn' =>
false);
if(!$attributes['#urn']) {
$url =
$this->url($url, true);
unset
($attributes['#urn']);
return $this->tag('uri', $url, $attributes);
* Finds URL for specified action.
* Returns a URL pointing at the provided parameters.
* @param mixed $url Either a relative string url like `/products/view/23` or
* an array of url parameters. Using an array for urls will allow you to leverage
* the reverse routing features of CakePHP.
* @param boolean $full If true, the full base URL will be prepended to the result
* @return string Full translated URL with base path.
* @link http://book.cakephp.org/view/1448/url
function url($url =
null, $full =
false) {
return html_entity_decode(parent::url($url, $full), ENT_QUOTES, Configure::read('App.encoding'));
* @param array $attribute 属性(ja) attributes(en)
* @return string 属性(ja) attributes(en)
foreach($attributes as $key =>
$content) {
$out .=
' ' .
$key .
'="' .
h($content) .
'"';
function __call($name, $arguments) {
if(preg_match('/^(setFeed|setEntry)([A-Z].+?)$/', $name, $matches)) {
* @param string $name 取り出すメンバー変数
* @param boolean $clean 溜めてあるものを消去するか
function __getContent($name, $clean =
false) {
foreach($this->{$name} as $key =>
$content) {
if($content['name'] ===
'#text') {
$out .=
implode($content['arguments']);
$this->{$name} =
array();
Documentation generated on Sat, 27 Aug 2011 09:04:27 +0000 by phpDocumentor 1.4.3