| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Log; |
| 4 |
|
| 5 |
use Give\Log\ValueObjects\LogType; |
| 6 |
use Give\Log\ValueObjects\LogCategory; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class LogFactory |
| 10 |
* @package Give\Log |
| 11 |
* |
| 12 |
* @since 2.10.0 |
| 13 |
*/ |
| 14 |
class LogFactory { |
| 15 |
/** |
| 16 |
* Make LogModel instance |
| 17 |
* |
| 18 |
* @param string $type |
| 19 |
* @param string $message |
| 20 |
* @param string $category |
| 21 |
* @param string $source |
| 22 |
* @param array $context |
| 23 |
* @param int|null $logId |
| 24 |
* @param string|null $date |
| 25 |
* |
| 26 |
* @return LogModel |
| 27 |
*/ |
| 28 |
public static function make( $type, $message, $category, $source, $context = [], $logId = null, $date = null ) { |
| 29 |
return new LogModel( $type, $message, $category, $source, $context, $logId, $date ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Make LogModel instance from array of data |
| 34 |
* |
| 35 |
* @param array $data |
| 36 |
* |
| 37 |
* @return LogModel |
| 38 |
*/ |
| 39 |
public static function makeFromArray( $data ) { |
| 40 |
// Get default |
| 41 |
$data = array_merge( static::getDefaults(), $data ); |
| 42 |
|
| 43 |
return new LogModel( |
| 44 |
$data['type'], |
| 45 |
$data['message'], |
| 46 |
$data['category'], |
| 47 |
$data['source'], |
| 48 |
$data['context'], |
| 49 |
$data['id'], |
| 50 |
$data['date'] |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get log default fields array |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public static function getDefaults() { |
| 60 |
return [ |
| 61 |
'type' => LogType::getDefault(), |
| 62 |
'message' => esc_html__( 'Something went wrong', 'give' ), |
| 63 |
'category' => LogCategory::getDefault(), |
| 64 |
'source' => esc_html__( 'Give Core', 'give' ), |
| 65 |
'context' => [], |
| 66 |
'id' => null, |
| 67 |
'date' => date( 'Y-m-d H:i:s' ), |
| 68 |
]; |
| 69 |
} |
| 70 |
} |
| 71 |
|