8000 [12.x] Add `Rule::contains` by stevebauman · Pull Request #55809 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add Rule::contains #55809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump 8000 to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Rule::contains
  • Loading branch information
stevebauman committed May 21, 2025
commit f732003bbcf80fa369aec147af33ebb324d2815f
15 changes: 15 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ public static function anyOf($rules)
return new AnyOf($rules);
}

/**
* Get a contains rule builder instance.
*
* @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\UnitEnum|array|string $values
* @return \Illuminate\Validation\Rules\Contains
*/
public static function contains($values)
{
if ($values instanceof Arrayable) {
$values = $values->toArray();
}

return new Rules\Contains(is_array($values) ? $values : func_get_args());
}

/**
* Compile a set of rules for an attribute.
*
Expand Down
49 changes: 49 additions & 0 deletions src/Illuminate/Validation/Rules/Contains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Illuminate\Validation\Rules;

use Illuminate\Contracts\Support\Arrayable;
use Stringable;

use function Illuminate\Support\enum_value;

class Contains implements Stringable
{
/**
* The values that should be contained in the attribute.
*
* @var array
*/
protected $values;

/**
* Create a new contains rule instance.
*
* @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\UnitEnum|array|string $values
* @return void
*/
public function __construct($values)
{
if ($values instanceof Arrayable) {
$values = $values->toArray();
}

$this->values = is_array($values) ? $values : func_get_args();
}

/**
* Convert the rule to a validation string.
*
* @return string
*/
public function __toString()
{
$values = array_map(function ($value) {
$value = enum_value($value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevebauman doesnt that reference a method registered in orchestra? Not sure if that dependency is wanted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patriziotomato

function enum_value($value, $default = null)


return '"'.str_replace('"', '""', $value).'"';
}, $this->values);

return 'contains:'.implode(',', $values);
}
}
0