xnate

webclass.php

Mar 31st, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. class WebPage {
  4.     /**
  5.      * @var string Texte compris entre <head> et </head>
  6.      */
  7.     private $head  = null;
  8.     /**
  9.      * @var string Texte compris entre <title> et </title>
  10.      */
  11.     private $title = null ;
  12.     /**
  13.      * @var string Texte compris entre <body> et </body>
  14.      */
  15.     private $body  = null ;
  16.  
  17.     /**
  18.      * Constructeur
  19.      * @param string $title Titre de la page
  20.      */
  21.     public function __construct($title=null) {
  22.         $this->head = <<<HTML
  23. <!DOCTYPE html>
  24. <html>
  25.     <head>   
  26. HTML;
  27.     }
  28.  
  29.     /**
  30.      * Protéger les caractères spéciaux pouvant dégrader la page Web
  31.      * @param string $string La chaîne à protéger
  32.      * @return string La chaîne protégée
  33.      */
  34.     public function escapeString($string) {
  35.     return htmlspecialchars($string , ENT_HTML5);
  36.     }
  37.  
  38.     /**
  39.      * Affecter le titre de la page
  40.      * @param string $title Le titre
  41.      */
  42.     public function setTitle($title) {
  43.         $this->title .= "<title>$title</title>";
  44.     }
  45.    /**
  46.      * Ajouter un contenu dans head
  47.      * @param string $content Le contenu à ajouter
  48.      * @return void
  49.      */
  50.     public function appendToHead($content) {
  51.     $this->head .= $content;
  52.     }
  53.  
  54.     /**
  55.      * Ajouter un contenu CSS dans head
  56.      * @param string $css Le contenu CSS à ajouter
  57.      * @return void
  58.      */
  59.     public function appendCss($css) {
  60.     $this->head .= "<style>$css</style>";
  61.     }
  62.  
  63.     /**
  64.      * Ajouter l'URL d'un script CSS dans head
  65.      * @param string $url L'URL du script CSS
  66.      * @return void
  67.      */
  68.     public function appendCssUrl($url) {
  69.     $this->head .= "<link href='$url' rel='stylesheet' type='text/css'>";
  70.     }
  71.  
  72.     /**
  73.      * Ajouter un contenu JavaScript dans head
  74.      * @param string $js Le contenu JavaScript à ajouter
  75.      * @return void
  76.      */
  77.     public function appendJs($js) {
  78.     $this->head .= "<script type='text/javascript' language='JavaScript'>$js</script>";
  79.     }
  80.  
  81.     /**
  82.      * Ajouter l'URL d'un script JavaScript dans head
  83.      * @param string $url L'URL du script JavaScript
  84.      * @return void
  85.      */
  86.     public function appendJsUrl($url) {
  87.     $this->head .= "<script type='text/javascript' language='JavaScript' src='$url'></script>";
  88.     }
  89.  
  90.     /**
  91.      * Ajouter un contenu dans body
  92.      * @param string $content Le contenu à ajouter
  93.      * @return void
  94.      */
  95.     public function appendContent($content) {
  96.     $this->body .= $content;
  97.     }
  98.  
  99.     /**
  100.      * Produire la page Web complète
  101.      * @return string
  102.      */
  103.     public function toHTML() {
  104.         return "$this->head"."$this->title"."</head><body>"."$this->body"."</body></html>";
  105.        
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment