Skip to content

Commit 8abb34c

Browse files
committed
🌱 Stable version initial release
0 parents  commit 8abb34c

File tree

16 files changed

+782
-0
lines changed

16 files changed

+782
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Attributes\V1\Appends;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::IS_REPEATABLE)]
10+
class Appends
11+
{
12+
public function __construct()
13+
{
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Attributes\V1\Cast;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
10+
class Cast
11+
{
12+
public function __construct(public string $type)
13+
{
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Attributes\V1\EagerLoad;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::IS_REPEATABLE)]
10+
class EagerLoad
11+
{
12+
public function __construct()
13+
{
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Attributes\V1\Fillable;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::IS_REPEATABLE)]
10+
class Fillable
11+
{
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Attributes\V1\Hidden;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::IS_REPEATABLE)]
10+
class Hidden
11+
{
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Attributes\V1\Selectable;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS_CONSTANT)]
10+
class Selectable
11+
{
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Attributes\V1\TableColumn;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::IS_REPEATABLE)]
10+
class TableColumn
11+
{
12+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Enums\V1\StatusCode;
6+
7+
use Foundation\Support\Traits\V1\CleanEnum\CleanEnum;
8+
9+
enum StatusCode: int
10+
{
11+
use CleanEnum;
12+
13+
case HttpContinue = 100;
14+
case HttpSwitchingProtocols = 101;
15+
case HttpProcessing = 102;
16+
case HttpEarlyHints = 103;
17+
case HttpOk = 200;
18+
case HttpCreated = 201;
19+
case HttpAccepted = 202;
20+
case HttpNonAuthoritativeInformation = 203;
21+
case HttpNoContent = 204;
22+
case HttpResetContent = 205;
23+
case HttpPartialContent = 206;
24+
case HttpMultiStatus = 207;
25+
case HttpAlreadyReported = 208;
26+
case HttpImUsed = 226;
27+
case HttpMultipleChoices = 300;
28+
case HttpMovedPermanently = 301;
29+
case HttpFound = 302;
30+
case HttpSeeOther = 303;
31+
case HttpNotModified = 304;
32+
case HttpUseProxy = 305;
33+
case HttpReserved = 306;
34+
case HttpTemporaryRedirect = 307;
35+
case HttpPermanentlyRedirect = 308;
36+
case HttpBadRequest = 400;
37+
case HttpUnauthorized = 401;
38+
case HttpPaymentRequired = 402;
39+
case HttpForbidden = 403;
40+
case HttpNotFound = 404;
41+
case HttpMethodNotAllowed = 405;
42+
case HttpNotAcceptable = 406;
43+
case HttpProxyAuthenticationRequired = 407;
44+
case HttpRequestTimeout = 408;
45+
case HttpConflict = 409;
46+
case HttpGone = 410;
47+
case HttpLengthRequired = 411;
48+
case HttpPreconditionFailed = 412;
49+
case HttpRequestEntityTooLarge = 413;
50+
case HttpRequestUriTooLong = 414;
51+
case HttpUnsupportedMediaType = 415;
52+
case HttpRequestedRangeNotSatisfiable = 416;
53+
case HttpExpectationFailed = 417;
54+
case HttpIAmATeapot = 418;
55+
case HttpMisdirectedRequest = 421;
56+
case HttpUnprocessableEntity = 422;
57+
case HttpLocked = 423;
58+
case HttpFailedDependency = 424;
59+
case HttpTooEarly = 425;
60+
case HttpUpgradeRequired = 426;
61+
case HttpPreconditionRequired = 428;
62+
case HttpTooManyRequests = 429;
63+
case HttpRequestHeaderFieldsTooLarge = 431;
64+
case HttpUnavailableForLegalReasons = 451;
65+
case HttpInternalServerError = 500;
66+
case HttpNotImplemented = 501;
67+
case HttpBadGateway = 502;
68+
case HttpServiceUnavailable = 503;
69+
case HttpGatewayTimeout = 504;
70+
case HttpVersionNotSupported = 505;
71+
case HttpVariantAlsoNegotiatesExperimental = 506;
72+
case HttpInsufficientStorage = 507;
73+
case HttpLoopDetected = 508;
74+
case HttpNotExtended = 510;
75+
case HttpNetworkAuthenticationRequired = 511;
76+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Traits\V1\CleanEnum;
6+
7+
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Str;
9+
use Throwable;
10+
use ValueError;
11+
12+
trait CleanEnum
13+
{
14+
/**
15+
* Get the translation's file key
16+
*/
17+
public static function translationKey(): string
18+
{
19+
return self::shortName();
20+
}
21+
22+
/**
23+
* Get the enum short name
24+
*/
25+
private static function shortName(bool $lower = true): string
26+
{
27+
$_name = basename(str_replace('\\', '/', get_called_class()));
28+
29+
return $lower ? Str::snake($_name) : $_name;
30+
}
31+
32+
/**
33+
* Get the module name
34+
*/
35+
private static function moduleName(bool $lower = true): string
36+
{
37+
$_name = explode('\\', __CLASS__)[1];
38+
39+
return $lower ? str($_name)->snake()->lower()->toString() : str($_name)->snake()->toString();
40+
}
41+
42+
/**
43+
* Get pairs
44+
*/
45+
public static function pairs(): Collection
46+
{
47+
return collect(self::cases())->mapWithKeys(fn ($case) => [$case->value => $case->trans()]);
48+
}
49+
50+
/**
51+
* Translate case
52+
*/
53+
public function trans(): string
54+
{
55+
$key = 'v1.'.self::moduleName().'::enum.'.self::shortName().'.'.$this->attribute();
56+
$translation = __($key);
57+
return $key === $translation ? str($this->attribute())->title()->toString() : $translation;
58+
}
59+
60+
/**
61+
* Get the translation's file colors key
62+
*/
63+
public static function translationColorsKey(): string
64+
{
65+
return 'colors';
66+
}
67+
68+
/**
69+
* Get the case color
70+
*/
71+
public function color(): string|array
72+
{
73+
$key = 'v1.'.self::moduleName().'::enum.'.self::shortName().'.'.self::translationColorsKey().'.'.$this->attribute();
74+
$color = __($key);
75+
return $key === $color ? 'gray' : $color;
76+
}
77+
78+
/**
79+
* Get the translation's file icons key
80+
*/
81+
public static function translationIconsKey(): string
82+
{
83+
return 'icons';
84+
}
85+
86+
/**
87+
* Get the case icon
88+
*/
89+
public function icon(): string
90+
{
91+
$key = 'v1.'.self::moduleName().'::enum.'.self::shortName().'.'.self::translationIconsKey().'.'.$this->attribute();
92+
$icon = __($key);
93+
return $key === $icon ? 'heroicon-o-question-mark-circle' : $icon;
94+
}
95+
96+
97+
/**
98+
* Get case from name
99+
*/
100+
public static function fromName(string $name): self
101+
{
102+
foreach (self::cases() as $case)
103+
{
104+
if ($name === mb_strtolower($case->name))
105+
{
106+
return $case;
107+
}
108+
}
109+
110+
throw new ValueError("{$name} is not a valid value for backed-enum of ".self::class);
111+
}
112+
113+
/**
114+
* Get case from name with a try catch block
115+
*/
116+
public static function tryFromName(string $name): mixed
117+
{
118+
try
119+
{
120+
foreach (self::cases() as $case)
121+
{
122+
if ($name === mb_strtolower($case->name))
123+
{
124+
return $case;
125+
}
126+
}
127+
128+
return null;
129+
} catch (Throwable)
130+
{
131+
return null;
132+
}
133+
}
134+
135+
/**
136+
* Get all values
137+
*/
138+
public static function getAllValues(): array
139+
{
140+
return array_column(self::cases(), 'value');
141+
}
142+
143+
/**
144+
* Get all values with label
145+
*/
146+
public static function getAllValuesWithLabel(): array
147+
{
148+
return collect(self::cases())->map(fn ($case) => [
149+
'label' => __($case->value),
150+
'value' => $case->value,
151+
])->toArray();
152+
}
153+
154+
/**
155+
* Get the case value
156+
*/
157+
public function attribute(): mixed
158+
{
159+
return $this->value;
160+
}
161+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foundation\Support\Traits\V1\EventableEnum;
6+
7+
trait EventableEnum
8+
{
9+
/**
10+
* Get the event name
11+
*/
12+
public function event(): string
13+
{
14+
return $this->value;
15+
}
16+
17+
/**
18+
* Call the system event
19+
*/
20+
public function fire(...$params): void
21+
{
22+
event($this->value, $params);
23+
}
24+
}

0 commit comments

Comments
 (0)