Skip to content

Commit 8e27d86

Browse files
committed
✨ Feat: add Comparable trait
1 parent 06e6168 commit 8e27d86

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
lines changed

.php-cs-fixer.cache

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"8.1.11","version":"3.14.4:v3.14.4#1b3d9dba63d93b8a202c31e824748218781eae6b","indent":" ","lineEnding":"\n","rules":{"array_syntax":true,"backtick_to_shell_exec":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","case","continue","declare","default","exit","goto","include","include_once","phpdoc","require","require_once","return","switch","throw","try"]},"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"cast_spaces":true,"class_attributes_separation":{"elements":{"method":"one"}},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"class_reference_name_casing":true,"clean_namespace":true,"concat_space":{"spacing":"one"},"echo_tag_syntax":true,"empty_loop_body":true,"empty_loop_condition":true,"fully_qualified_strict_types":true,"function_typehint_space":true,"general_phpdoc_tag_rename":{"replacements":{"inheritDocs":"inheritDoc"}},"global_namespace_import":{"import_classes":true,"import_constants":false,"import_functions":false},"include":true,"increment_style":true,"integer_literal_case":true,"lambda_not_used_import":true,"linebreak_after_opening_tag":true,"magic_constant_casing":true,"magic_method_casing":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"native_function_casing":true,"native_function_type_declaration_casing":true,"no_alias_language_construct_call":true,"no_alternative_syntax":true,"no_binary_string":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["attribute","break","case","continue","curly_brace_block","default","extra","parenthesis_brace_block","return","square_brace_block","switch","throw","use"]},"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_superfluous_phpdoc_tags":{"allow_mixed":true,"allow_unused_params":true},"no_trailing_comma_in_singleline":true,"no_unneeded_control_parentheses":{"statements":["break","clone","continue","echo_print","negative_instanceof","others","return","switch_case","yield","yield_from"]},"no_unneeded_curly_braces":{"namespaces":true},"no_unneeded_import_alias":true,"no_unset_cast":true,"no_unused_imports":true,"no_useless_concat_operator":true,"no_useless_nullsafe_operator":true,"no_whitespace_before_comma_in_array":{"after_heredoc":true},"normalize_index_brace":true,"object_operator_without_whitespace":true,"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"php_unit_fqcn_annotation":true,"phpdoc_align":true,"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag_normalizer":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_order":{"order":["param","return","throws"]},"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_single_line_var_spacing":true,"phpdoc_tag_type":{"tags":{"inheritDoc":"inline"}},"phpdoc_to_comment":{"ignored_tags":["noinspection"]},"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_var_without_name":true,"protected_to_private":true,"semicolon_after_instruction":true,"simple_to_complex_string_variable":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":{"group_to_single_imports":false},"single_line_comment_spacing":true,"single_line_comment_style":true,"single_quote":true,"single_space_after_construct":{"constructs":["abstract","as","attribute","break","case","catch","class","clone","comment","const","const_import","continue","do","echo","else","elseif","enum","extends","final","finally","for","foreach","function","function_import","global","goto","if","implements","include","include_once","instanceof","insteadof","interface","match","named_argument","namespace","new","open_tag_with_echo","php_doc","php_open","print","private","protected","public","readonly","require","require_once","return","static","switch","throw","trait","try","type_colon","use","use_lambda","use_trait","var","while","yield","yield_from"]},"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"switch_continue_to_break":true,"trailing_comma_in_multiline":{"elements":["arrays","arguments","parameters"]},"trim_array_spaces":true,"types_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":{"ensure_single_space":true},"yoda_style":{"equal":false,"identical":false,"less_and_greater":false},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"align_multiline_comment":true,"array_indentation":true,"combine_consecutive_issets":true,"combine_consecutive_unsets":true,"escape_implicit_backslashes":true,"explicit_indirect_variable":true,"explicit_string_variable":true,"heredoc_to_nowdoc":true,"method_chaining_indentation":true,"multiline_comment_opening_closing":true,"multiline_whitespace_before_semicolons":{"strategy":"no_multi_line"},"no_null_property_initialization":true,"no_superfluous_elseif":true,"no_useless_else":true,"no_useless_return":true,"operator_linebreak":{"only_booleans":true},"phpdoc_add_missing_param_annotation":true,"phpdoc_no_empty_return":true,"phpdoc_order_by_value":true,"phpdoc_var_annotation_correct_order":true,"return_assignment":true,"octal_notation":true,"assign_null_coalescing_to_coalesce_equal":true,"list_syntax":true,"ternary_to_null_coalescing":true,"control_structure_continuation_position":true,"declare_parentheses":true,"general_phpdoc_annotation_remove":true,"nullable_type_declaration_for_default_null_value":true,"phpdoc_line_span":true,"simplified_if_return":true,"simplified_null_return":true},"hashes":{"tests\/HasDescriptionTest.php":"d1ba2b750601f4ea2e8e0d1d694ab8fa","tests\/HasValuesTest.php":"104721d27b85552136e326e01c2d9fcc","tests\/Enums\/Status.php":"33c2bfa1830e75ef3bfb241bfb08f617","tests\/Enums\/Capital.php":"f6d423d0487a25f900bbcd5e8056caed","tests\/HasLabelTest.php":"d118d32028c98c678537abbaaf37ac6b","src\/HasDescription.php":"ed8e4e777c730d8e653033274ffc9ebf","src\/HasValues.php":"29eacd73070310e204c93d169887ac8e","src\/HasLabel.php":"9c8910fcd0bf57e9e1595caa24d83686","tests\/HasNamesTest.php":"288d8de00e0b297e26867f8af2bf721a","src\/HasNames.php":"347fecea103d8b7e2a2c4b4653856529"}}
1+
{"php":"8.1.11","version":"3.14.4:v3.14.4#1b3d9dba63d93b8a202c31e824748218781eae6b","indent":" ","lineEnding":"\n","rules":{"array_syntax":true,"backtick_to_shell_exec":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","case","continue","declare","default","exit","goto","include","include_once","phpdoc","require","require_once","return","switch","throw","try"]},"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"cast_spaces":true,"class_attributes_separation":{"elements":{"method":"one"}},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"class_reference_name_casing":true,"clean_namespace":true,"concat_space":{"spacing":"one"},"echo_tag_syntax":true,"empty_loop_body":true,"empty_loop_condition":true,"fully_qualified_strict_types":true,"function_typehint_space":true,"general_phpdoc_tag_rename":{"replacements":{"inheritDocs":"inheritDoc"}},"global_namespace_import":{"import_classes":true,"import_constants":false,"import_functions":false},"include":true,"increment_style":true,"integer_literal_case":true,"lambda_not_used_import":true,"linebreak_after_opening_tag":true,"magic_constant_casing":true,"magic_method_casing":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"native_function_casing":true,"native_function_type_declaration_casing":true,"no_alias_language_construct_call":true,"no_alternative_syntax":true,"no_binary_string":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["attribute","break","case","continue","curly_brace_block","default","extra","parenthesis_brace_block","return","square_brace_block","switch","throw","use"]},"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_superfluous_phpdoc_tags":{"allow_mixed":true,"allow_unused_params":true},"no_trailing_comma_in_singleline":true,"no_unneeded_control_parentheses":{"statements":["break","clone","continue","echo_print","negative_instanceof","others","return","switch_case","yield","yield_from"]},"no_unneeded_curly_braces":{"namespaces":true},"no_unneeded_import_alias":true,"no_unset_cast":true,"no_unused_imports":true,"no_useless_concat_operator":true,"no_useless_nullsafe_operator":true,"no_whitespace_before_comma_in_array":{"after_heredoc":true},"normalize_index_brace":true,"object_operator_without_whitespace":true,"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"php_unit_fqcn_annotation":true,"phpdoc_align":true,"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag_normalizer":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_order":{"order":["param","return","throws"]},"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_single_line_var_spacing":true,"phpdoc_tag_type":{"tags":{"inheritDoc":"inline"}},"phpdoc_to_comment":{"ignored_tags":["noinspection"]},"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_var_without_name":true,"protected_to_private":true,"semicolon_after_instruction":true,"simple_to_complex_string_variable":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":{"group_to_single_imports":false},"single_line_comment_spacing":true,"single_line_comment_style":true,"single_quote":true,"single_space_after_construct":{"constructs":["abstract","as","attribute","break","case","catch","class","clone","comment","const","const_import","continue","do","echo","else","elseif","enum","extends","final","finally","for","foreach","function","function_import","global","goto","if","implements","include","include_once","instanceof","insteadof","interface","match","named_argument","namespace","new","open_tag_with_echo","php_doc","php_open","print","private","protected","public","readonly","require","require_once","return","static","switch","throw","trait","try","type_colon","use","use_lambda","use_trait","var","while","yield","yield_from"]},"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"switch_continue_to_break":true,"trailing_comma_in_multiline":{"elements":["arrays","arguments","parameters"]},"trim_array_spaces":true,"types_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":{"ensure_single_space":true},"yoda_style":{"equal":false,"identical":false,"less_and_greater":false},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"align_multiline_comment":true,"array_indentation":true,"combine_consecutive_issets":true,"combine_consecutive_unsets":true,"escape_implicit_backslashes":true,"explicit_indirect_variable":true,"explicit_string_variable":true,"heredoc_to_nowdoc":true,"method_chaining_indentation":true,"multiline_comment_opening_closing":true,"multiline_whitespace_before_semicolons":{"strategy":"no_multi_line"},"no_null_property_initialization":true,"no_superfluous_elseif":true,"no_useless_else":true,"no_useless_return":true,"operator_linebreak":{"only_booleans":true},"phpdoc_add_missing_param_annotation":true,"phpdoc_no_empty_return":true,"phpdoc_order_by_value":true,"phpdoc_var_annotation_correct_order":true,"return_assignment":true,"octal_notation":true,"assign_null_coalescing_to_coalesce_equal":true,"list_syntax":true,"ternary_to_null_coalescing":true,"control_structure_continuation_position":true,"declare_parentheses":true,"general_phpdoc_annotation_remove":true,"nullable_type_declaration_for_default_null_value":true,"phpdoc_line_span":true,"simplified_if_return":true,"simplified_null_return":true},"hashes":{"tests\/HasDescriptionTest.php":"d1ba2b750601f4ea2e8e0d1d694ab8fa","tests\/HasValuesTest.php":"104721d27b85552136e326e01c2d9fcc","tests\/Enums\/Status.php":"174ae1ed85b8dba3741a178dea66af1f","tests\/Enums\/Capital.php":"f6d423d0487a25f900bbcd5e8056caed","tests\/HasLabelTest.php":"d118d32028c98c678537abbaaf37ac6b","src\/HasDescription.php":"ed8e4e777c730d8e653033274ffc9ebf","src\/HasValues.php":"29eacd73070310e204c93d169887ac8e","src\/HasLabel.php":"9c8910fcd0bf57e9e1595caa24d83686","tests\/HasNamesTest.php":"288d8de00e0b297e26867f8af2bf721a","src\/HasNames.php":"347fecea103d8b7e2a2c4b4653856529","tests\/ComparableTest.php":"bb59804b5417ae4dbd5879e78d89919a","src\/Comparable.php":"a6bc70a3e95df82027f53e946417f49b"}}

