public function randomGet( $n = null, $returnContainer = false, $removeFromContainer = false )
    {
        $n = $n === null ? 1 : (int) $n;

        // anything to give?
        if( 0 === $containerSize = $this->count() ) {
            if( $returnContainer or $n > 1 ) {
                return $this->newContainer();
            }
            return null;
        }

        // single entity
        if( $n === 1 ) {

            $key = array_rand( $this->collection );
            $entity = $this->collection[$key];

            if( $removeFromContainer ) {
                unset( $this->collection[$key] );
            }

            return $returnContainer
                ? $this->newContainer( $entity )
                : $entity
                ;

        }

        $output = $this->newContainer();

        // to protected against the peculiar, $n = 0 still returns one element
        if( $n > 1 ) {
            // asking for more than we've got?
            if( $n <= $containerSize ) {
                $keys = array_flip( array_rand( $this->collection, $n ) );
                $output->add( array_intersect_key( $this->collection, $keys ) );
                if( $removeFromContainer ) {
                    $this->collection = array_diff_key( $this->collection, $keys );
                }
            } else {
                $output->add( $this );
                if( $removeFromContainer ) {
                    $this->truncate();
                }
            }
        }
        return $output;
    }
public function randomGet( $n = null, $returnContainer = false, $removeFromContainer = false )
    {
        $n = $n === null ? 1 : (int) $n;

        // anything to give?
        if( 0 === $containerSize = $this->count() ) {
            if( $returnContainer or $n > 1 ) {
                return $this->newContainer();
            }
            return null;
        }

        // single entity
        if( $n === 1 ) {

            $key = array_rand( $this->collection );
            $entity = $this->collection[$key];

            if( $removeFromContainer ) {
                unset( $this->collection[$key] );
            }

            return $returnContainer
                ? $this->newContainer( $entity )
                : $entity
                ;

        }

        $output = $this->newContainer();

        // to protected against the peculiar, $n = 0 still returns one element
        if( $n > 1 ) {
            // asking for more than we've got?
            if( $n <= $containerSize ) {
                $keys = array_flip( array_rand( $this->collection, $n ) );
                $output->add( array_intersect_key( $this->collection, $keys ) );
                if( $removeFromContainer ) {
                    $this->collection = array_diff_key( $this->collection, $keys );
                }
            } else {
                $output->add( $this );
                if( $removeFromContainer ) {
                    $this->truncate();
                }
            }
        }
        return $output;
    }
public function randomGet ( $ n = null , $ returnContainer = false , $ removeFromContainer = false ) { $ n = $ n === null ? 1 : ( int ) $ n ; // anything to give? if ( 0 === $ containerSize = $ this -> count ( ) ) { if ( $ returnContainer or $ n > 1 ) { return $ this -> newContainer ( ) ; } return null ; } // single entity if ( $ n === 1 ) { $ key = array_rand ( $ this -> collection ) ; $ entity = $ this -> collection [ $ key ] ; if ( $ removeFromContainer ) { unset ( $ this -> collection [ $ key ] ) ; } return $ returnContainer ? $ this -> newContainer ( $ entity ) : $ entity ; } $ output = $ this -> newContainer ( ) ; // to protected against the peculiar, $n = 0 still returns one element if ( $ n > 1 ) { // asking for more than we've got? if ( $ n <= $ containerSize ) { $ keys = array_flip ( array_rand ( $ this -> collection , $ n ) ) ; $ output -> add ( array_intersect_key ( $ this -> collection , $ keys ) ) ; if ( $ removeFromContainer ) { $ this -> collection = array_diff_key ( $ this -> collection , $ keys ) ; } } else { $ output -> add ( $ this ) ; if ( $ removeFromContainer ) { $ this -> truncate ( ) ; } } } return $ output ; }
Return a random number of entities from this collection.
If n = 1 you will recieve a entity otherwise you will get a container
@param int
@param bool Force a container to always be returned
@param bool Remove the items from the container as they are returned
@return ContainerableInterface|Container
Return a random number of entities from this collection . If n = 1 you will recieve a entity otherwise you will get a container