Source for file REPFileItems.class.php

Documentation is available at REPFileItems.class.php

  1. <?php
  2. /**
  3.  * Módosítva: 2010.03.06.
  4.  *
  5.  * @author Takács Ákos (Rimelek), programmer [at] rimelek [dot] hu
  6.  * @copyright Copyright (C) 2010, Takács Ákos
  7.  * @version 1.0
  8.  * @package REPagination
  9.  */
  10.  
  11. /**
  12.  * @ignore
  13.  */
  14. require_once dirname(__FILE__).'/../REPClasses.class.php';
  15. /*
  16.  * AREPItems importálása
  17.  */
  18. REPClasses::import('AREPItems');
  19. /**
  20.  * Egyszerű, szöveges fájlok tartalmát iterálhatóvá tevő osztály.
  21.  * Egy sor egy rekord. És egy rekordban a mezőket tetszőlegesen
  22.  * megadható karaktersor választja el.
  23.  *
  24.  * @author Takács Ákos (Rimelek), programmer [at] rimelek [dot] hu
  25.  * @copyright Copyright (C) 2010, Takács Ákos
  26.  * @package REPagination
  27.  */
  28. class REPFileItems extends AREPItems
  29. {
  30.     /**
  31.      * A fájlban levő rekordok tömbje. Két dimenziós.
  32.      * @var array 
  33.      */
  34.     protected $items = array();
  35.     /**
  36.      * A mezőket elválasztó karaktersor
  37.      * @var string 
  38.      */
  39.     private $sep '&&';
  40.     /**
  41.      * Beolvasandó fájl neve
  42.      * @var string 
  43.      */
  44.     protected $filename = null;
  45.  
  46.     /**
  47.      * @see init()
  48.      *
  49.      * @param mixed $file Ha string, akkor a {@link $filename fájlnevet} állítja be.
  50.      *         Ha tömb, akkor rögtön az {@link $items} tulajdonságot.
  51.      *         Így akár előre beolvasott tartalmat, vagy tömböt is
  52.      *         lehet lapozni vele.
  53.      * @param array $indexes Lekérdezendő mezők listája Részletek az {@link init()} metódusnál.
  54.      * @param string $sep Mezőket elválasztó karaktersor.
  55.      */
  56.     public function __construct($file,$indexes=array()$sep='&&')
  57.     {
  58.         if (is_string($file))
  59.         {
  60.             $this->filename = $file;
  61.         }
  62.         else if (is_array($file))
  63.         {
  64.             $this->items = $file;
  65.         }
  66.         if (count($this->items)) return;
  67.         $this->init($indexes$sep);
  68.     }
  69.     /**
  70.      * Beolvassa a megfelelő fájlt, ha szükséges
  71.      * és beállítja az indexeit a mezőknek.
  72.      *
  73.      * @param array $indexes Két dimenziós tömb, amiben megadható,
  74.      *         hogy melyik oszlopnak mi legyen az asszociatív indexe.
  75.      *         Az eredeti tömbben azonos indexű elem új neve lesz az
  76.      *         $indexes adott eleme. Ezzel sorrend is változtatható, ha megadjuk
  77.      *         kézzel az indexeket a tömbben.
  78.      * @param string $sep Mezőket elválasztó karaktersor.
  79.      */
  80.     protected function init($indexes=array(),$sep '&&')
  81.     {
  82.         $this->sep $sep;
  83.         if (!is_null($this->filenameand file_exists($this->filename))
  84.         {
  85.             $this->items = file($this->filename);
  86.         }
  87.  
  88.         if (isset($this->items[0]and !is_array($this->items[0]))
  89.         {
  90.             foreach ($this->items as &$item)
  91.             {
  92.                 $item explode($sep$item);
  93.                 if (!is_array($indexesor !count($indexes))
  94.                 {
  95.                     continue;
  96.                 }
  97.                 $tmp array();
  98.                 foreach ($indexes as $key => $value)
  99.                 {
  100.                     if (!isset($item[$key])) continue;
  101.                     $tmp[$value$item[$key];
  102.                 }
  103.                 $item $tmp;
  104.             }
  105.         }
  106.     }
  107.  
  108.     /**
  109.      * Létrehoz egy új azonos típusú példányt, mint az objektum, amin hívjuk a metódust.
  110.      *
  111.      * @param array $items Az új példány {@link $items} tulajdonsága ezt fogja tartalmazni
  112.      * @return REPFileItems A leszármaztatott osztályokból származtatott
  113.      *         objektumok ezen metódusa az adott osztály típusú objektummal
  114.      *         tér vissza.
  115.      */
  116.     public function newInstance($items)
  117.     {
  118.         $ref new ReflectionObject($this);
  119.         $obj $ref->newInstance($this->filename);
  120.         $props $ref->getProperties();
  121.         $obj->items $items;
  122.         foreach ($props as  $prop => $value)
  123.         {
  124.             if ($prop == 'items'continue;
  125.             $obj->$prop $value;
  126.         }
  127.         
  128.         return $obj;
  129.     }
  130.  
  131.     /**
  132.      * @ignore
  133.      */
  134.     public function offsetExists($offset)
  135.     {
  136.         return isset($this->items[$offset]);
  137.     }
  138.     /**
  139.      * @ignore
  140.      */
  141.     public function offsetUnset($offset)
  142.     {
  143.         unset($this->items[$offset]);
  144.     }
  145.     /**
  146.      * @ignore
  147.      */
  148.     public function offsetGet($offset)
  149.     {
  150.         return isset($this->items[$offset]$this->items[$offsetnull;
  151.     }
  152.  
  153.     /**
  154.      * @ignore
  155.      */
  156.     public function offsetSet($offset$value)
  157.     {
  158.         $this->items[$offset$value;
  159.     }
  160.  
  161.     /**
  162.      * {@link $items} tömb mérete
  163.      * @return int 
  164.      */
  165.     public function count()
  166.     {
  167.         return count($this->items);
  168.     }
  169.  
  170.     /**
  171.      * @ignore
  172.      */
  173.     public function current()
  174.     {
  175.         return current($this->items);
  176.     }
  177.  
  178.     /**
  179.      * @ignore
  180.      */
  181.     public function key()
  182.     {
  183.         return key($this->items);
  184.     }
  185.  
  186.     /**
  187.      * @ignore
  188.      */
  189.     public function next()
  190.     {
  191.         next($this->items);
  192.     }
  193.  
  194.     /**
  195.      * @ignore
  196.      */
  197.     public function rewind()
  198.     {
  199.         reset($this->items);
  200.     }
  201.  
  202.     /**
  203.      * @ignore
  204.      */
  205.     public function valid()
  206.     {
  207.         return key($this->items!== null;
  208.     }
  209. }
  210. ?>

Documentation generated on Sat, 06 Mar 2010 22:00:16 +0100 by phpDocumentor 1.4.1