09 November 2009

StringList class and use of it

This class can be used to manage lists of strings. It can take a list of strings separated by line breaks and extracts key value pairs separated by the = character.The class can return the list of strings as an array, add more strings to the list, and save or load the list from a file.

stringlist_class.php


class StringList{
  private $Values=array();
  private $Text;
  private $Lines=array();
  /**
  * It's constructor func
  *
  * @param mixed $string String you want parse.
  * @return StringList
  */
  public function StringList($string){

    $this->Text=$string;
    $this->Lines=explode("\n",$string);
    $Count=count($this->Lines);

    Foreach ($this->Lines As $LineText){
      $EsitPos=strpos($LineText,'=');
      $this->Values[substr($LineText,0,$EsitPos)]=substr($LineText,$EsitPos+1,strlen($LineText));
    }

    return $this;
  }
  /**
  * Return The Text Or Set Text (ReCreate Object).
  *
  * @param mixed $SetTex it will change if it assigned
  * @return StringList
  */
  public function Text($SetTex=False){
    if($SetTex){
      return $this->StringList($SetTex);
    } else {
      return $this->Text;
    }
  }
  /**
  * It can return A Line or All Lines Or Set The Lines ( ReCreate Object )
  *
  * @param mixed $LinesArray if is numeric it will return Line with this number .
  * @return StringList
  */
  public function Lines($LinesArray=false){
    if(is_array($LinesArray)){
      return $this->StringList(implode("\n",$LinesArray));
    } elseif(is_numeric($LinesArray)) {
      return $this->Lines[$LinesArray];
    }else {
      return $this->Lines;
    }
  }
  /**
  * You Can add Line Or Lines by Array.
  * @param mixed $LineOrLines
  */
  public function Add($LineOrLines){
    if(strstr($LineOrLines,"\n")){
      $Lines=explode("\n",$LineOrLines);
    }else {
      $Lines[]=$LineOrLines;
    }

    Foreach($Lines As $Line){
      $this->Lines[]=$Line;
    }
   return $this;
  }
  /**
  * Save Text to File.
  * @param mixed $FileName Dosya Adı
  */
  public function SaveToFile($FileName='UnnamedStringlistFile'){
    if($fileOpened=@fopen($FileName,'w+')){
      fwrite($this->Text);
      fclose($fileOpened);
    }
  }
  /**
  * Read Text From File (Recreate Object)
  *
  * @param mixed $FileName
  */
  public function ReadFromFile($FileName){
    if(is_file($FileName) && is_readable($FileName)){
      $this->StringList(file_get_contents($FileName));
    }
    return $this;
  }
  /**
  * PROPERTIES
  */
  public function Values($Name=false,$SetValue=False){
    if($Name){
      if($SetValue){
        foreach($this->Values AS $valName=>$valValue){
        $Lines[]=$valName.'='.$valValue."\n";
      }
      $this->Lines($Lines);
      } else {
        return $this->Values[$Name];
      }
    } else {
      return $this->Values;
    }
  }
  public function Count(){
    return Count($this->Lines);
  }

  /**
  * This func is only for show ->Lines()->Add() etc on PHP Editores
  *
  */
  private function ForPhpEditores(){
    die('Don\'t Use This Function.!');
    $this->Text=new StringList('');
    $this->Lines=new StringList('');
  }
} 

example.php


include 'stringlist_class.php';
$str=new Stringlist("Key=Value\nKey2=Value2");

echo $str->Values('Key2'); // return Value2
$str->SaveToFile('file.txt'); //will write Key=Value\nKey2=Value2 2 lines on file.txt
$str->Count(); // return 2
$str->Text(); //Return All Text
$str->Text('new Text=asdasd'); // Set The Text
$str->Lines()->Add('newline=newValue'); // Add a New Line. 


No comments:

Post a Comment