Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Jan Kuchař
grifart-enum
Commits
2b2dd89d
Commit
2b2dd89d
authored
Jan 08, 2019
by
Jan Kuchař
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added implementation of __toString() method
parent
fb770fef
Pipeline
#14393
passed with stages
in 1 minute and 30 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
0 deletions
+49
-0
src/Enum.php
src/Enum.php
+8
-0
tests/Basic/accessing-scalar-value.phpt
tests/Basic/accessing-scalar-value.phpt
+41
-0
No files found.
src/Enum.php
View file @
2b2dd89d
...
...
@@ -123,6 +123,14 @@ abstract class Enum
return
$this
->
scalarValue
;
}
public
function
__toString
():
string
{
// as enum does not allow mixed key types (all must be int or all string),
// we can safely convert integers to strings without worrying introducing
// value conflicts
return
(
string
)
$this
->
toScalar
();
}
/**
* Retrieves constant name that is used to access enum value.
*
...
...
tests/Basic/accessing-scalar-value.phpt
0 → 100644
View file @
2b2dd89d
<?php
declare
(
strict_types
=
1
);
require
__DIR__
.
'/../bootstrap.php'
;
/**
* @method static EnumString VALUE1()
* @method static EnumString VALUE2()
*/
class
EnumString
extends
\
Grifart\Enum\Enum
{
use
Grifart\Enum\AutoInstances
;
protected
const
VALUE1
=
'value1'
;
protected
const
VALUE2
=
'value2'
;
}
\
Tester\Assert
::
same
(
'value1'
,
EnumString
::
VALUE1
()
->
toScalar
());
\
Tester\Assert
::
same
(
'value1'
,
(
string
)
EnumString
::
VALUE1
());
\
Tester\Assert
::
same
(
'value2'
,
EnumString
::
VALUE2
()
->
toScalar
());
\
Tester\Assert
::
same
(
'value2'
,
(
string
)
EnumString
::
VALUE2
());
/**
* @method static EnumInt VALUE1()
* @method static EnumInt VALUE2()
*/
class
EnumInt
extends
\
Grifart\Enum\Enum
{
use
Grifart\Enum\AutoInstances
;
protected
const
VALUE1
=
1
;
protected
const
VALUE2
=
2
;
}
\
Tester\Assert
::
same
(
1
,
EnumInt
::
VALUE1
()
->
toScalar
());
\
Tester\Assert
::
same
(
'1'
,
(
string
)
EnumInt
::
VALUE1
());
\
Tester\Assert
::
same
(
2
,
EnumInt
::
VALUE2
()
->
toScalar
());
\
Tester\Assert
::
same
(
'2'
,
(
string
)
EnumInt
::
VALUE2
());
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment