src/Oss2/Doctrine2/WithPreferences.php
public function addIndexedPreference( $name, $value, $expires = null, $max = 0 )
    {
        // what's the current highest index and how many is there?
        $highest = -1;
        $count = 0;

        foreach( $this->getPreferences() as $pref )
        {
            if( $pref->getName() == $name )
            {
                $count++;

                if( $pref->getIx() > $highest )
                    $highest = $pref->getIx();
            }
        }

        if( $max && ( $count >= $max ) )
            throw new IndexLimitException( 'Requested maximum number of indexed preferences reached' );

        if( is_array( $value ) )
        {
            foreach( $value as $v )
                $this->setPreference( $name, $value, $expires, ++$highest );
        }
        else
        {
            $this->setPreference( $name, $value, $expires, ++$highest );
        }

        return $this;
    }
public function addIndexedPreference( $name, $value, $expires = null, $max = 0 )
    {
        // what's the current highest index and how many is there?
        $highest = -1;
        $count = 0;

        foreach( $this->getPreferences() as $pref )
        {
            if( $pref->getName() == $name )
            {
                $count++;

                if( $pref->getIx() > $highest )
                    $highest = $pref->getIx();
            }
        }

        if( $max && ( $count >= $max ) )
            throw new IndexLimitException( 'Requested maximum number of indexed preferences reached' );

        if( is_array( $value ) )
        {
            foreach( $value as $v )
                $this->setPreference( $name, $value, $expires, ++$highest );
        }
        else
        {
            $this->setPreference( $name, $value, $expires, ++$highest );
        }

        return $this;
    }
public function addIndexedPreference ( $ name , $ value , $ expires = null , $ max = 0 ) { // what's the current highest index and how many is there? $ highest = - 1 ; $ count = 0 ; foreach ( $ this -> getPreferences ( ) as $ pref ) { if ( $ pref -> getName ( ) == $ name ) { $ count ++ ; if ( $ pref -> getIx ( ) > $ highest ) $ highest = $ pref -> getIx ( ) ; } } if ( $ max && ( $ count >= $ max ) ) throw new IndexLimitException ( 'Requested maximum number of indexed preferences reached' ) ; if ( is_array ( $ value ) ) { foreach ( $ value as $ v ) $ this -> setPreference ( $ name , $ value , $ expires , ++ $ highest ) ; } else { $ this -> setPreference ( $ name , $ value , $ expires , ++ $ highest ) ; } return $ this ; }
Add an indexed preference

Let's say we need to add a list of email addresses as a preference where the following is
the list:

$emails = [ 'a@b.c', 'd@e.f', 'g@h.i' ];

then we could add these as an indexed preference as follows for a given User $u:

$u->addPreference( 'mailing_list.goalies.email', $emails );

which would result in database entries as follows:

attribute                      index   op   value
------------------------------------------------------
| mailing_list.goalies.email | 0     | =  | a@b.c    |
| mailing_list.goalies.email | 1     | =  | d@e.f    |
| mailing_list.goalies.email | 2     | =  | g@h.i    |
------------------------------------------------------

we could then add a fourth address as follows:

$u->addPreference( 'mailing_list.goalies.email', 'j@k.l' );

which would result in database entries as follows:

attribute                      index   op   value
------------------------------------------------------
| mailing_list.goalies.email | 0     | =  | a@b.c    |
| mailing_list.goalies.email | 1     | =  | d@e.f    |
| mailing_list.goalies.email | 2     | =  | g@h.i    |
| mailing_list.goalies.email | 3     | =  | j@k.l    |
------------------------------------------------------


===== BEGIN NOT IMPLEMENTED =====

If out list was to be of names and emails, then we could create an array as follows:

$emails = [
[ 'name' => 'John Smith', 'email' => 'a@b.c' ],
[ 'name' => 'David Blue', 'email' => 'd@e.f' ]
];

then we could add these as an indexed preference as follows for a given User $u:

$u->addPreference( 'mailing_list.goalies', $emails );

which would result in database entries as follows:

attribute                      index   op   value
--------------------------------------------------------
| mailing_list.goalies!email | 0     | =  | a@b.c      |
| mailing_list.goalies!name  | 0     | =  | John Smith |
| mailing_list.goalies!email | 1     | =  | d@e.f      |
| mailing_list.goalies!name  | 1     | =  | David Blue |
--------------------------------------------------------

We can further be specific on operator for each one as follows:

$emails = [
[ 'name' => [ value = 'John Smith', expires = '123456789' ] ]
];

Note that in the above form, value is required but if expires is not set, it will be taken from the function parameters.

===== END NOT IMPLEMENTED =====

@param string $name The preference name
@param string $value The value to assign to the preference
@param int $expires default null The expiry as a UNIX timestamp. null which means never.
@param int $max The maximum index allowed. Defaults to 0 meaning no limit.
@return \Oss2\Doctrine2\WithPreferences An instance of this object for fluid interfaces.
@throws \Oss2\Doctrine2\WithPreferences\IndexLimitException If $max is set and limit exceeded
Add an indexed preference