* @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"; if ($transpose) { foreach ($this as $key => $value) { if ($typeHint) { $valueOptions['title'] = Enum::getType(strtoupper($value)); } if (is_array($value)) { $value = '
' . print_r($value, true) . '
'; } else { $value = Html::tag('span', $value, $valueOptions); } $table .= "\t\t" . Html::tag('span', $key, $keyOptions) . "" . "" . $value . "\n\t\n"; } $table .= ""; return $table; } if (!isset($array[0]) || !is_array($array[0])) { $array = array($array); } // Take the keys from the first row as the headings foreach (array_keys($array[0]) as $heading) { $table .= '' . Html::tag('span', $heading, $keyOptions) . ''; } $table .= "\n"; // The body foreach ($array as $row) { $table .= "\t"; foreach ($row as $cell) { $table .= ''; // Cast objects if (is_object($cell)) { $cell = (array)$cell; } if ($recursive === true && is_array($cell) && !empty($cell)) { // Recursive mode $table .= "\n" . static::array2table($cell, true, true) . "\n"; } else { if (!is_null($cell) && is_bool($cell)) { $val = $cell ? 'true' : 'false'; $type = 'boolean'; } else { $chk = (strlen($cell) > 0); $type = $chk ? Enum::getType($cell) : 'null'; $val = $chk ? htmlspecialchars((string)$cell) : $null; } if ($typeHint) { $valueOptions['title'] = $type; } $table .= Html::tag('span', $val, $valueOptions); } $table .= ''; } $table .= "\n"; } $table .= ''; return $table; } }