Skip to content

Commit 820ba0f

Browse files
Cookie, Env, Tame and Request Solid update - Same usage
1 parent 81129a6 commit 820ba0f

File tree

11 files changed

+501
-257
lines changed

11 files changed

+501
-257
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ Support Package For PHP and Laravel
176176
* [first](#collection-first)
177177
* [firstWhere](#collection-firstwhere)
178178
* [last](#collection-last)
179+
* [startsWith](#collection-startsWith)
180+
* [endsWith](#collection-endsWith)
181+
* [matchesAnyPrefixOf](#collection-matchesAnyPrefixOf)
179182
* [contains](#collection-contains)
180183
* [doesntContain](#collection-doesntcontain)
181184
* [every](#collection-every)

src/Capsule/CommandHelper.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ protected function isConsole(): bool
5353
return HttpRequest::runningInConsole();
5454
}
5555

56+
/**
57+
* Alias for `isConsole()` method
58+
* @param bool
59+
*/
60+
protected function runningInConsole(): bool
61+
{
62+
return $this->isConsole();
63+
}
64+
5665
/**
5766
* Check if database connection is successful.
5867
* @param \Tamedevelopers\Database\Connectors\Connector $conn
@@ -71,14 +80,14 @@ protected function checkConnection($conn): void
7180
}
7281

7382
/**
74-
* Determine if the current environment is production.
83+
* Determines if the application is running in a given environment.
84+
*
85+
* @param array|string $env
86+
* @param bool $strict
7587
*/
76-
protected function isProduction(): bool
88+
protected function environment($env = 'local', $strict = false): bool
7789
{
78-
$env = Env::env('APP_ENV');
79-
$productionAliases = ['prod', 'production', 'live'];
80-
81-
return in_array(Str::lower($env), $productionAliases, true);
90+
return Env::environment($env, $strict);
8291
}
8392

8493
/**
@@ -94,7 +103,7 @@ protected function forceChecker(): void
94103

95104
$force = (isset($args['force']) || isset($args['f']));
96105

97-
if ($this->isProduction()) {
106+
if ($this->environment('production')) {
98107
if (!$force) {
99108
$this->error("You are in production! Use [--force|-f] flag, to run this command.");
100109
if($this->isConsole()){

src/Collections/Traits/RelatedTrait.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,86 @@ public function last()
534534
return Str::last($this->items);
535535
}
536536

537+
/**
538+
* Check if any item in the collection starts with the given value.
539+
*
540+
* @param string $needle
541+
* @param bool $caseSensitive
542+
* @return bool
543+
*/
544+
public function startsWith(string $needle, bool $caseSensitive = true)
545+
{
546+
foreach ($this->items as $item) {
547+
if (!is_string($item)) {
548+
continue;
549+
}
550+
551+
$haystack = $caseSensitive ? $item : strtolower($item);
552+
$target = $caseSensitive ? $needle : strtolower($needle);
553+
554+
if (str_starts_with($haystack, $target)) {
555+
return true;
556+
}
557+
}
558+
559+
return false;
560+
}
561+
562+
/**
563+
* Check if any item in the collection ends with the given value.
564+
*
565+
* @param string $needle
566+
* @param bool $caseSensitive
567+
* @return bool
568+
*/
569+
public function endsWith(string $needle, bool $caseSensitive = true)
570+
{
571+
foreach ($this->items as $item) {
572+
if (!is_string($item)) {
573+
continue;
574+
}
575+
576+
$haystack = $caseSensitive ? $item : strtolower($item);
577+
$target = $caseSensitive ? $needle : strtolower($needle);
578+
579+
if (str_ends_with($haystack, $target)) {
580+
return true;
581+
}
582+
}
583+
584+
return false;
585+
}
586+
587+
/**
588+
* Return true if the provided $haystack starts with any item in the collection.
589+
*
590+
* Example: tcollect(['127.', '192.168.'])->matchesAnyPrefixOf('192.168.1.173') => true
591+
*
592+
* @param string $haystack
593+
* @param bool $caseSensitive
594+
* @return bool
595+
*/
596+
public function matchesAnyPrefixOf(string $haystack, bool $caseSensitive = true)
597+
{
598+
if (!$caseSensitive) {
599+
$haystack = strtolower($haystack);
600+
}
601+
602+
foreach ($this->items as $prefix) {
603+
if (!is_string($prefix)) {
604+
continue;
605+
}
606+
607+
$p = $caseSensitive ? $prefix : strtolower($prefix);
608+
609+
if (str_starts_with($haystack, $p)) {
610+
return true;
611+
}
612+
}
613+
614+
return false;
615+
}
616+
537617
/**
538618
* Determine if the collection contains a given value.
539619
*

0 commit comments

Comments
 (0)