.phpunit.result.cache

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":1,"defects":[],"times":{"Fuwasegu\\PhpEnumUtil\\Tests\\HasDescriptionTest::test_description":0.005,"Fuwasegu\\PhpEnumUtil\\Tests\\HasDescriptionTest::test_descriptions":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasLabelTest::test_label":0.001,"Fuwasegu\\PhpEnumUtil\\Tests\\HasLabelTest::test_labels":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasNamesTest::test":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasValuesTest::test_int_backed_enum":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasValuesTest::test_string_backed_enum":0}}
1+
{"version":1,"defects":{"Fuwasegu\\PhpEnumUtil\\Tests\\ComparableTest::test_isFrom":4,"Fuwasegu\\PhpEnumUtil\\Tests\\ComparableTest::test_isNotFrom":4},"times":{"Fuwasegu\\PhpEnumUtil\\Tests\\HasDescriptionTest::test_description":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasDescriptionTest::test_descriptions":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasLabelTest::test_label":0.001,"Fuwasegu\\PhpEnumUtil\\Tests\\HasLabelTest::test_labels":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasNamesTest::test":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasValuesTest::test_int_backed_enum":0,"Fuwasegu\\PhpEnumUtil\\Tests\\HasValuesTest::test_string_backed_enum":0,"Fuwasegu\\PhpEnumUtil\\Tests\\ComparableTest::test_is":0.003,"Fuwasegu\\PhpEnumUtil\\Tests\\ComparableTest::test_isNot":0,"Fuwasegu\\PhpEnumUtil\\Tests\\ComparableTest::test_isFrom":0,"Fuwasegu\\PhpEnumUtil\\Tests\\ComparableTest::test_isNotFrom":0}}

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,35 @@ Status::names();
134134

