Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
php-modules
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
library
php-modules
Commits
3ba26154
Commit
3ba26154
authored
3 years ago
by
Hoop, Bert Jan de
Browse files
Options
Downloads
Patches
Plain Diff
L01ALG-1072 add AttributesTrait
parent
96387230
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Library/AttributesTrait.php
+81
-0
81 additions, 0 deletions
src/Library/AttributesTrait.php
tests/Library/AttributesTraitTest.php
+99
-0
99 additions, 0 deletions
tests/Library/AttributesTraitTest.php
with
180 additions
and
0 deletions
src/Library/AttributesTrait.php
0 → 100644
+
81
−
0
View file @
3ba26154
<?php
namespace
Library
;
/**
* Trait that adds support for getting/setting attributes/properties.
* Useful for settings from a configuration file.
* And no need to create getter/setter for each attribute.
*/
trait
AttributesTrait
{
/**
* Attributes of instance
*
* @var array with key, value pairs
*/
protected
$attributes
=
[];
/**
* Add attributes to existing attributes
*
* @param array|\ArrayAccess $attributes
* @return $this
*/
public
function
addAttributes
(
$attributes
)
{
// array_merge does not work on \ArrayAccess objects (settings in Slim framework)
foreach
(
$attributes
as
$key
=>
$value
)
{
$this
->
attributes
[
$key
]
=
$value
;
}
return
$this
;
}
/**
* Set attributes, replaces all existing attributes
*
* @param array|\ArrayAccess $attributes new attributes
* @return $this
*/
public
function
setAttributes
(
$attributes
)
{
$this
->
attributes
=
[];
return
$this
->
addAttributes
(
$attributes
);
}
/**
* Return array with all attributes
*
* @return array key/value array with all attributes
*/
public
function
getAttributes
():
array
{
return
$this
->
attributes
;
}
/**
* Set a key/value pair to the attributes
*
* @param string $key
* @param mixed $value
* @return $this
*/
public
function
setAttribute
(
string
$key
,
$value
)
{
$this
->
attributes
[
$key
]
=
$value
;
return
$this
;
}
/**
* Return the value of the attribute with given key
* if the attribute is not set then return the given default value
*
* @param string $key
* @param mixed $default
* @return mixed value of attribute with given key of the default value
*/
public
function
getAttribute
(
string
$key
,
$default
=
''
)
{
return
key_exists
(
$key
,
$this
->
attributes
)
?
$this
->
attributes
[
$key
]
:
$default
;
}
}
This diff is collapsed.
Click to expand it.
tests/Library/AttributesTraitTest.php
0 → 100644
+
99
−
0
View file @
3ba26154
<?php
// phpcs:disable Generic.Files.LineLength
namespace
Tests\App
;
class
AttributesTraitTest
extends
\PHPUnit\Framework\TestCase
{
private
$instance
;
protected
function
setUp
():
void
{
$this
->
instance
=
new
class
{
use
\Library\AttributesTrait
;
};
}
public
function
testDefaultAttributes
()
{
$this
->
assertEquals
([],
$this
->
instance
->
getAttributes
(),
'default attributes'
);
}
public
function
testAddAttributes
()
{
$attribs
=
[
"a"
=>
1
,
];
$this
->
instance
->
addAttributes
(
$attribs
);
$this
->
assertEquals
(
$attribs
,
$this
->
instance
->
getAttributes
(),
'first set attributes'
);
$attribs
=
[
"b"
=>
2
,
"c"
=>
3
,
];
$this
->
instance
->
addAttributes
(
$attribs
);
$expect
=
[
"a"
=>
1
,
"b"
=>
2
,
"c"
=>
3
,
];
$this
->
assertEquals
(
$expect
,
$this
->
instance
->
getAttributes
(),
'second set attributes'
);
}
public
function
testSetAttributes
()
{
$attribs
=
[
"a"
=>
1
,
];
$this
->
instance
->
setAttributes
(
$attribs
);
$this
->
assertEquals
(
$attribs
,
$this
->
instance
->
getAttributes
(),
'first set attributes'
);
$attribs
=
[
"b"
=>
2
,
"c"
=>
3
,
];
$this
->
instance
->
setAttributes
(
$attribs
);
$this
->
assertEquals
(
$attribs
,
$this
->
instance
->
getAttributes
(),
'second set attributes'
);
}
public
function
testSetAttribute
()
{
$this
->
instance
->
setAttribute
(
"a"
,
1
);
$expect
=
[
"a"
=>
1
,
];
$this
->
assertEquals
(
$expect
,
$this
->
instance
->
getAttributes
(),
'attribute "a" set'
);
$this
->
instance
->
setAttribute
(
"b"
,
2
);
$expect
=
[
"a"
=>
1
,
"b"
=>
2
,
];
$this
->
assertEquals
(
$expect
,
$this
->
instance
->
getAttributes
(),
'attribute "b" set'
);
$this
->
instance
->
setAttribute
(
"a"
,
3
);
$expect
=
[
"a"
=>
3
,
"b"
=>
2
,
];
$this
->
assertEquals
(
$expect
,
$this
->
instance
->
getAttributes
(),
'attribute "a" changed'
);
}
public
function
testGetAttribute
()
{
$attribs
=
[
"a"
=>
1
,
"b"
=>
2
,
"c"
=>
3
,
];
$this
->
instance
->
addAttributes
(
$attribs
);
$this
->
assertEquals
(
1
,
$this
->
instance
->
getAttribute
(
"a"
),
'get attribute "a" without default'
);
$this
->
assertEquals
(
1
,
$this
->
instance
->
getAttribute
(
"a"
,
"x"
),
'get attribute "a" with default'
);
$this
->
assertEquals
(
2
,
$this
->
instance
->
getAttribute
(
"b"
),
'get attribute "b" without default'
);
$this
->
assertEquals
(
3
,
$this
->
instance
->
getAttribute
(
"c"
),
'get attribute "c" without default'
);
$this
->
assertEquals
(
null
,
$this
->
instance
->
getAttribute
(
"d"
),
'get unknown attribute "d" without default'
);
$this
->
assertEquals
(
"x"
,
$this
->
instance
->
getAttribute
(
"d"
,
"x"
),
'get unknown attribute "d" with default'
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment