Joomla! Platform 12.1

Source code for file /libraries/joomla/base/object.php

Documentation is available at object.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Base
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Base object class.
  14.  *
  15.  * This class allows for simple but smart objects with get and set methods
  16.  * and an internal error handler.
  17.  *
  18.  * @package     Joomla.Platform
  19.  * @subpackage  Base
  20.  * @since       11.1
  21.  */
  22. class JObject
  23. {
  24.     /**
  25.      * An array of error messages or JExceptions objects.
  26.      *
  27.      * @var    array 
  28.      * @since  11.1
  29.      */
  30.     protected $_errors = array();
  31.  
  32.     /**
  33.      * Class constructor, overridden in descendant classes.
  34.      *
  35.      * @param   mixed  $properties  Either and associative array or another
  36.      *                               object to set the initial properties of the object.
  37.      *
  38.      * @since   11.1
  39.      */
  40.     public function __construct($properties null)
  41.     {
  42.         if ($properties !== null)
  43.         {
  44.             $this->setProperties($properties);
  45.         }
  46.     }
  47.  
  48.     /**
  49.      * Magic method to convert the object to a string gracefully.
  50.      *
  51.      * @return  string  The classname.
  52.      *
  53.      * @since   11.1
  54.      */
  55.     public function __toString()
  56.     {
  57.         return get_class($this);
  58.     }
  59.  
  60.     /**
  61.      * Sets a default value if not alreay assigned
  62.      *
  63.      * @param   string  $property  The name of the property.
  64.      * @param   mixed   $default   The default value.
  65.      *
  66.      * @return  mixed 
  67.      *
  68.      * @since   11.1
  69.      */
  70.     public function def($property$default null)
  71.     {
  72.         $value $this->get($property$default);
  73.         return $this->set($property$value);
  74.     }
  75.  
  76.     /**
  77.      * Returns a property of the object or the default value if the property is not set.
  78.      *
  79.      * @param   string  $property  The name of the property.
  80.      * @param   mixed   $default   The default value.
  81.      *
  82.      * @return  mixed    The value of the property.
  83.      *
  84.      * @since   11.1
  85.      *
  86.      * @see     getProperties()
  87.      */
  88.     public function get($property$default null)
  89.     {
  90.         if (isset($this->$property))
  91.         {
  92.             return $this->$property;
  93.         }
  94.         return $default;
  95.     }
  96.  
  97.     /**
  98.      * Returns an associative array of object properties.
  99.      *
  100.      * @param   boolean  $public  If true, returns only the public properties.
  101.      *
  102.      * @return  array 
  103.      *
  104.      * @since   11.1
  105.      *
  106.      * @see     get()
  107.      */
  108.     public function getProperties($public true)
  109.     {
  110.         $vars get_object_vars($this);
  111.         if ($public)
  112.         {
  113.             foreach ($vars as $key => $value)
  114.             {
  115.                 if ('_' == substr($key01))
  116.                 {
  117.                     unset($vars[$key]);
  118.                 }
  119.             }
  120.         }
  121.  
  122.         return $vars;
  123.     }
  124.  
  125.     /**
  126.      * Get the most recent error message.
  127.      *
  128.      * @param   integer  $i         Option error index.
  129.      * @param   boolean  $toString  Indicates if JError objects should return their error message.
  130.      *
  131.      * @return  string   Error message
  132.      *
  133.      * @since   11.1
  134.      */
  135.     public function getError($i null$toString true)
  136.     {
  137.         // Find the error
  138.         if ($i === null)
  139.         {
  140.             // Default, return the last message
  141.             $error end($this->_errors);
  142.         }
  143.         elseif (!array_key_exists($i$this->_errors))
  144.         {
  145.             // If $i has been specified but does not exist, return false
  146.             return false;
  147.         }
  148.         else
  149.         {
  150.             $error $this->_errors[$i];
  151.         }
  152.  
  153.         // Check if only the string is requested
  154.         if ($error instanceof Exception && $toString)
  155.         {
  156.             return (string) $error;
  157.         }
  158.  
  159.         return $error;
  160.     }
  161.  
  162.     /**
  163.      * Return all errors, if any.
  164.      *
  165.      * @return  array  Array of error messages or JErrors.
  166.      *
  167.      * @since   11.1
  168.      */
  169.     public function getErrors()
  170.     {
  171.         return $this->_errors;
  172.     }
  173.  
  174.     /**
  175.      * Modifies a property of the object, creating it if it does not already exist.
  176.      *
  177.      * @param   string  $property  The name of the property.
  178.      * @param   mixed   $value     The value of the property to set.
  179.      *
  180.      * @return  mixed  Previous value of the property.
  181.      *
  182.      * @since   11.1
  183.      */
  184.     public function set($property$value null)
  185.     {
  186.         $previous = isset($this->$property$this->$property null;
  187.         $this->$property $value;
  188.         return $previous;
  189.     }
  190.  
  191.     /**
  192.      * Set the object properties based on a named array/hash.
  193.      *
  194.      * @param   mixed  $properties  Either an associative array or another object.
  195.      *
  196.      * @return  boolean 
  197.      *
  198.      * @since   11.1
  199.      *
  200.      * @see     set()
  201.      */
  202.     public function setProperties($properties)
  203.     {
  204.         if (is_array($properties|| is_object($properties))
  205.         {
  206.             foreach ((array) $properties as $k => $v)
  207.             {
  208.                 // Use the set function which might be overridden.
  209.                 $this->set($k$v);
  210.             }
  211.             return true;
  212.         }
  213.  
  214.         return false;
  215.     }
  216.  
  217.     /**
  218.      * Add an error message.
  219.      *
  220.      * @param   string  $error  Error message.
  221.      *
  222.      * @return  void 
  223.      *
  224.      * @since   11.1
  225.      */
  226.     public function setError($error)
  227.     {
  228.         array_push($this->_errors$error);
  229.     }
  230.  
  231.     /**
  232.      * Converts the object to a string (the class name).
  233.      *
  234.      * @return  string 
  235.      *
  236.      * @since   11.1
  237.      * @deprecated  12.1    Use magic method __toString()
  238.      * @see         __toString()
  239.      */
  240.     public function toString()
  241.     {
  242.         // @codeCoverageIgnoreStart
  243.         // Deprecation warning.
  244.         JLog::add('JObject::toString() is deprecated.'JLog::WARNING'deprecated');
  245.  
  246.         return $this->__toString();
  247.         // @codeCoverageIgnoreEnd
  248.     }
  249. }
/html>