| 1 |
<?php |
| 2 |
namespace Depicter\Database\Entity; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use TypeRocket\Models\Model as BaseModel; |
| 6 |
|
| 7 |
class Model extends BaseModel |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Determines what fields should be updated automatically. |
| 11 |
* |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
protected $autoFill = []; |
| 15 |
|
| 16 |
/** |
| 17 |
* Update resource fields |
| 18 |
* |
| 19 |
* @param array $fields |
| 20 |
* |
| 21 |
* @return mixed |
| 22 |
*/ |
| 23 |
public function update( $fields = [] ) |
| 24 |
{ |
| 25 |
$fields = $this->formatProperties( $fields ); |
| 26 |
return parent::update( $fields ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Create resource by fields |
| 31 |
* |
| 32 |
* When a resource is created the Model ID should be set to the |
| 33 |
* resource's ID. |
| 34 |
* |
| 35 |
* @param array $fields |
| 36 |
* |
| 37 |
* @return mixed |
| 38 |
*/ |
| 39 |
public function create( $fields = [] ) |
| 40 |
{ |
| 41 |
$fields = $this->formatProperties( $fields ); |
| 42 |
return parent::create( $fields ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Format properties |
| 47 |
* |
| 48 |
* @param array $fields |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public function formatProperties( $fields ) |
| 53 |
{ |
| 54 |
$fields = Arr::merge( $fields, $this->autoFill ); |
| 55 |
|
| 56 |
foreach( $fields as $name => $value ) { |
| 57 |
if( ! empty( $this->format[ $name ] ) && is_callable( $this->format[ $name ] ) ){ |
| 58 |
$fields[ $name ] = call_user_func( $this->format[ $name ], $value ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return $fields; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Get Date Time |
| 68 |
* |
| 69 |
* @return bool|string |
| 70 |
*/ |
| 71 |
public function getDateTime() |
| 72 |
{ |
| 73 |
return gmdate('Y-m-d H:i:s', time()); |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|