Laravel 9 Update (#1413)
Update to Laravel 9 and PHP 8+ Co-authored-by: B.Fatih KOZ <fatih.koz@gmail.com>
This commit is contained in:
@@ -37,10 +37,10 @@ abstract class Award
|
||||
*/
|
||||
|
||||
/** @var \App\Models\Award|null */
|
||||
protected $award;
|
||||
protected ?AwardModel $award;
|
||||
|
||||
/** @var \App\Models\User|null */
|
||||
protected $user;
|
||||
protected ?User $user;
|
||||
|
||||
public function __construct(AwardModel $award = null, User $user = null)
|
||||
{
|
||||
@@ -69,9 +69,9 @@ abstract class Award
|
||||
/**
|
||||
* Add the award to this user, if they don't already have it
|
||||
*
|
||||
* @return bool|UserAward
|
||||
* @return bool|UserAward|null
|
||||
*/
|
||||
protected function addAward()
|
||||
protected function addAward(): bool|UserAward|null
|
||||
{
|
||||
$w = [
|
||||
'user_id' => $this->user->id,
|
||||
|
||||
@@ -84,6 +84,7 @@ abstract class Controller extends \Illuminate\Routing\Controller
|
||||
*
|
||||
* @param $message
|
||||
* @param null|mixed $count
|
||||
* @param mixed $attrs
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
|
||||
@@ -7,14 +7,14 @@ namespace App\Contracts;
|
||||
*/
|
||||
abstract class Enum
|
||||
{
|
||||
protected static $cache = [];
|
||||
protected static $codes = [];
|
||||
protected static $labels = [];
|
||||
protected static array $cache = [];
|
||||
protected static array $codes = [];
|
||||
protected static array $labels = [];
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $value;
|
||||
protected int $value;
|
||||
|
||||
/**
|
||||
* Create an instance of this Enum
|
||||
@@ -29,9 +29,9 @@ abstract class Enum
|
||||
/**
|
||||
* Return the value that's been set if this is an instance
|
||||
*
|
||||
* @return mixed
|
||||
* @return int|null
|
||||
*/
|
||||
final public function getValue()
|
||||
final public function getValue(): ?int
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
@@ -41,9 +41,9 @@ abstract class Enum
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return mixed
|
||||
* @return string
|
||||
*/
|
||||
final public static function label($value)
|
||||
final public static function label($value): string
|
||||
{
|
||||
if (isset(static::$labels[$value])) {
|
||||
$val = static::$labels[$value];
|
||||
@@ -53,6 +53,8 @@ abstract class Enum
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,11 +89,11 @@ abstract class Enum
|
||||
*
|
||||
* @return false|int|string
|
||||
*/
|
||||
public static function convertToCode($value)
|
||||
public static function convertToCode($value): bool|int|string|null
|
||||
{
|
||||
$value = (int) $value;
|
||||
if (!array_key_exists($value, static::$codes)) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
return static::$codes[$value];
|
||||
|
||||
9
app/Contracts/Factory.php
Normal file
9
app/Contracts/Factory.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory as EloquentFactory;
|
||||
|
||||
abstract class Factory extends EloquentFactory
|
||||
{
|
||||
}
|
||||
@@ -133,7 +133,10 @@ abstract class Repository extends BaseRepository
|
||||
$page = (int) request()->query('page', 1);
|
||||
|
||||
$results = $this->model->{$method}($limit, $columns, 'page', $page);
|
||||
$results->appends(app('request')->query());
|
||||
|
||||
$qs = request()->except(['page', 'user']);
|
||||
$results->appends($qs);
|
||||
|
||||
$this->resetModel();
|
||||
|
||||
return $this->parserResult($results);
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Contracts;
|
||||
|
||||
use ArrayAccess;
|
||||
use PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure;
|
||||
|
||||
/**
|
||||
* Abstract unit wrapper
|
||||
@@ -11,45 +10,89 @@ use PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure;
|
||||
class Unit implements ArrayAccess
|
||||
{
|
||||
/**
|
||||
* The unit this is kept as
|
||||
* The localized unit the user wants it displayed in
|
||||
*/
|
||||
public $unit;
|
||||
public string $localUnit;
|
||||
|
||||
/**
|
||||
* All of the units of this class
|
||||
* The unit that this value is stored in locally
|
||||
*/
|
||||
public $units;
|
||||
public string $internalUnit;
|
||||
|
||||
/**
|
||||
* All of the units of this class which are reported in an API response
|
||||
*/
|
||||
public array $units;
|
||||
|
||||
/**
|
||||
* Holds an instance of the PhpUnit type
|
||||
*
|
||||
* @var \PhpUnitsOfMeasure\AbstractPhysicalQuantity
|
||||
*/
|
||||
protected $instance;
|
||||
protected mixed $instance;
|
||||
|
||||
/**
|
||||
* Units that are included as part of the REST response
|
||||
*/
|
||||
public $responseUnits = [];
|
||||
public array $responseUnits = [];
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* Factory method for creating a new unit type
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param string $unit
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return Unit
|
||||
*/
|
||||
public function value()
|
||||
public static function make(mixed $value, string $unit): self
|
||||
{
|
||||
return $this->__toString();
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return new static($value, $unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value in an internal format
|
||||
*
|
||||
* @param int|null $round Optional value to round to
|
||||
*
|
||||
* @return float|null
|
||||
*/
|
||||
public function internal(?int $round = null): ?float
|
||||
{
|
||||
return $this->toUnit($this->internalUnit, $round);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value in the localized format
|
||||
*
|
||||
* @param int|null $round Optional value to round to
|
||||
*
|
||||
* @return float|null
|
||||
*/
|
||||
public function local(?int $round = null): ?float
|
||||
{
|
||||
return $this->toUnit($this->localUnit, $round);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just call toUnit() on the PhpUnitOfMeasure instance
|
||||
*
|
||||
* @param string $unit
|
||||
* @param string $unit
|
||||
* @param int|null $round Optional value to round to
|
||||
*
|
||||
* @return mixed
|
||||
* @return float|null
|
||||
*/
|
||||
public function toUnit($unit)
|
||||
public function toUnit(string $unit, ?int $round = null): ?float
|
||||
{
|
||||
return $this->instance->toUnit($unit);
|
||||
$val = $this->instance->toUnit($unit);
|
||||
if ($round === null) {
|
||||
return $val;
|
||||
}
|
||||
|
||||
return round($val, $round);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,7 +102,7 @@ class Unit implements ArrayAccess
|
||||
{
|
||||
$response = [];
|
||||
foreach ($this->responseUnits as $unit) {
|
||||
$response[$unit] = $this[$unit] ?? 0;
|
||||
$response[$unit] = round($this->instance->toUnit($unit), 2);
|
||||
}
|
||||
|
||||
return $response;
|
||||
@@ -72,7 +115,7 @@ class Unit implements ArrayAccess
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
public function offsetExists($offset): bool
|
||||
{
|
||||
return $this->offsetGet($offset) !== null;
|
||||
}
|
||||
@@ -80,19 +123,15 @@ class Unit implements ArrayAccess
|
||||
/**
|
||||
* Implements ArrayAccess
|
||||
*
|
||||
* @param $offset
|
||||
* @param $unit
|
||||
*
|
||||
* @return mixed
|
||||
* @return float|null
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
public function offsetGet($unit): ?float
|
||||
{
|
||||
try {
|
||||
$value = $this->instance->toUnit($offset);
|
||||
if (!$value) {
|
||||
return;
|
||||
}
|
||||
} catch (UnknownUnitOfMeasure $e) {
|
||||
return;
|
||||
$value = $this->instance->toUnit($unit);
|
||||
if (!$value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return round($value, 2);
|
||||
@@ -124,6 +163,6 @@ class Unit implements ArrayAccess
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return (string) $this->offsetGet($this->unit);
|
||||
return (string) $this->offsetGet($this->localUnit);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user