Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Last active February 25, 2020 05:15
Show Gist options
  • Save lgedeon/37015ebecf78a0907f87dbaab51413d5 to your computer and use it in GitHub Desktop.
Save lgedeon/37015ebecf78a0907f87dbaab51413d5 to your computer and use it in GitHub Desktop.
Logging system
<?php
namespace IOne\IOne3\Core\Entities;
/**
* Class LogEntries
*
* Represent all log entries when they need to be gathered before output.
*/
class LogEntries {
/**
* Keep track of all entries.
*
* @var LogEntry[]
*/
private $entries = [];
/**
* LogEntries constructor.
] */
public function __construct() {
// Not sure yet if we will need this.
}
/**
* Add an existing LogEntry.
*
* @param LogEntry $entry A single log entry.
*/
public function add( LogEntry $entry ) {
$this->entries[] = $entry;
}
/**
* Create a new LogEntry, and return it, while keeping a reference here.
*
* @param array $fields Array of key => value pairs to add to new entry.
*
* @return LogEntry
*/
public function create( array $fields ) {
$entry = new LogEntry();
$entry->add_fields( $fields );
$this->add( $entry );
return $entry;
}
/**
* Array of all LogEntry objects.
*
* @return LogEntry[]
*/
public function objects() {
return $this->entries;
}
/**
* Array of arrays containing all entries.
*
* Ensures that all entries have the same fields.
*
* @param array $defaults Default values if certain fields must be included.
*
* @return array[]
*/
public function arrays( array $defaults = [] ) {
if ( empty( $defaults ) ) {
$defaults = array_fill_keys( $this->all_fields(), null );
}
return array_map(
function ( $entry ) use ( $defaults ) {
return array_merge(
$defaults,
$entry->fields()
);
},
$this->entries
);
}
/**
* List of all keys used across all entries.
*
* @return array
*/
public function all_fields() {
return array_reduce(
$this->entries,
function( $array, $object ) {
return array_merge( $array, $object->keys() );
},
[]
);
}
}
<?php
use IOne\IOne3\Core\Entities\LogEntries;
use IOne\IOne3\Core\Entities\LogEntry;
/**
* Test LogEntriesTest Class
*
* @package ione3
* @group helpers
*/
class LogEntriesTest extends WP_UnitTestCase {
// Test add() and objects() methods.
public function test_objects() {
$entries = new LogEntries();
$entry = new LogEntry();
$entries->add( $entry );
$this->assertSame( $entry, $entries->objects()[0] );
}
// Test create(), arrays(), and all_fields() methods.
public function test_arrays() {
$entries = new LogEntries();
$entries->create(
[
'earth' => 'stone',
'fire' => 'light',
'water' => 'ice',
'air' => 'wind',
]
);
$entries->create(
[
'moon' => 'beam',
'i' => 'beam',
'transporter' => 'beam',
]
);
$this->assertSame(
[
[
'earth' => 'stone',
'fire' => 'light',
'water' => 'ice',
'air' => 'wind',
'moon' => null,
'i' => null,
'transporter' => null,
],
[
'earth' => null,
'fire' => null,
'water' => null,
'air' => null,
'moon' => 'beam',
'i' => 'beam',
'transporter' => 'beam',
]
],
$entries->arrays()
);
$this->assertSame(
[
[
'earth' => 'stone',
'sky' => 'dome',
'fire' => 'light',
'water' => 'ice',
'air' => 'wind',
],
[
'earth' => 'angel',
'sky' => 'dome',
'moon' => 'beam',
'i' => 'beam',
'transporter' => 'beam',
]
],
$entries->arrays(
[
'earth' => 'angel',
'sky' => 'dome',
]
)
);
}
}
<?php
namespace IOne\IOne3\Core\Entities;
/**
* Class LogEntry
*
* Represent a single log entry as details are added to it.
*/
class LogEntry {
/**
* Key => value pairs. If output as CSV each key is a column.
*
* @var mixed[]
*/
protected $fields = [];
/**
* Add a field.
*
* @param string $key Field name / column name.
* @param mixed $value Scalar value.
*/
public function add_field( string $key, $value ) {
$this->fields[ $key ] = $value;
}
/**
* Add several fields at once.
*
* @param array $fields Key => value pairs to add to entry.
*/
public function add_fields( array $fields ) {
$this->fields = array_merge( $this->fields, $fields );
}
/**
* Return all fields.
*
* @return array
*/
public function fields() {
return $this->fields;
}
/**
* Return just the keys.
*/
public function keys() {
return array_keys( $this->fields );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment