Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Type/TypeCallable.php - 139 lines - 3314 bytes - Summary - Text - Print

Description: Callable object.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * http://textpattern.com
   6   *
   7   * Copyright (C) 2016 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <http://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Callable object.
  26   *
  27   * Inspects and converts callables.
  28   *
  29   * <code>
  30   * echo Txp::get('\Textpattern\Type\TypeCallable', array('class', 'method'))->toString();
  31   * </code>
  32   *
  33   * @since   4.6.0
  34   * @package Type
  35   */
  36  
  37  namespace Textpattern\Type;
  38  
  39  class TypeCallable implements TypeInterface
  40  {
  41      /**
  42       * The callable.
  43       *
  44       * @var callable
  45       */
  46  
  47      protected $callable;
  48  
  49      /**
  50       * Constructor.
  51       *
  52       * @param string $callable The callable
  53       */
  54  
  55      public function __construct($callable)
  56      {
  57          $this->callable = $callable;
  58      }
  59  
  60      /**
  61       * Gets the callable string presentation.
  62       *
  63       * @return string
  64       */
  65  
  66      public function __toString()
  67      {
  68          return (string)$this->toString();
  69      }
  70  
  71      /**
  72       * Converts a callable to a string presentation.
  73       *
  74       * If the callable is an object, returns the class name. For a callable
  75       * array of object and method, a 'class::staticMethod' or a 'class->method',
  76       * and for functions the name.
  77       *
  78       * <code>
  79       * echo (string) Txp::get('\Textpattern\Type\TypeCallable', function () {return 'Hello world!';});
  80       * </code>
  81       *
  82       * Returns 'Closure'.
  83       *
  84       * <code>
  85       * echo (string) Txp::get('\Textpattern\Type\TypeCallable', array('DateTimeZone', 'listAbbreviations'));
  86       * </code>
  87       *
  88       * Returns 'DateTimeZone::listAbbreviations'.
  89       *
  90       * <code>
  91       * echo (string) Txp::get('\Textpattern\Type\TypeCallable', array(new DateTime(), 'setTime'));
  92       * </code>
  93       *
  94       * Returns 'DateTime->setTime'.
  95       *
  96       * <code>
  97       * echo (string) Txp::get('\Textpattern\Type\TypeCallable', 'date');
  98       * </code>
  99       *
 100       * Returns 'date'.
 101       *
 102       * <code>
 103       * echo (string) Txp::get('\Textpattern\Type\TypeCallable', 1);
 104       * </code>
 105       *
 106       * Returns ''.
 107       *
 108       * @return string The callable as a human-readable string
 109       */
 110  
 111      public function toString()
 112      {
 113          $callable = $this->callable;
 114  
 115          if (is_object($callable)) {
 116              return get_class($callable);
 117          }
 118  
 119          if (is_array($callable)) {
 120              $class = array_shift($callable);
 121              $separator = '::';
 122  
 123              if (is_object($class)) {
 124                  $class = get_class($class);
 125                  $separator = '->';
 126              }
 127  
 128              array_unshift($callable, $class);
 129  
 130              return implode($separator, array_filter($callable, 'is_scalar'));
 131          }
 132  
 133          if (!is_string($callable)) {
 134              return '';
 135          }
 136  
 137          return $callable;
 138      }
 139  }

title

Description

title

Description

title

Description

title

title

Body