Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
leopoletto authored and actions-user committed Dec 26, 2020
1 parent dd110d8 commit 39d883d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
16 changes: 8 additions & 8 deletions src/Concerns/HasMetaAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public function setMeta(string $key): AttributeCast
*/
public function getMeta(string $key, $fallback = null): ?MetaAttribute
{
if( $meta = $this->metas()->firstWhere('key', $key)){
if ($meta = $this->metas()->firstWhere('key', $key)) {
return $meta;
}
}

/**
* Get a collection of meta attributes
*
* @return object
*/
/**
* Get a collection of meta attributes
*
* @return object
*/
public function getMetas(): object
{
return (object) $this->metas->pluck('value', 'key')->toArray();
Expand All @@ -63,7 +63,7 @@ public function getMetas(): object
*/
public function getMetaValue(string $key, $fallback = null)
{
if($meta = $this->metas()->firstWhere('key', $key)){
if ($meta = $this->metas()->firstWhere('key', $key)) {
return $meta->value;
}

Expand All @@ -74,7 +74,7 @@ public function getMetaValue(string $key, $fallback = null)
* Check if a meta attribute exists
*
* @param string $key
* @return boolean
* @return bool
*/
public function hasMeta(string $key): bool
{
Expand Down
17 changes: 8 additions & 9 deletions src/Models/MetaAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class MetaAttribute extends Model
* @var array
*/
protected $fillable = [
'key',
'value',
'type'
'key',
'value',
'type',
];

/**
Expand All @@ -25,7 +25,7 @@ class MetaAttribute extends Model
*/
protected $hidden = [
'model_id',
'model_type'
'model_type',
];

/**
Expand All @@ -38,13 +38,13 @@ protected static function boot()
parent::boot();

static::retrieved(function (MetaAttribute $meta) {
if( $meta->type == 'encrypted' ){
if ($meta->type == 'encrypted') {
$meta->value = decrypt($meta->value);
}
});

static::saving(function ($meta) {
if( $meta->type == 'encrypted' ){
if ($meta->type == 'encrypted') {
$meta->value = encrypt($meta->value);
}
});
Expand All @@ -53,17 +53,16 @@ protected static function boot()
public function getValueAttribute($value)
{
$this->casts = [
'value' => $this->type
'value' => $this->type,
];

return $this->castAttribute('value', $value);
}


public function setTypeAttribute($value)
{
$this->casts = [
'value' => $value
'value' => $value,
];

$this->attributes['type'] = $value;
Expand Down
83 changes: 42 additions & 41 deletions src/Support/AttributeCast.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php
<?php

namespace Nncodes\MetaAttributes\Support;

use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Nncodes\MetaAttributes\Models\MetaAttribute;

class AttributeCast
{
/**
* Meta attribute key
*
*
* @var string
*/
protected string $key;
Expand All @@ -23,25 +23,25 @@ class AttributeCast
*/
protected $relationship;

/**
* Create a new instance
*
* @param \Illuminate\Database\Eloquent\Relations\MorphMany $relationship
* @param string $key
*/
/**
* Create a new instance
*
* @param \Illuminate\Database\Eloquent\Relations\MorphMany $relationship
* @param string $key
*/
public function __construct(MorphMany $relationship, string $key)
{
$this->relationship = $relationship;
$this->key = $key;
}

/**
* Build a new instance
*
* @param \Illuminate\Database\Eloquent\Relations\MorphMany $relationship
* @param string $key
* @return self
*/
/**
* Build a new instance
*
* @param \Illuminate\Database\Eloquent\Relations\MorphMany $relationship
* @param string $key
* @return self
*/
public static function make(MorphMany $relationship, string $key): self
{
return new static($relationship, $key);
Expand All @@ -56,8 +56,7 @@ public static function make(MorphMany $relationship, string $key): self
*/
protected function storeCastingAs(string $type, $value): MetaAttribute
{
if($meta = $this->relationship->firstWhere('key', $this->key)){

if ($meta = $this->relationship->firstWhere('key', $this->key)) {
$meta->fill([
'type' => $type,
'value' => $value,
Expand All @@ -69,29 +68,29 @@ protected function storeCastingAs(string $type, $value): MetaAttribute
return $this->relationship->create([
'key' => $this->key,
'type' => $type,
'value' => $value
'value' => $value,
]);
}

/**
* Create a meta attribute casting the value as array
*
*
* @param array $value
* @return \Nncodes\MetaAttributes\Models\MetaAttribute
*/
public function asArray(array $value): MetaAttribute
{
{
return $this->storeCastingAs('array', $value);
}

/**
* Create a meta attribute casting the value as boolean
*
*
* @param mixed $value
* @return \Nncodes\MetaAttributes\Models\MetaAttribute
*/
public function asBoolean($value): MetaAttribute
{
{
return $this->storeCastingAs('boolean', $value);
}

Expand All @@ -106,37 +105,39 @@ public function asCollection($value): MetaAttribute
return $this->storeCastingAs('collection', $value);
}

/**
* Create a meta attribute casting the value as date
*
* @param string @format
* @param mixed $value
* @return \Nncodes\MetaAttributes\Models\MetaAttribute
*/
/**
* Create a meta attribute casting the value as date
*
* @param string @format
* @param mixed $value
* @return \Nncodes\MetaAttributes\Models\MetaAttribute
*/
public function asDate($value, $format = 'Y-m-d'): MetaAttribute
{
$value = $value instanceof \Carbon\Carbon ? $value->format($format) : $value;

return $this->storeCastingAs('date:' . $format, $value);
}

/**
* Create a meta attribute casting the value as datetime
*
*
* @param string @format
* @param mixed $value
* @return \Nncodes\MetaAttributes\Models\MetaAttribute
*/
public function asDatetime($value, $format = 'Y-m-d H:i:s'): MetaAttribute
{
$value = $value instanceof \Carbon\Carbon ? $value->format($format) : $value;

return $this->storeCastingAs('datetime:' . $format, $value);
}

/**
* Create a meta attribute casting the value as decimal
*
*
* @param mixed $value
* @param integer $digits
* @param int $digits
* @return \Nncodes\MetaAttributes\Models\MetaAttribute
*/
public function asDecimal($value, int $digits = 2): MetaAttribute
Expand Down Expand Up @@ -234,34 +235,34 @@ public function asTimestamp($value): MetaAttribute

/**
* Create a meta attribute casting the value using autodiscover
*
*
* @param mixed $value
* @return \Nncodes\MetaAttributes\Models\MetaAttribute
*/
public function value($value): MetaAttribute
{

if( $timestamp = strtotime($value) ){
if ($timestamp = strtotime($value)) {
$value = Carbon::createFromTimestamp($timestamp);
}

if( $value instanceof \Carbon\Carbon ){
if( $value->hour || $value->minute || $value->second ){
if ($value instanceof \Carbon\Carbon) {
if ($value->hour || $value->minute || $value->second) {
return $this->asDatetime($value);
}

return $this->asDate($value);
}

if( $value instanceof Collection ){
if ($value instanceof Collection) {
return $this->asCollection($value);
}

$primitiveTypes = ['boolean', 'integer', 'double', 'string', 'object', 'array'];

if(!in_array($type = gettype($value), $primitiveTypes)){
if (! in_array($type = gettype($value), $primitiveTypes)) {
$type = 'string';
}

return $this->storeCastingAs($type, $value);
}
}
}

0 comments on commit 39d883d

Please sign in to comment.