135135
// Join Enum names with a string
136136
Status::implodeNames();
137+
```
138+
139+
### Comparable trait
140+
This trait provides a method for easy comparison of Bakced Enums
141+
142+
```php
143+
enum Status: string
144+
{
145+
use Comparable;
146+
147+
case ACTIVE = 'active';
148+
149+
case INACTIVE = 'inactive';
150+
151+
case RETIED = 'retired';
152+
}
153+
```
154+
155+
```php
156+
// Compare Enum peer to peer
157+
$maybeActive = Status::ACTIVE;
158+
Status::ACTIVE->is($maybeActive); // true
159+
Status::INACTIVE->isNot($maybeActive); // false
160+
161+
// Attempts to convert the compared int or string to an Enum with Enum::tryFrom before comparing
162+
// If tryFrom is null, `isFrom()` returns false and `isNotFrom()` returns true.
163+
$value = 'active';
164+
Status::ACTIVE->isFrom($value); // true
165+
Status::INACTIVE->isNotFrom($value); // false
166+
Status::ACTIVE->isFrom('foo'); // false
167+
Status::ACTIVE->isNotFrom('foo') // true
137168
```

src/Comparable.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fuwasegu\PhpEnumUtil;
6+
7+
trait Comparable
8+
{
9+
public function is(self $enum): bool
10+
{
11+
return $this === $enum;
12+
}
13+
14+
public function isNot(self $enum): bool
15+
{
16+
return !$this->is($enum);
17+
}
18+
19+
public function isFrom(int|string $value): bool
20+
{
21+
return !is_null($enum = self::tryFrom($value)) && $this->is($enum);
22+
}
23+
24+
public function isNotFrom(int|string $value): bool
25+
{
26+
return !$this->isFrom($value);
27+
}
28+
}

tests/ComparableTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fuwasegu\PhpEnumUtil\Tests;
6+
7+
use Fuwasegu\PhpEnumUtil\Tests\Enums\Status;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ComparableTest extends TestCase
11+
{
12+
public function test_is(): void
13+
{
14+
$maybeActive = Status::ACTIVE;
15+
$this->assertTrue(Status::ACTIVE->is($maybeActive));
16+
$this->assertFalse(Status::INACTIVE->is($maybeActive));
17+
}
18+
19+
public function test_isNot(): void
20+
{
21+
$maybeActive = Status::ACTIVE;
22+
$this->assertFalse(Status::ACTIVE->isNot($maybeActive));
23+
$this->assertTrue(Status::INACTIVE->isNot($maybeActive));
24+
}
25+
26+
public function test_isFrom(): void
27+
{
28+
$value = 'active';
29+
$this->assertTrue(Status::ACTIVE->isFrom($value));
30+
$this->assertFalse(Status::INACTIVE->isFrom($value));
31+
$this->assertFalse(Status::ACTIVE->isFrom('foo'));
32+
}
33+
34+
public function test_isNotFrom(): void
35+
{
36+
$value = 'active';
37+
$this->assertFalse(Status::ACTIVE->isNotFrom($value));
38+
$this->assertTrue(Status::INACTIVE->isNotFrom($value));
39+
$this->assertTrue(Status::ACTIVE->isNotFrom('foo'));
40+
}
41+
}

tests/Enums/Status.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace Fuwasegu\PhpEnumUtil\Tests\Enums;
66

7+
use Fuwasegu\PhpEnumUtil\Comparable;
78
use Fuwasegu\PhpEnumUtil\HasDescription;
89
use Fuwasegu\PhpEnumUtil\HasValues;
910

1011
enum Status: string
1112
{
1213
use HasDescription;
1314
use HasValues;
15+
use Comparable;
1416

1517
case ACTIVE = 'active';
1618

0 commit comments

Comments
 (0)