PHP Gruppierte Listen / Gruppen


<?php

/**
 * Class Groupable:
 * Universelle Klasse zum gruppieren von Typen
 * 
 * 
 * @package lib\universal_modell
 */
class GroupList implements IteratorAggregate
{


    /**
     * @var array
     */
    private $group;


    /**
     * Groupable constructor.
     *
     * @param array $list_of_names
     */
    public function __construct ( $list_of_names = [] ){

        $this -> group = $list_of_names;
    }


    /**
     * @return ArrayIterator
     */
    public function getIterator (){
        return new ArrayIterator( $this -> group  );
    }


    /**
     * @param string|array $name 
     *
     * @return bool
     */
    public function add( $name ){

		if ( 'array' === gettype( $name ) ){
			$this -> add_list( $name );
		}

        if ( 
			'string' === gettype( $name ) 
			&& 
			false === $this -> exsists( $name ) 
		   ){
            array_push( $this -> group , $name );
            return TRUE;
        }

        return FALSE;
    }


	private function add_list( $name_list ){

		foreach( $name_list as $name ){
			$this -> add( $name );
		}

	}


    /**
     * Lösch den Inhalt der Dateiliste
     */
    public function clear_all(){

        $this -> group = [];

    }

    
    
    /**
     * Löscht den angegebenen Gruppennamen aus der Gruppenliste
     * 
     * @param $name
     *
     * @return bool
     */
    public function del( $name ){
        
		$pos = array_search( $name , $this -> group , true );

        if( false !== $pos ){
            unset( $this -> group[ $pos ] );
            return TRUE;
        }
        
        return FALSE;
       
    }
    

    /**
     * Prüfen strikt ob der Gruppenname existiert
     * 
     * @param $name
     *
     * @return bool
     */
    public function exsists( $name ){

		if( 'string' === gettype( $name ) ) {
	        return in_array( $name , $this -> group , true );    
		}

		return false;

    }
    
    
    
}




$group = new GroupList();

$group -> add( 'hans' );
$group -> add( 'wurst' );
$group -> del( 'hans' );
$group -> add( ['2','1',null,false,0,1] );



echo PHP_EOL;

foreach( $group as $item ){
	print_r( $item . PHP_EOL );
}


print_r(
	
	$group -> exsists( 1 ) ? 'ja' : 'nein'
	
);


?>