* @copyright 2017 adeattwood.co.uk * @license BSD-2-Clause http://adeattwood.co.uk/license.html * @link adeattwood.co.uk * @since v0.1 */ class ArrayObject extends \ArrayObject { /** * __construct * * @param mixed $array * @param mixed $flags * @param string $iterator_class * * @return void */ public function __construct( $array = [], $flags = null, $iterator_class = 'ArrayIterator' ) { if( $flags === null ) { $flags = self::ARRAY_AS_PROPS; } parent::__construct( $array, $flags, $iterator_class ); } /** * Test that the ArrayObject contains an item * * @param $item The item to test * * @return bool */ public function contains( $item ) { $flipped = array_flip( ( array )$this ); return isset( $flipped[ $item ] ); } /** * Gets a random value from the array * * @param int $count * * @return int|array */ public function randomValue( $count = 1) { $arr = ( array )$this; shuffle($arr); $r = array(); for ($i = 0; $i < $count; $i++) { $r[] = $arr[$i]; } return $count == 1 ? $r[0] : $r; } /** * Converts the ArrayObject into a table * * @param mixed $transpose * @param mixed $recursive * @param mixed $typeHint * @param string $tableOptions * @param mixed $keyOptions * @param string $valueOptions * @param string $null * * @return string */ public function toTable( $transpose = false, $recursive = false, $typeHint = true, $tableOptions = ['class' => 'table table-bordered table-striped'], $keyOptions = [], $valueOptions = ['style' => 'cursor: default; border-bottom: 1px #aaa dashed;'], $null = '(not set)' ) { // Sanity check if (empty( ( array )$this)) { return false; } $array = ( array )$this; // Start the table $table = Html::beginTag('table', $tableOptions) . "\n"; // The header $table .= "\t
' . print_r($value, true) . ''; } else { $value = Html::tag('span', $value, $valueOptions); } $table .= "\t\t