Skip to content

Commit d30ef57

Browse files
authored
Merge pull request #96 from siamak2/2.x
[2.0.1] Improved getMetaArray method by removing linear search from getMetaArray
2 parents a8d831a + 2ee3f41 commit d30ef57

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release Notes
22

3+
## v2.0.1
4+
5+
### Changes
6+
7+
* Improved `getMetaArray` method by removing linear search from `getMetaArray`
8+
39
## v2.0.0
410

511
### Added

src/Kodeine/Metable/Metable.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public function hasMeta($key): bool {
9696
if ( is_string( $key ) && preg_match( '/[,|]/is', $key ) ) {
9797
$key = preg_split( '/ ?[,|] ?/', $key );
9898
}
99-
$setMeta = 'hasMeta' . ucfirst( gettype( $key ) );
99+
$hasMeta = 'hasMeta' . ucfirst( gettype( $key ) );
100100

101-
return $this->$setMeta( $key );
101+
return $this->$hasMeta( $key );
102102
}
103103

104104
protected function hasMetaString($key): bool {
@@ -183,6 +183,7 @@ public function getDefaultMetaValue($key) {
183183
}
184184

185185
protected function getMetaString($key, $default = null) {
186+
$key = strtolower( $key );
186187
$meta = $this->getMetaData()->get( $key );
187188

188189
if ( is_null( $meta ) || $meta->isMarkedForDeletion() ) {
@@ -195,16 +196,18 @@ protected function getMetaString($key, $default = null) {
195196

196197
protected function getMetaArray($keys, $default = null): BaseCollection {
197198
$collection = new BaseCollection();
198-
$flipped = array_flip( $keys );
199-
foreach ($this->getMetaData() as $meta) {
200-
if ( ! $meta->isMarkedForDeletion() && isset( $flipped[$meta->key] ) ) {
201-
unset( $flipped[$meta->key] );
202-
$collection->put( $meta->key, $meta->value );
199+
200+
foreach ($keys as $key) {
201+
$key = strtolower( $key );
202+
if ( $this->hasMeta( $key ) ) {
203+
$meta = $this->getMetaData()[$key];
204+
if ( ! $meta->isMarkedForDeletion() ) {
205+
$collection->put( $key, $meta->value );
206+
continue;
207+
}
203208
}
204-
}
205-
// If there are any keys left in $flipped, it means they are not set. so fill them with default values.
206-
// Default values set in defaultMetaValues property take precedence over default values passed to this method
207-
foreach ($flipped as $key => $value) {
209+
// Key does not exist, so it's value will be the default value
210+
// Default values set in defaultMetaValues property take precedence over default value passed to this method
208211
$defaultValue = $this->getDefaultMetaValue( $key );
209212
if ( is_null( $defaultValue ) ) {
210213
if ( is_array( $default ) ) {

0 commit comments

Comments
 (0)