162 lines
4.6 KiB
PHP
162 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace adeattwood\helpers;
|
|
|
|
use Yii;
|
|
use yii\helpers\Html;
|
|
use adeattwood\helpers\Enum;
|
|
|
|
/**
|
|
* @category PHP
|
|
* @package adeattwood\yii-helpers
|
|
* @author Ade Attwood <attwood16@googlemail.com>
|
|
* @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 = '<span class="not-set">(not set)</span>'
|
|
) {
|
|
|
|
// Sanity check
|
|
if (empty( ( array )$this)) {
|
|
return false;
|
|
}
|
|
|
|
$array = ( array )$this;
|
|
|
|
// Start the table
|
|
$table = Html::beginTag('table', $tableOptions) . "\n";
|
|
// The header
|
|
$table .= "\t<tr>";
|
|
if ($transpose) {
|
|
foreach ($this as $key => $value) {
|
|
if ($typeHint) {
|
|
$valueOptions['title'] = Enum::getType(strtoupper($value));
|
|
}
|
|
if (is_array($value)) {
|
|
$value = '<pre>' . print_r($value, true) . '</pre>';
|
|
} else {
|
|
$value = Html::tag('span', $value, $valueOptions);
|
|
}
|
|
$table .= "\t\t<th>" . Html::tag('span', $key, $keyOptions) . "</th>" .
|
|
"<td>" . $value . "</td>\n\t</tr>\n";
|
|
}
|
|
$table .= "</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 .= '<th>' . Html::tag('span', $heading, $keyOptions) . '</th>';
|
|
}
|
|
$table .= "</tr>\n";
|
|
// The body
|
|
foreach ($array as $row) {
|
|
$table .= "\t<tr>";
|
|
foreach ($row as $cell) {
|
|
$table .= '<td>';
|
|
// 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 .= '</td>';
|
|
}
|
|
$table .= "</tr>\n";
|
|
}
|
|
$table .= '</table>';
|
|
return $table;
|
|
}
|
|
}
|