PhpMyObject
[ class tree: PhpMyObject ] [ index: PhpMyObject ] [ all elements ]

Source for file PMO_MyArray.php

Documentation is available at PMO_MyArray.php

  1. <?php
  2. /**
  3.  * This file contains PMO_MyConfig class which is used to store
  4.  * the PhpMyObject configuration.
  5.  *
  6.  * This file is part of the PhpMyObject project.
  7.  * 
  8.  * For questions, help, comments, discussion, etc., please join our
  9.  * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
  10.  * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
  11.  *
  12.  * PhpMyObject is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24.  *
  25.  * @package        PhpMyObject
  26.  * @subpackage PMO_Core
  27.  * @author        Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  28.  * @link            http://pmo.developpez.com/
  29.  * @since        PhpMyObject v0.14
  30.  * @version        $Revision: $
  31.  * @copyright    Copyright (C) 2007-2008 Nicolas Boiteux
  32.  * @license        GPLv3 {@link http://www.gnu.org/licenses/gpl}
  33.  * @filesource
  34.  *
  35.  */ 
  36.  
  37. /**
  38.  * PMO_MyArray adds iterators to PMO stores.
  39.  *
  40.  * @package        PhpMyObject
  41.  * @subpackage PMO_Core
  42.  */
  43. class PMO_MyArray implements Countable,Iterator{
  44.  
  45.     /**
  46.     * holds the PMO objects
  47.     * @var array 
  48.     */
  49.     protected $array = array();
  50.  
  51.     public function append($value){
  52.         $this->array[$value;
  53.     }    
  54.     
  55.     public function add($key$value){
  56.         $this->array[$key$value;
  57.     }
  58.     
  59.     public function offsetset($key$value){
  60.         $this->array[$key$value;
  61.     }
  62.     
  63.     public function offsetget($key){
  64.         return $this->array[$key];
  65.     }
  66.     
  67.     public function offsetExists($key){
  68.         if(isset($this->array[$key]))
  69.             return TRUE;
  70.             
  71.         return FALSE;
  72.     }
  73.     
  74.     public function arsort(int $sort_flags NULL){
  75.         return asort($this->array$sort_flags);
  76.     }
  77.     
  78.     public function asort(int $sort_flags NULL){
  79.         return asort($this->array,$sort_flags);
  80.     }
  81.         
  82.     public function change_key_case(){
  83.         $this->array = array_change_key_case($this->array);
  84.     }
  85.  
  86.     /**
  87.     * already taken care by ArrayIterator as count();
  88.     */
  89.     public function count_values(){
  90.         return array_count_values($this->array);
  91.     }
  92.     
  93.     public function count(int $mode NULL){
  94.         return count($this->array$mode);
  95.     }
  96.  
  97.     public function current(){
  98.         return current($this->array);
  99.     }
  100.         
  101.     public function each(){
  102.         if($this->valid()){
  103.             $value array($this->key()=>$this->current());
  104.             $this->next();
  105.             return $value;
  106.         }else{
  107.             return NULL;
  108.         }
  109.     }
  110.     
  111.     public function end(){
  112.         return end($this->array);
  113.     }
  114.     
  115.     public function get($key){
  116.         return $this->array[$key];
  117.     }
  118.     
  119.     public function in_array(mixed $needlearray $haystackbool $strict NULL){
  120.         return in_array($needle$this->array$strict);
  121.     }
  122.  
  123.     public function implode($char){
  124.         return implode($char$this->array);
  125.     }
  126.     
  127.     public function key(){
  128.         return key($this->array);
  129.     }
  130.     
  131.     /**
  132.     * return true if the key exists, false otherwise
  133.     *
  134.     * Note  l'array est indexé numériquement. Donc la clé est un entier,
  135.     *       à moins d'utiliser un map hash comme dans MyMapHash?
  136.     *
  137.     * @return bool      true if the key exists, false otherwise
  138.     */
  139.     public function key_existsmixed $key){
  140.           return array_key_exists($key$this->array);
  141.     }    
  142.     
  143.     public function krsort(int $sort_flags NULL){
  144.         return krsort($this->array$sort_flags);
  145.     }
  146.     
  147.     public function ksort(int $sort_flags NULL){
  148.         return ksort($this->array$sort_flags);
  149.     }
  150.  
  151.     public function fetch(){
  152.         if($this->valid(== TRUE){
  153.             $value $this->current();
  154.             $this->next();
  155.             return $value;
  156.         }else{
  157.             return NULL;
  158.         }
  159.     }
  160.         
  161.     public function flip(){
  162.         $this->array array_flip($this->array);
  163.     }
  164.     
  165.     public function multisort(){
  166.         return $this->array array_multisort($this->array);
  167.     }
  168.  
  169.     /**
  170.     * merge any number of array to the main array
  171.     *
  172.     * arrays must be of the type provided when the object was istanciated
  173.     *
  174.     * @var array $arg,...     any number of array to merge
  175.     */
  176.     public function merge($arg){
  177.     if (count($arg0){
  178.         $this->array array_merge($this->array$arg);
  179.     }
  180.     else
  181.         throw new Exception("Error: No argument provided");
  182.     }
  183.  
  184.     public function natcasesort(){
  185.         $this->array natcasesort($this->array);
  186.     }
  187.     
  188.     public function natsort(){
  189.         $this->array natsort($this->array);
  190.     }
  191.     
  192.     public function next(){
  193.         return next($this->array);    
  194.     }    
  195.     
  196.     public function pad(int $pad_sizemixed $pad_value){
  197.         return array_pad($this->array$pad_size$pad_value);
  198.     }
  199.  
  200.     /**
  201.     * returns the last element from the array and removes it
  202.     *
  203.     * @return object 
  204.     */
  205.     public function pop(){
  206.         return array_pop($this->array);
  207.     }
  208.  
  209.     public function product(){
  210.         return array_product($this->array);
  211.     }
  212.  
  213.     public function prev(){
  214.         return prev($this->array);
  215.     }    
  216.     
  217.     /**
  218.     * adds an object to the internal store
  219.     *
  220.     * @param object $var 
  221.     */
  222.     public function push($var){
  223.         if(isset($this->instanceof))
  224.             if($var instanceof $this->instanceof)
  225.                 $this->array[$var;
  226.             else
  227.                 throw new Exception("Error of type");
  228.     }
  229.     
  230.     public function rand(int $num_req){
  231.         return array_rand($this->array$num_req);
  232.     }
  233.  
  234.     public function reverse(bool $preserve_keys NULL){
  235.         $this->array array_reverse($this->array$preserve_keys);
  236.     }
  237.  
  238.     public function reset(){
  239.         return reset($this->array);
  240.     }    
  241.     
  242.     public function unique(){
  243.         $this->array array_unique($this->array);
  244.     }
  245.  
  246.     /**
  247.     * prepends a new object to the store
  248.     *
  249.     * @var object $var 
  250.     */
  251.     public function unshift(Object $var){
  252.         return array_unshift($this->array$var);
  253.     }
  254.  
  255.     public function values(){
  256.         return array_values($this->array);
  257.     }
  258.     
  259.     public function rewind(){
  260.         return $this->reset();
  261.     }    
  262.     
  263.     public function rsort(int $sort_flags NULL){
  264.         return rsort($this->array$sort_flags);
  265.     }
  266.  
  267.     public function shuffle(){
  268.         return shuffle($this->array);
  269.     }
  270.  
  271.     public function search(mixed $needlebool $strict NULL){
  272.         return array_search($needle,$strict);
  273.     }
  274.  
  275.     /**
  276.     * returns an object from the beginnig if the store and remvoes it
  277.     *
  278.     * @return object 
  279.     */
  280.     public function shift(){
  281.         return array_shift($this->array);
  282.     }
  283.     
  284.     public function slice(int $offset,int $length NULLbool $preserve_keys NULL){
  285.         return array_slice($this->array$offset$length$preserve_keys);
  286.     }
  287.     
  288.     public function sum(){
  289.         return array_sum($this->array);
  290.     }    
  291.     
  292.     public function sort(int $sort_flags NULL){
  293.         return sort($this->array$sort_flags);
  294.     }    
  295.     
  296.     public function valid(){
  297.         if($this->current())
  298.             return TRUE;
  299.         
  300.         $this->rewind();
  301.         return FALSE;
  302.     }
  303. }
  304.  
  305. ?>

Documentation generated on Wed, 15 Oct 2008 09:16:44 -0400 by phpDocumentor 1.4.1