Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 2e72706

Browse files
author
Andrey Helldar
committed
Small refactored
1 parent 85ecf56 commit 2e72706

File tree

4 files changed

+77
-30
lines changed

4 files changed

+77
-30
lines changed

src/Support/Container.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Helldar\ApiResponse\Support;
4+
5+
abstract class Container
6+
{
7+
protected static $containers = [];
8+
9+
/**
10+
* @param object|string $class
11+
*
12+
* @return object|null
13+
*/
14+
protected static function makeContainer($class)
15+
{
16+
$name = static::containerName($class);
17+
18+
if (! isset(static::$containers[$name])) {
19+
static::$containers[$name] = Is::object($class) ? $class : new $class;
20+
}
21+
22+
return static::$containers[$name];
23+
}
24+
25+
/**
26+
* @param object|string $class
27+
*
28+
* @return string
29+
*/
30+
protected static function containerName($class): string
31+
{
32+
return Is::object($class) ? get_class($class) : $class;
33+
}
34+
}

src/Support/Exception.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public static function getType(Throwable $class): string
6161

6262
public static function getData($exception)
6363
{
64-
if ($exception instanceof SymfonyResponse || $exception instanceof LaravelResponse) {
64+
if (Instance::of($exception, [SymfonyResponse::class, LaravelResponse::class])) {
6565
return Instance::callsWhenNotEmpty($exception, ['getOriginalContent', 'getContent', 'getMessage']);
6666
}
6767

68-
if ($exception instanceof Responsable || $exception instanceof HttpResponseException) {
68+
if (Instance::of($exception, [Responsable::class, HttpResponseException::class])) {
6969
return $exception->getResponse();
7070
}
7171

72-
if ($exception instanceof ValidationException) {
72+
if (Instance::of($exception, ValidationException::class)) {
7373
return $exception->errors();
7474
}
7575

src/Support/Instance.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
use ReflectionClass;
77
use Throwable;
88

9-
class Instance
9+
final class Instance extends Container
1010
{
11-
protected static $instances = [];
12-
1311
/**
1412
* @param mixed $haystack
1513
* @param array|string $needles
@@ -24,17 +22,17 @@ public static function of($haystack, $needles): bool
2422
return false;
2523
}
2624

25+
$reflection = static::reflection($haystack);
26+
2727
foreach (Arr::wrap($needles) as $needle) {
2828
if (! static::isClassExists($needle)) {
2929
continue;
3030
}
3131

32-
$reflection = static::reflection($haystack);
33-
3432
if (
35-
$reflection->isInstance(static::make($needle)) ||
33+
$reflection->isInstance(static::makeContainer($needle)) ||
3634
$reflection->isSubclassOf($needle) ||
37-
$reflection->implementsInterface($needle)
35+
(Is::contract($reflection) && $reflection->implementsInterface($needle))
3836
) {
3937
return true;
4038
}
@@ -99,7 +97,7 @@ protected static function isExistsObject($value): bool
9997
*/
10098
protected static function isClassExists($class = null): bool
10199
{
102-
return is_string($class) && class_exists($class);
100+
return Is::string($class) && class_exists($class);
103101
}
104102

105103
/**
@@ -109,7 +107,7 @@ protected static function isClassExists($class = null): bool
109107
*/
110108
protected static function isObject($value): bool
111109
{
112-
return is_object($value);
110+
return Is::object($value);
113111
}
114112

115113
/**
@@ -123,22 +121,4 @@ protected static function reflection(Throwable $class): ReflectionClass
123121
{
124122
return new ReflectionClass($class);
125123
}
126-
127-
/**
128-
* @param string|Throwable $class
129-
*
130-
* @return Throwable
131-
*/
132-
protected static function make($class): Throwable
133-
{
134-
$is_object = static::isObject($class);
135-
136-
$name = $is_object ? get_class($class) : $class;
137-
138-
if (! isset(static::$instances[$name])) {
139-
static::$instances[$name] = $is_object ? $class : new $class;
140-
}
141-
142-
return static::$instances[$name];
143-
}
144124
}

src/Support/Is.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Helldar\ApiResponse\Support;
4+
5+
use ReflectionClass;
6+
7+
final class Is
8+
{
9+
public static function object($class): bool
10+
{
11+
return is_object($class);
12+
}
13+
14+
public static function string($class): bool
15+
{
16+
return is_string($class);
17+
}
18+
19+
/**
20+
* @param $class
21+
*
22+
* @throws \ReflectionException
23+
* @return bool
24+
*/
25+
public static function contract($class): bool
26+
{
27+
if (! ($class instanceof ReflectionClass)) {
28+
$class = new ReflectionClass($class);
29+
}
30+
31+
return $class->isInterface();
32+
}
33+
}

0 commit comments

Comments
 (0)