Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/utilities/array.php

Documentation is available at array.php

  1. <?php
  2. /**
  3.  * @version        $Id: array.php 6150 2007-01-02 04:53:35Z Jinx $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Utilities
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant to the
  9.  *  GNU General Public License, and as distributed it includes or is derivative
  10.  *  of works licensed under the GNU General Public License or other free or open
  11.  *  source software licenses. See COPYRIGHT.php for copyright notices and
  12.  *  details.
  13.  */
  14.  
  15. /**
  16.  * JArrayHelper is an array utility class for doing all sorts of odds and ends with arrays.
  17.  *
  18.  * @static
  19.  * @author        Louis Landry <[email protected]>
  20.  * @package     Joomla.Framework
  21.  * @subpackage        Utilities
  22.  * @since        1.5
  23.  */
  24. {
  25.     /**
  26.      * Function to convert array to integer values
  27.      *
  28.      * @static
  29.      * @param    array    $array        The source array to convert
  30.      * @param    int        $default    A default value to assign if $array is not an array
  31.      * @return    array 
  32.      * @since    1.5
  33.      */
  34.     function toInteger&$array$default null)
  35.     {
  36.         if (is_array($array)) {
  37.             foreach ($array as $i => $v{
  38.                 $array[$iintval($v);
  39.             }
  40.         else {
  41.             if (is_null($default)) {
  42.                 return array();
  43.             else {
  44.                 return array($default);
  45.             }
  46.         }
  47.     }
  48.  
  49.     /**
  50.      * Utility function to map an array to a stdClass object.
  51.      *
  52.      * @static
  53.      * @param    array    $array        The array to map.
  54.      * @param    boolean    $jobject    Optionally create a JObject
  55.      * @return    object    The object mapped from the given array
  56.      * @since    1.5
  57.      */
  58.     function toObject(&$array$toJObject false)
  59.     {
  60.         $obj null;
  61.         if (is_array($array))
  62.         {
  63.             $obj $toJObject new JObject(new stdClass();
  64.             foreach ($array as $k => $v)
  65.             {
  66.                 if (is_array($v)) {
  67.                     $obj->$k JArrayHelper::toObject($v$toJObject);
  68.                 else {
  69.                     $obj->$k $v;
  70.                 }
  71.             }
  72.         }
  73.         return $obj;
  74.     }
  75.  
  76.     /**
  77.      * Utility function to map an object to an array
  78.      *
  79.      * @static
  80.      * @param    object    The source object
  81.      * @param    boolean    True to recurve through multi-level objects
  82.      * @param    string    An optional regular expression to match on field names
  83.      * @return    array    The array mapped from the given object
  84.      * @since    1.5
  85.      */
  86.     function fromObject$p_obj$recurse true$regex null )
  87.     {
  88.         $result null;
  89.         if (is_object$p_obj ))
  90.         {
  91.             $result array();
  92.             foreach (get_object_vars($p_objas $k => $v)
  93.             {
  94.                 if ($regex)
  95.                 {
  96.                     if (!preg_match$regex$k ))
  97.                     {
  98.                         continue;
  99.                     }
  100.                 }
  101.                 if (is_object$v ))
  102.                 {
  103.                     if ($recurse)
  104.                     {
  105.                         $result[$kJArrayHelper::fromObject$v$recurse$regex );
  106.                     }
  107.                 }
  108.                 else
  109.                 {
  110.                     $result[$k$v;
  111.                 }
  112.             }
  113.         }
  114.         return $result;
  115.     }
  116.  
  117.     /**
  118.      * Extracts a column from an array of arrays or objects
  119.      *
  120.      * @static
  121.      * @param    array    $array    The source array
  122.      * @param    string    $index    The index of the column or name of object property
  123.      * @return    array    Column of values from the source array
  124.      * @since    1.5
  125.      */
  126.     function getColumn(&$array$index)
  127.     {
  128.         $result array ();
  129.  
  130.         if (is_array($array))
  131.         {
  132.             $n count($array);
  133.             for ($i 0$i $n$i++)
  134.             {
  135.                 $item $array[$i];
  136.                 if (is_array($item&& isset ($item[$index])) {
  137.                     $result[$item[$index];
  138.                 elseif (is_object($item&& isset ($item-> $index)) {
  139.                     $result[$item-> $index;
  140.                 }
  141.                 // else ignore the entry
  142.             }
  143.         }
  144.         return $result;
  145.     }
  146.  
  147.     /**
  148.      * Utility function to return a value from a named array or a specified default
  149.      *
  150.      * @static
  151.      * @param    array    $array        A named array
  152.      * @param    string    $name        The key to search for
  153.      * @param    mixed    $default    The default value to give if no key found
  154.      * @param    string    $type        Return type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY)
  155.      * @return    mixed    The value from the source array
  156.      * @since    1.5
  157.      */
  158.     function getValue(&$array$name$default=null$type='')
  159.     {
  160.         // Initialize variables
  161.         $result null;
  162.  
  163.         if (isset ($array[$name])) {
  164.             $result $array[$name];
  165.         }
  166.  
  167.         // Handle the default case
  168.         if ((is_null($result))) {
  169.             $result $default;
  170.         }
  171.  
  172.         // Handle the type constraint
  173.         switch (strtoupper($type))
  174.         {
  175.             case 'INT' :
  176.             case 'INTEGER' :
  177.                 // Only use the first integer value
  178.                 preg_match('/-?[0-9]+/'$result$matches);
  179.                 $result (int) $matches[0];
  180.                 break;
  181.  
  182.             case 'FLOAT' :
  183.             case 'DOUBLE' :
  184.                 // Only use the first floating point value
  185.                 preg_match('/-?[0-9]+(\.[0-9]+)?/'$result$matches);
  186.                 $result (float) $matches[0];
  187.                 break;
  188.  
  189.             case 'BOOL' :
  190.             case 'BOOLEAN' :
  191.                 $result = (bool) $result;
  192.                 break;
  193.  
  194.             case 'ARRAY' :
  195.                 if (!is_array($result)) {
  196.                     $result array ($result);
  197.                 }
  198.                 break;
  199.  
  200.             case 'STRING' :
  201.                 $result = (string) $result;
  202.                 break;
  203.  
  204.             case 'WORD' :
  205.                 $result = (string) preg_replace'#\W#'''$result );
  206.                 break;
  207.  
  208.             case 'NONE' :
  209.             default :
  210.                 // No casting necessary
  211.                 break;
  212.         }
  213.         return $result;
  214.     }
  215.  
  216.     /**
  217.      * Utility function to sort an array of objects on a given field
  218.      *
  219.      * @static
  220.      * @param    array    $arr        An array of objects
  221.      * @param    string    $k            The key to sort on
  222.      * @param    int        $direction    Direction to sort in [1 = Ascending] [-1 = Descending]
  223.      * @return    array    The sorted array of objects
  224.      * @since    1.5
  225.      */
  226.     function sortObjects&$a$k$direction=)
  227.     {
  228.         $GLOBALS['JAH_so'array(
  229.             'key'        => $k,
  230.             'direction'    => $direction
  231.         );
  232.         usort$aarray('JArrayHelper''_sortObjects') );
  233.         unset$GLOBALS['JAH_so');
  234.  
  235.         return $a;
  236.     }
  237.  
  238.     /**
  239.      * Private callback function for sorting an array of objects on a key
  240.      *
  241.      * @static
  242.      * @param    array    $a    An array of objects
  243.      * @param    array    $b    An array of objects
  244.      * @return    int        Comparison status
  245.      * @since    1.5
  246.      * @see        JArrayHelper::sortObjects()
  247.      */
  248.     function _sortObjects&$a&$b )
  249.     {
  250.         $params $GLOBALS['JAH_so'];
  251.         if $a->$params['key'$b->$params['key'{
  252.             return $params['direction'];
  253.         }
  254.         if $a->$params['key'$b->$params['key'{
  255.             return -$params['direction'];
  256.         }
  257.         return 0;
  258.     }
  259. }
  260. ?>

Documentation generated on Mon, 05 Mar 2007 20:52:23 +0000 by phpDocumentor 1.3.1