Skip to content

Commit

Permalink
add ByteUnit cast
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Aug 15, 2024
1 parent 5ae5214 commit 5ee8de9
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Casts/ByteUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace OpenSoutheners\ExtendedLaravel\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use OpenSoutheners\ByteUnitConverter\ByteUnitConverter;

/**
* @implements \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\OpenSoutheners\ByteUnitConverter\ByteUnitConverter, int>
*/
class ByteUnit implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
{
return (string) ByteUnitConverter::new($value)->nearestUnit();
}

/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}
}

0 comments on commit 5ee8de9

Please sign in to comment.