Skip to content

Commit

Permalink
Merge pull request #96 from siamak2/2.x
Browse files Browse the repository at this point in the history
[2.0.1] Improved getMetaArray method by removing linear search from getMetaArray
  • Loading branch information
kodeine authored Jun 6, 2022
2 parents a8d831a + 2ee3f41 commit d30ef57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## v2.0.1

### Changes

* Improved `getMetaArray` method by removing linear search from `getMetaArray`

## v2.0.0

### Added
Expand Down
25 changes: 14 additions & 11 deletions src/Kodeine/Metable/Metable.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public function hasMeta($key): bool {
if ( is_string( $key ) && preg_match( '/[,|]/is', $key ) ) {
$key = preg_split( '/ ?[,|] ?/', $key );
}
$setMeta = 'hasMeta' . ucfirst( gettype( $key ) );
$hasMeta = 'hasMeta' . ucfirst( gettype( $key ) );

return $this->$setMeta( $key );
return $this->$hasMeta( $key );
}

protected function hasMetaString($key): bool {
Expand Down Expand Up @@ -183,6 +183,7 @@ public function getDefaultMetaValue($key) {
}

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

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

protected function getMetaArray($keys, $default = null): BaseCollection {
$collection = new BaseCollection();
$flipped = array_flip( $keys );
foreach ($this->getMetaData() as $meta) {
if ( ! $meta->isMarkedForDeletion() && isset( $flipped[$meta->key] ) ) {
unset( $flipped[$meta->key] );
$collection->put( $meta->key, $meta->value );

foreach ($keys as $key) {
$key = strtolower( $key );
if ( $this->hasMeta( $key ) ) {
$meta = $this->getMetaData()[$key];
if ( ! $meta->isMarkedForDeletion() ) {
$collection->put( $key, $meta->value );
continue;
}
}
}
// If there are any keys left in $flipped, it means they are not set. so fill them with default values.
// Default values set in defaultMetaValues property take precedence over default values passed to this method
foreach ($flipped as $key => $value) {
// Key does not exist, so it's value will be the default value
// Default values set in defaultMetaValues property take precedence over default value passed to this method
$defaultValue = $this->getDefaultMetaValue( $key );
if ( is_null( $defaultValue ) ) {
if ( is_array( $default ) ) {
Expand Down

0 comments on commit d30ef57

Please sign in to comment.