Source for file Buffers.php

Documentation is available at Buffers.php

  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fdm=marker encoding=utf8 :
  3. /**
  4.  * Pxxo - build self-supported and interoperable Web graphical components
  5.  * 
  6.  * Copyright (c) 2008, Nicolas Thouvenin
  7.  *
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  *
  13.  *     * Redistributions of source code must retain the above copyright
  14.  *       notice, this list of conditions and the following disclaimer.
  15.  *     * Redistributions in binary form must reproduce the above copyright
  16.  *       notice, this list of conditions and the following disclaimer in the
  17.  *       documentation and/or other materials provided with the distribution.
  18.  *     * Neither the name of the author nor the names of its contributors may be
  19.  *       used to endorse or promote products derived from this software without
  20.  *       specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25.  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
  26.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33.  * @package    Pxxo
  34.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  35.  * @license    http://opensource.org/licenses/bsd-license.php
  36.  * @version    $Id$
  37.  */
  38. require_once 'Pxxo.php';
  39. /**
  40. * Classe permet de stocker des Objets Pxxo_Buffers
  41.  *
  42.  * @package    Pxxo
  43.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  44.  * @license    http://opensource.org/licenses/bsd-license.php
  45.  */
  46.  class Pxxo_Buffers extends Pxxo implements CountableIterator
  47. {
  48.     /**
  49.      * Iteration index
  50.      *
  51.      * @var integer 
  52.      */
  53.     protected $_index;
  54.  
  55.  
  56.     /**
  57.      * Contains array of configuration data
  58.      *
  59.      * @var array 
  60.      */
  61.      protected $_data;
  62.  
  63.      /**
  64.      * Reprsentation trié de _data
  65.      *
  66.      * @var array 
  67.      */
  68.      private $_sort;
  69.  
  70.  
  71.     /**
  72.     * Constructeur
  73.     */
  74.     public function __construct()
  75.     {
  76.         parent::__construct();
  77.         $this->_index = 0;
  78.         $this->_data = array();
  79.         $this->_sort = array();
  80.     }
  81.  
  82.     /**
  83.      * Test si l'objet est un objet de type Pxxo_Buffer
  84.      * 
  85.      * @return  Pxxo_Buffer 
  86.      */
  87.  
  88.     public function isBuffer($o
  89.     {
  90.         if (is_null($oor !is_object($o) ) return false;
  91.         if ($o instanceof Pxxoreturn false;
  92.         if (!isset($o->schemeor !isset($o->uri)) return false;
  93.         return true;
  94.     }
  95.  
  96.      /**
  97.      * Recupere un Buffer particulier de la liste
  98.      * 
  99.      * @return  Pxxo_Buffer 
  100.      */
  101.     public function get($i
  102.     {
  103.         if (isset($this->_data[$i])) return $this->_data[$i];
  104.         else return null;
  105.     }
  106.  
  107.     /**
  108.      * Magic function so that $obj->value will work.
  109.      *
  110.      * @param string 
  111.      * @return mixed 
  112.      */
  113.     public function __get($i)
  114.     {
  115.         return $this->get($i);
  116.     }
  117.  
  118.  
  119.     /**
  120.      * Ajout d'un Buffer
  121.      *
  122.      * @param Pxxo_Buffer 
  123.      * @return integer indice
  124.      */
  125.     public function add($o)
  126.     {
  127.         if (!$this->isBuffer($o)) return false;
  128.  
  129.         $i $o->getID();
  130.         $this->_data[$i$o;
  131.         $this->_sort[$i$o->getWeight();
  132.  
  133.         return $i;
  134.     }
  135.  
  136.  
  137.     /**
  138.      * Defined by Countable interface
  139.      *
  140.      * @return int 
  141.      */
  142.     public function count()
  143.     {
  144.         return count($this->_data);
  145.     }
  146.  
  147.     /**
  148.      * Defined by Iterator interface
  149.      *
  150.      * @return mixed 
  151.      */
  152.     public function current()
  153.     {
  154.         $k key($this->_sort);
  155.         return($this->_data[$k]);
  156.     }
  157.  
  158.     /**
  159.      * Defined by Iterator interface
  160.      *
  161.      * @return mixed 
  162.      */
  163.     public function key()
  164.     {
  165.         return key($this->_sort);
  166.     }
  167.  
  168.     /**
  169.      * Defined by Iterator interface
  170.      *
  171.      */
  172.     public function next()
  173.     {
  174.         next($this->_sort);
  175.         $this->_index++;
  176.     }
  177.  
  178.     /**
  179.      * Defined by Iterator interface
  180.      *
  181.      */
  182.     public function rewind()
  183.     {
  184.         reset($this->_data);
  185.         $this->_index = 0;
  186.         $this->_sort = array();
  187.         foreach($this->_data as $k => $o{
  188.             $this->_sort[$k$o->getWeight();
  189.         }
  190.         arsort($this->_sortSORT_NUMERIC);
  191.     }
  192.  
  193.     /**
  194.      * Defined by Iterator interface
  195.      *
  196.      * @return boolean 
  197.      */
  198.     public function valid()
  199.     {
  200.         return $this->_index < $this->count();
  201.     }
  202.  
  203.     /**
  204.      * Merge une liste de Buffers avec la liste courante,
  205.      * les entrées en communs sont ignorés.
  206.      *
  207.      * @param Pxxo_Buffers $merge 
  208.      * @return Pxxo_Buffers 
  209.      */
  210.      public function merge(Pxxo_Buffers $merge)
  211.     {
  212.         foreach($merge as $key => $item{
  213.             if (!array_key_exists($key$this->_data)) {
  214.                 $this->add($item);
  215.             }
  216.         }
  217.         return $this;
  218.     }
  219.  
  220.     /**
  221.      * Recherche le Buffer contenant une valeur
  222.      *
  223.      * @param string 
  224.      * @return string 
  225.      */
  226.     public function search($val)
  227.     {
  228.         foreach($this->_data as $k => $vif ($val == $v->get()) return $k;
  229.     }
  230.  
  231.     /**
  232.      * Concatene les Buffers ayant un type etendu identique
  233.      *
  234.      * @param string 
  235.      * @return Pxxo_Buffers 
  236.      */
  237.     public function concat()
  238.     {
  239.         $a array();
  240.         foreach($this->_data as $cle => $buf{
  241.             $extyp $buf->getExtendedType();
  242.             if (!isset($a[$extyp])) $a[$extyp$buf;
  243.             else {
  244.                 $a[$extyp]->merge($buf);
  245.                 unset($this->_data[$cle]);
  246.             }
  247.         }
  248.     }
  249.  
  250.  
  251.     /**
  252.      * N'en garder qu'un seul, supprime tout les autres...
  253.      *
  254.      * @param string 
  255.      * @return Pxxo_Buffer 
  256.      */
  257.     public function keepOnlyOne($i
  258.     {
  259.         foreach($this->_data as $k => $vif ($k != $iunset($this->_data[$k])
  260.     }
  261. }

Documentation generated on Thu, 13 Mar 2008 22:03:11 +0100 by phpDocumentor 1.4.1