Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Consul/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
namespace FriendsOfHyperf\Jet\Consul;

use FriendsOfHyperf\Jet\Exception\ServerException;
use FriendsOfHyperf\Jet\Support\Arr;
use Psr\Http\Message\ResponseInterface;

use function FriendsOfHyperf\Jet\array_get;

class Response
{
/**
Expand Down Expand Up @@ -57,7 +56,7 @@ public function json(?string $key = null, $default = null)
return $this->decoded;
}

return array_get($this->decoded, $key, $default);
return Arr::get($this->decoded, $key, $default);
}

/**
Expand Down
154 changes: 0 additions & 154 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,68 +104,6 @@ function with($value, ?callable $callback = null) // @phpstan-ignore-line
return is_null($callback) ? $value : $callback($value);
}

/**
* @param string $delimiter
* @return string
*/
function str_snake(string $value, $delimiter = '_')
{
if (! ctype_lower($value)) {
$value = preg_replace('/\s+/u', '', ucwords($value));
$value = str_lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
}

return $value;
}

/**
* @return string
*/
function str_lower(string $value)
{
return mb_strtolower($value, 'UTF-8');
}

/**
* @return string
*/
function str_studly(string $value, string $gap = '')
{
$value = ucwords(str_replace(['-', '_'], ' ', $value));

return str_replace(' ', $gap, $value);
}

/**
* @return string
*/
function str_replace_first(string $search, string $replace, string $subject)
{
if ($search == '') {
return $subject;
}

$position = strpos($subject, $search);

if ($position !== false) {
return substr_replace($subject, $replace, $position, strlen($search));
}

return $subject;
}

/**
* @return string
*/
function str_replace_array(string $search, array $replace, string $subject)
{
foreach ($replace as $value) {
$subject = str_replace_first($search, (string) $value, $subject);
}

return $subject;
}

/**
* @param mixed $value
* @return mixed
Expand All @@ -174,95 +112,3 @@ function value($value)
{
return $value instanceof \Closure ? $value() : $value;
}

/**
* Get an item from an array using "dot" notation.
*
* @param array|\ArrayAccess $array
* @param null|int|string $key
* @param mixed $default
*/
function array_get($array, $key = null, $default = null)
{
if (is_null($key)) {
return $array;
}

if (isset($array[$key])) {
return $array[$key];
}

if (! is_string($key) || strpos($key, '.') === false) {
return $array[$key] ?? value($default);
}

foreach (explode('.', $key) as $segment) {
if (array_accessible($array) && array_exists($array, $segment)) {
$array = $array[$segment];
} else {
return value($default);
}
}

return $array;
}

/**
* Check if an item or items exist in an array using "dot" notation.
*
* @param array|\ArrayAccess $array
* @param null|array|string $keys
*/
function array_has($array, $keys)
{
if (is_null($keys)) {
return false;
}

$keys = (array) $keys;

if (! $array || $keys === []) {
return false;
}

foreach ($keys as $key) {
$subKeyArray = $array;

if (array_exists($array, $key)) {
continue;
}

foreach (explode('.', $key) as $segment) {
if (array_accessible($subKeyArray) && array_exists($subKeyArray, $segment)) {
$subKeyArray = $subKeyArray[$segment];
} else {
return false;
}
}
}

return true;
}

/**
* @param mixed $array
* @param int|string $key
* @return bool
*/
function array_exists($array, $key)
{
if ($array instanceof \ArrayAccess) {
return $array->offsetExists($key);
}

return array_key_exists($key, $array);
}

/**
* @param mixed $value
* @return bool
*/
function array_accessible($value)
{
return is_array($value) || $value instanceof \ArrayAccess;
}
10 changes: 4 additions & 6 deletions src/PathGenerator/DotPathGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@
namespace FriendsOfHyperf\Jet\PathGenerator;

use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface;

use function FriendsOfHyperf\Jet\str_replace_array;
use function FriendsOfHyperf\Jet\str_studly;
use FriendsOfHyperf\Jet\Support\Str;

class DotPathGenerator implements PathGeneratorInterface
{
public function generate(string $service, string $method): string
{
$handledNamespace = explode('\\', $service);
$handledNamespace = str_replace_array('\\', ['/'], end($handledNamespace));
$path = str_studly($handledNamespace);
$handledNamespace = Str::replaceArray('\\', ['/'], end($handledNamespace));
$path = Str::studly($handledNamespace);

return $path . '.' . str_studly($method);
return $path . '.' . Str::studly($method);
}
}
5 changes: 2 additions & 3 deletions src/PathGenerator/PathGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
namespace FriendsOfHyperf\Jet\PathGenerator;

use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface;

use function FriendsOfHyperf\Jet\str_snake;
use FriendsOfHyperf\Jet\Support\Str;

class PathGenerator implements PathGeneratorInterface
{
Expand All @@ -22,7 +21,7 @@ public function generate(string $service, string $method): string
$handledNamespace = explode('\\', $service);
$handledNamespace = str_replace('\\', '/', end($handledNamespace));
$handledNamespace = str_replace('Service', '', $handledNamespace);
$path = str_snake($handledNamespace);
$path = Str::snake($handledNamespace);

if ($path[0] !== '/') {
$path = '/' . $path;
Expand Down
14 changes: 7 additions & 7 deletions src/Registry/ConsulRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
use FriendsOfHyperf\Jet\Contract\RegistryInterface;
use FriendsOfHyperf\Jet\LoadBalancer\Node;
use FriendsOfHyperf\Jet\LoadBalancer\RoundRobin;
use FriendsOfHyperf\Jet\Support\Arr;
use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter;
use FriendsOfHyperf\Jet\Transporter\StreamSocketTransporter;
use GuzzleHttp\Client;

use function FriendsOfHyperf\Jet\array_get;
use function FriendsOfHyperf\Jet\retry;
use function FriendsOfHyperf\Jet\with;

Expand Down Expand Up @@ -120,21 +120,21 @@ public function getServiceNodes(string $service, ?string $protocol = null)
$nodes = [];

foreach ($serviceNodes as $node) {
if (array_get($node, 'Checks.1.Status') != 'passing') {
if (Arr::get($node, 'Checks.1.Status') != 'passing') {
continue;
}

if (! is_null($protocol) && $protocol != array_get($node, 'Service.Meta.Protocol')) {
if (! is_null($protocol) && $protocol != Arr::get($node, 'Service.Meta.Protocol')) {
continue;
}

$nodes[] = new Node(
array_get($node, 'Service.Address'),
(int) array_get($node, 'Service.Port'),
Arr::get($node, 'Service.Address'),
(int) Arr::get($node, 'Service.Port'),
1,
[
'type' => array_get($node, 'Checks.1.Type'),
'protocol' => array_get($node, 'Service.Meta.Protocol'),
'type' => Arr::get($node, 'Checks.1.Type'),
'protocol' => Arr::get($node, 'Service.Meta.Protocol'),
]
);
}
Expand Down
111 changes: 111 additions & 0 deletions src/Support/Arr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/jet.
*
* @link https://github.com/friendsofhyperf/jet
* @document https://github.com/friendsofhyperf/jet/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Jet\Support;

use function FriendsOfHyperf\Jet\value;

class Arr
{
/**
* Get an item from an array using "dot" notation.
*
* @param array|\ArrayAccess $array
* @param null|int|string $key
* @param mixed $default
* @return mixed
*/
public static function get($array, $key = null, $default = null)
{
if (is_null($key)) {
return $array;
}

if (isset($array[$key])) {
return $array[$key];
}

if (! is_string($key) || strpos($key, '.') === false) {
return isset($array[$key]) ? $array[$key] : value($default);
}

foreach (explode('.', $key) as $segment) {
if (static::accessible($array) && static::exists($array, $segment)) {
$array = $array[$segment];
} else {
return value($default);
}
}

return $array;
}

/**
* Check if an item or items exist in an array using "dot" notation.
*
* @param array|\ArrayAccess $array
* @param null|array|string $keys
* @return bool
*/
public static function has($array, $keys)
{
if (is_null($keys)) {
return false;
}

$keys = (array) $keys;

if (! $array || $keys === []) {
return false;
}

foreach ($keys as $key) {
$subKeyArray = $array;

if (static::exists($array, $key)) {
continue;
}

foreach (explode('.', $key) as $segment) {
if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
$subKeyArray = $subKeyArray[$segment];
} else {
return false;
}
}
}

return true;
}

/**
* @param mixed $array
* @param int|string $key
* @return bool
*/
public static function exists($array, $key)
{
if ($array instanceof \ArrayAccess) {
return $array->offsetExists($key);
}

return array_key_exists($key, $array);
}

/**
* @param mixed $value
* @return bool
*/
public static function accessible($value)
{
return is_array($value) || $value instanceof \ArrayAccess;
}
}
Loading