Skip to content

Commit

Permalink
Implement ArrayAccess
Browse files Browse the repository at this point in the history
Solves symbiote#45. Note that this is meant for the older version 3 of this module. I didn't start from the master branch because I still have SilverStripe 3 so I wouln't be able to test with SS4. But I guess this can be easily adapted to the newer version.
  • Loading branch information
Taitava authored Sep 6, 2018
1 parent db1ed5d commit 5a4b021
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion code/fields/MultiValueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @author Marcus Nyeholt <[email protected]>
*/
class MultiValueField extends DBField implements CompositeDBField
class MultiValueField extends DBField implements CompositeDBField, ArrayAccess
{

/**
Expand Down Expand Up @@ -211,4 +211,72 @@ protected function restoreValueFromDb($value)
}
return $value ?: array();
}

/**
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset <p>
* An offset to check for.
* </p>
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
* @since 5.0.0
*/
public function offsetExists($offset)
{
$field_values = $this->getValue();
return isset($field_values[$offset]);
}

/**
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
* @return mixed Can return all value types.
* @since 5.0.0
*/
public function offsetGet($offset)
{
$field_values = $this->getValue();
return $field_values[$offset];
}

/**
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetSet($offset, $value)
{
$field_values = $this->getValue();
$field_values[$offset] = $value;
$this->setValue($field_values);
}

/**
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset <p>
* The offset to unset.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetUnset($offset)
{
$field_values = $this->getValue();
unset($field_values[$offset]);
$this->setValue($field_values);
}
}

0 comments on commit 5a4b021

Please sign in to comment.