Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Netcarver/Textile/Tag.php - 121 lines - 3565 bytes - Summary - Text - Print

Description: Textile - A Humane Web Text Generator.

   1  <?php
   2  
   3  /**
   4   * Textile - A Humane Web Text Generator.
   5   *
   6   * @link https://github.com/textile/php-textile
   7   */
   8  
   9  /*
  10   * Copyright (c) 2016-2017, Netcarver https://github.com/netcarver
  11   *
  12   * Redistribution and use in source and binary forms, with or without
  13   * modification, are permitted provided that the following conditions are met:
  14   *
  15   * * Redistributions of source code must retain the above copyright notice,
  16   * this list of conditions and the following disclaimer.
  17   *
  18   * * Redistributions in binary form must reproduce the above copyright notice,
  19   * this list of conditions and the following disclaimer in the documentation
  20   * and/or other materials provided with the distribution.
  21   *
  22   * * Neither the name Textile nor the names of its contributors may be used to
  23   * endorse or promote products derived from this software without specific
  24   * prior written permission.
  25   *
  26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36   * POSSIBILITY OF SUCH DAMAGE.
  37   */
  38  
  39  namespace Netcarver\Textile;
  40  
  41  /**
  42   * Renders HTML elements.
  43   *
  44   * This class can be used to HTML elements. It
  45   * does not sanitise attribute values, but can be
  46   * used to construct tags with nice object oriented
  47   * syntax.
  48   *
  49   * bc. use Netcarver\Textile\Tag;
  50   * $img = new Tag('img');
  51   * echo (string) $img->class('big blue')->src('images/elephant.jpg');
  52   *
  53   * @method Tag alt(string $text)
  54   * @method Tag align(string $alignment)
  55   * @method Tag href(string $url, bool $allowEmpty = false)
  56   * @method Tag rel(string $relationship)
  57   * @method Tag title(string $title)
  58   * @internal
  59   */
  60  
  61  class Tag extends DataBag
  62  {
  63      /**
  64       * The name of the tag.
  65       *
  66       * @var string|null
  67       */
  68  
  69      protected $tag;
  70  
  71      /**
  72       * Whether the tag is self-closing.
  73       *
  74       * @var bool
  75       */
  76  
  77      protected $selfclose;
  78  
  79      /**
  80       * Constructor.
  81       *
  82       * @param string|null $name        The tag name
  83       * @param array       $attributes  An array of attributes
  84       * @param bool        $selfclosing Whether the tag is self-closing
  85       */
  86  
  87      public function __construct($name, array $attributes = null, $selfclosing = true)
  88      {
  89          parent::__construct($attributes);
  90          $this->tag = $name;
  91          $this->selfclose = $selfclosing;
  92      }
  93  
  94      /**
  95       * Returns the tag as HTML.
  96       *
  97       * bc. $img = new Tag('img');
  98       * $img->src('images/example.jpg')->alt('Example image');
  99       * echo (string) $img;
 100       *
 101       * @return string A HTML element
 102       */
 103  
 104      public function __toString()
 105      {
 106          $attributes = '';
 107  
 108          if ($this->data) {
 109              ksort($this->data);
 110              foreach ($this->data as $name => $value) {
 111                  $attributes .= " $name=\"$value\"";
 112              }
 113          }
 114  
 115          if ($this->tag) {
 116              return '<' . $this->tag . $attributes . (($this->selfclose) ? " />" : '>');
 117          }
 118  
 119          return $attributes;
 120      }
 121  }

title

Description

title

Description

title

Description

title

title

Body