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
ea295f89
Commit
ea295f89
authored
Jan 09, 2019
by
Jan Kuchař
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'better-access-to-scalar-value' into 'master'
Better access to scalar value See merge request
!10
parents
3f0d7920
2b2dd89d
Pipeline
#14394
passed with stages
in 37 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
63 additions
and
17 deletions
+63
-17
src/Enum.php
src/Enum.php
+17
-9
src/Internal/Meta.php
src/Internal/Meta.php
+2
-2
src/exceptions.php
src/exceptions.php
+0
-3
tests/Basic/accessing-scalar-value.phpt
tests/Basic/accessing-scalar-value.phpt
+41
-0
tests/Basic/autoinstances.phpt
tests/Basic/autoinstances.phpt
+1
-1
tests/Reflection/constantNames.inherited.phpt
tests/Reflection/constantNames.inherited.phpt
+2
-2
No files found.
src/Enum.php
View file @
ea295f89
...
...
@@ -104,23 +104,31 @@ abstract class Enum
// -------- INSTANCE IMPLEMENTATION ---------
/** @var int|string */
private
$scalar
;
private
$scalar
Value
;
/**
* @param int|string $scalar
* @param int|string $scalar
Value
*/
protected
function
__construct
(
$scalar
)
protected
function
__construct
(
$scalar
Value
)
{
$this
->
scalar
=
$scalar
;
$this
->
scalar
Value
=
$scalar
Value
;
}
/**
*
Provide
s scalar representation of enum value.
*
Return
s scalar representation of enum value.
* @return int|string
*/
public
function
ge
tScalar
()
public
function
t
o
Scalar
()
{
return
$this
->
scalar
;
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
();
}
/**
...
...
@@ -132,7 +140,7 @@ abstract class Enum
public
function
getConstantName
():
string
{
return
self
::
getMeta
()
->
getConstantNameForScalar
(
$this
->
ge
tScalar
()
$this
->
t
o
Scalar
()
);
}
...
...
@@ -152,6 +160,6 @@ abstract class Enum
*/
public
function
scalarEquals
(
$theOtherScalarValue
):
bool
{
return
$this
->
ge
tScalar
()
===
$theOtherScalarValue
;
return
$this
->
t
o
Scalar
()
===
$theOtherScalarValue
;
}
}
src/Internal/Meta.php
View file @
ea295f89
...
...
@@ -39,7 +39,7 @@ final class Meta
// check type of all scalar values
$keyType
=
null
;
foreach
(
$values
as
$value
)
{
$scalar
=
$value
->
ge
tScalar
();
$scalar
=
$value
->
t
o
Scalar
();
if
(
$keyType
===
NULL
)
{
$keyType
=
\
gettype
(
$scalar
);
}
...
...
@@ -49,7 +49,7 @@ final class Meta
}
foreach
(
$values
as
$value
)
{
$scalar
=
$value
->
ge
tScalar
();
$scalar
=
$value
->
t
o
Scalar
();
if
(
isset
(
$scalarToValues
[
$scalar
]))
{
throw
new
UsageException
(
'You have provided duplicated scalar values.'
);
}
...
...
src/exceptions.php
View file @
ea295f89
...
...
@@ -11,9 +11,6 @@
namespace
Grifart\Enum
;
// Project root exceptions:
use
Throwable
;
class
UsageException
extends
\
LogicException
{}
abstract
class
RuntimeException
extends
\
RuntimeException
{}
...
...
tests/Basic/accessing-scalar-value.phpt
0 → 100644
View file @
ea295f89
<?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
());
tests/Basic/autoinstances.phpt
View file @
ea295f89
...
...
@@ -23,5 +23,5 @@ class OrderState extends \Grifart\Enum\Enum
\
Tester\Assert
::
notSame
(
OrderState
::
NEW
(),
OrderState
::
DELIVERED
());
\
Tester\Assert
::
notSame
(
OrderState
::
ACTIVE
(),
OrderState
::
DELIVERED
());
\
Tester\Assert
::
same
(
'active'
,
OrderState
::
ACTIVE
()
->
ge
tScalar
());
\
Tester\Assert
::
same
(
'active'
,
OrderState
::
ACTIVE
()
->
t
o
Scalar
());
\
Tester\Assert
::
same
(
OrderState
::
ACTIVE
(),
OrderState
::
fromScalar
(
'active'
));
tests/Reflection/constantNames.inherited.phpt
View file @
ea295f89
...
...
@@ -29,5 +29,5 @@ abstract class ReflectionConstantNames2 extends \Grifart\Enum\Enum
}
}
\
Tester\Assert
::
same
(
'new'
,
ReflectionConstantNames2
::
NEW
()
->
ge
tScalar
());
\
Tester\Assert
::
same
(
'active'
,
ReflectionConstantNames2
::
ACTIVE
()
->
ge
tScalar
());
\
Tester\Assert
::
same
(
'new'
,
ReflectionConstantNames2
::
NEW
()
->
t
o
Scalar
());
\
Tester\Assert
::
same
(
'active'
,
ReflectionConstantNames2
::
ACTIVE
()
->
t
o
Scalar
());
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