Created
June 22, 2012 19:12
-
-
Save rbarros/2974574 to your computer and use it in GitHub Desktop.
Classe para criação das tags <meta>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('Elements.php'); | |
/** | |
* Metatag Application | |
* | |
* An open source application development framework for PHP 5.1.6 or newer | |
* | |
* @package HTML | |
* @category Classes | |
* @author Ramon Barros | |
* @copyright Copyright (c) 2012, Ramon Barros. | |
* @license http://www.ramon-barros.com/ | |
* @link http://public.ramon-barros.com/ | |
* @since Version 1.9.12 | |
* @filesource Metatag.php | |
*/ | |
/** | |
* Interface Metatag | |
* Interface para definição das tags necessárias | |
* @author Ramon Barros | |
* @copyright Copyright (c) 2012, Ramon Barros. | |
*/ | |
interface Metatag { | |
/** | |
* Define as funções necessárias para criação da metatag | |
*/ | |
public function setMeta( $meta = array() ); | |
public function getMeta(); | |
public function getArrayMetatag(); | |
public function getMetatag($meta); | |
public function createMetatag(); | |
} | |
/** | |
* Meta Class | |
* | |
* Classe regação da tag <meta> | |
* | |
* @package Metatag | |
* @subpackage Libraries | |
* @category HTML | |
* @author Ramon Barros | |
* @link http://public.ramon-barros.com/ | |
*/ | |
class Meta implements Metatag { | |
/** | |
* @var array | |
*/ | |
private $meta; | |
/** | |
* @var array | |
*/ | |
private $meta_attr; | |
/** | |
* @var array | |
*/ | |
private $metatag; | |
/** | |
* @var array | |
*/ | |
private $returnMeta; | |
/** | |
* @var array | |
*/ | |
private $tag; | |
/** | |
* Constroi o objeto que representa as tags meta | |
* @param array $meta | |
*/ | |
public function __construct( $meta = array() ){ | |
$this->meta_attr = array( | |
'article'=>array( | |
array('property'=>'og:title','content'=>'Titulo da página'), | |
array('property'=>'og:type','content'=>'article'), | |
array('property'=>'og:url','content'=>'Url completa da página'), | |
array('property'=>'og:image','content'=>'Logo do da página'), | |
array('property'=>'og:site_name','content'=>'Página'), | |
array('property'=>'og:locale','content'=>'pt_BR'), | |
array('property'=>'article:section','content'=>'Página'), | |
array('property'=>'article:published_time','content'=>'0000-00-00T00:00:00'), | |
array('property'=>'fb:app_id','content'=>'') | |
), | |
'website'=>array( | |
array('property'=>'og:title','content'=>'Titulo da página'), | |
array('property'=>'og:type','content'=>'article'), | |
array('property'=>'og:url','content'=>'Url completa da página'), | |
array('property'=>'og:image','content'=>'Logo do site'), | |
array('property'=>'og:site_name','content'=>'Rio Grande do Sul'), | |
array('property'=>'og:description','content'=>'Descrição da Página') | |
), | |
'title'=>array( | |
array('name'=>"title",'content'=>"Titulo da Página") | |
), | |
'keywords'=>array( | |
array('name'=>"keywords",'content'=>"sites, web, desenvolvimento, html, design") | |
), | |
'description'=>array( | |
array('name'=>"description",'content'=>"Meta Tags description") | |
), | |
'content-language'=>array( | |
array('http-equiv'=>"content-language",'content'=>"pt-br") | |
), | |
'content-type'=>array( | |
array('http-equiv'=>"content-type",'content'=>"text/html; charset=iso-8859-1") | |
), | |
'refresh'=>array( | |
array('http-equiv'=>"refresh",'content'=>" 5 ;url=http://www.ramon-barros.com/") | |
), | |
'author'=>array( | |
array('name'=>"author",'content'=>"Ramon Barros") | |
), | |
'copyright'=>array( | |
array('name'=>"copyright",'content'=>"Ramon Barros") | |
), | |
'email'=>array( | |
array('name'=>"email",'content'=>"[email protected]") | |
), | |
'charset'=>array( | |
array('name'=>"charset",'content'=>"ISO-8859-1") | |
), | |
'distribution'=>array( | |
array('name'=>"distribution",'content'=>"Global") | |
), | |
'rating'=>array( | |
array('name'=>"rating",'content'=>"General") | |
), | |
'revisit-after'=>array( | |
array('name'=>"revisit-after",'content'=>"1 Day") | |
), | |
'reply-to'=>array( | |
array('name'=>'reply-to','content'=>'[email protected]') | |
), | |
'generator'=>array( | |
array('name'=>"generator",'content'=>"Microsoft FrontPage 5.0") | |
), | |
'robots'=>array( | |
array('name'=>"robots",'content'=>"index,follow") | |
), | |
'googlebot'=>array( | |
array('name'=>"robots",'content'=>"index,follow") | |
), | |
'google-site-verification'=>array( | |
array('name'=>"google-site-verification",'content'=>"") | |
), | |
'ie5'=>array( | |
array('http-equiv'=>"X-UA-Compatible",'content'=>"IE=5") // Quirks Mode | |
), | |
'ie7'=>array( | |
array('http-equiv'=>"X-UA-Compatible",'content'=>"IE=7") // IE7 Standards | |
), | |
'ie8'=>array( | |
array('http-equiv'=>"X-UA-Compatible",'content'=>"IE=8") // IE8 Standards | |
), | |
'ie9'=>array( | |
array('http-equiv'=>"X-UA-Compatible",'content'=>"IE=9") // IE9 Standards | |
), | |
'ie-edge'=>array( | |
array('http-equiv'=>"X-UA-Compatible",'content'=>"IE=edge") //The latest standards - never use in production! | |
) | |
); | |
$this->tag = $this->meta_attr; | |
$this->setMeta( $meta )->createMetatag(); | |
} | |
/** | |
* Define as tags <meta> a serem criadas. | |
* @param array $meta | |
* @return object | |
*/ | |
public function setMeta( $meta = array() ){ | |
$this->meta = $meta; | |
return $this; | |
} | |
/** | |
* Recupera o array contendo as informações da tag <meta>. | |
* @return array $meta | |
*/ | |
public function getMeta(){ | |
return $this->meta; | |
} | |
/** | |
* Recupera o array contendo as tag <meta> criadas. | |
* @return array $metatag | |
*/ | |
public function getArrayMetatag(){ | |
return $this->metatag; | |
} | |
/** | |
* Recupera o array contendo as tag <meta> criadas referente ao atributo informado. | |
* @param string $meta | |
* @return array $metatag | |
*/ | |
public function getMetatag($meta){ | |
return $this->metatag[$meta]; | |
} | |
/** | |
* Valida se o atributo informado possui : | |
* @param string $value | |
* @return bool | |
*/ | |
private function checkContent($value){ | |
if(preg_match('/([^:]+)[:]([^*]+)/', $value)){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
/** | |
* Recupera o atributo e o valor informado | |
* @param string $value | |
* @return array $content | |
*/ | |
private function getContent($value){ | |
preg_match('/([^:]+)[:]([^*]+)/', $value , $content); | |
return $content; | |
} | |
/** | |
* Atualiza somente as tag <meta> informadas preservando a configuração. | |
* | |
* @param string $metakey Chave que identifica a tag <meta>. | |
* @param int $tagkey Chave que identifica o array onde será modificado o valor. | |
* @param array $metavalue Array contendo os atributos e valores a serem modificados. | |
* @param string $attr Atributo e valor do array de configuração $meta_attr; | |
* @param string $value Atributo e valor do array de configuração $meta_attr; | |
*/ | |
private function updateContent($metakey,$tagkey,$metavalue,$attr,$value){ | |
foreach ($metavalue as $attrmeta => $valuemeta) { | |
if($this->checkContent($valuemeta)){ | |
$getContent = $this->getContent($valuemeta); | |
$property = $getContent[1]; | |
$content = $getContent[2]; | |
if(strpos($value,$property)===false){ | |
}else{ | |
$this->returnMeta[$metakey][$tagkey][$attr]=$value; | |
$this->returnMeta[$metakey][$tagkey]['content']=$content; | |
} | |
}else{ | |
$this->returnMeta[$metakey][$tagkey][$attr]=$value; | |
$this->returnMeta[$metakey][$tagkey]['content']=$valuemeta; | |
} | |
} | |
} | |
/** | |
* Criação das tags <meta>, verificando se o atributo informado conrresponde no array $meta_attr de configuração. | |
*/ | |
public function createMetatag(){ | |
if ( count( $this->meta ) > 0 ) { | |
foreach ($this->meta as $metakey => $metavalue) | |
{ | |
if( in_array( $metakey , array_keys($this->tag) ) && is_array($metavalue) ) | |
{ | |
foreach ($this->tag[$metakey] as $tagkey => $tagvalue) { | |
foreach ($this->tag[$metakey][$tagkey] as $attr => $value) { | |
$this->updateContent($metakey,$tagkey,$metavalue,$attr,$value); | |
} | |
} | |
}else{ | |
if(in_array( $metavalue , array_keys($this->tag) )){ | |
foreach ($this->tag[$metavalue] as $tagkey => $tagvalue) { | |
foreach ($this->tag[$metavalue][$tagkey] as $attr => $value) { | |
$this->returnMeta[$metavalue][$tagkey][$attr]=$value; | |
} | |
} | |
}else{ | |
throw new BadMethodCallException( 'Chave não encontrada.'.$metakey.':'. $metavalue ); | |
} | |
} | |
} | |
}else{ | |
throw new BadMethodCallException( 'Nenhuma meta tag definida.' ); | |
} | |
return $this; | |
} | |
/** | |
* Carrega as tags <meta> no documento. | |
* @return void | |
*/ | |
public function loadMetaTag(){ | |
if(count( $this->returnMeta )>0){ | |
foreach ($this->returnMeta as $metaKey => $metaValue) { | |
foreach ($this->returnMeta[$metaKey] as $key => $value) { | |
$tag = new tag('meta',$this->returnMeta[$metaKey][$key] ); | |
$this->metatag[$metaKey] = $tag->get(); | |
echo $tag->get(); | |
} | |
} | |
}else{ | |
throw new BadMethodCallException( 'Nenhuma meta tag definida.' ); | |
} | |
} | |
} | |
$t = array( | |
'article'=>array( | |
'title:Titulo da Página - Ramon Barros', | |
'type:article' | |
), | |
'author'=>array('Ramon Barros'), | |
'content-type'=>array('text/html; charset=iso-8859-1'), | |
'ie7', | |
'ie8', | |
'ie9' | |
); | |
?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | |
<head> | |
<?php | |
$meta = new Meta($t); | |
$meta->loadMetaTag(); | |
?> | |
<title>Teste MetaTag</title> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment