Skip to content

Commit

Permalink
Port array_first implementation
Browse files Browse the repository at this point in the history
Fixes #149
  • Loading branch information
baopham committed Sep 4, 2018
1 parent f41c927 commit a01d471
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ jobs:
- stage: linter
php: 7.2
before_script: source ./scripts/dev-setup.sh
script: phpcs {src/*,tests/*}
script: phpcs -s {src/*,tests/*}

3 changes: 2 additions & 1 deletion src/ConditionAnalyzer/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BaoPham\DynamoDb\ComparisonOperator;
use BaoPham\DynamoDb\DynamoDbModel;
use BaoPham\DynamoDb\H;

/**
* Class ConditionAnalyzer
Expand Down Expand Up @@ -143,7 +144,7 @@ public function identifierConditionValues()
*/
private function getCondition($column)
{
return array_first($this->conditions, function ($condition) use ($column) {
return H::array_first($this->conditions, function ($condition) use ($column) {
return $condition['column'] === $column;
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/DynamoDbQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BaoPham\DynamoDb\Concerns\HasParsers;
use BaoPham\DynamoDb\ConditionAnalyzer\Analyzer;
use BaoPham\DynamoDb\Facades\DynamoDb;
use BaoPham\DynamoDb\H;
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Expand Down Expand Up @@ -785,7 +786,7 @@ protected function isMultipleIds($id)
}

// could be ['foo', 'bar'], [['id1' => 'foo', 'id2' => 'bar'], ...]
return $this->model->hasCompositeKey() ? is_array(array_first($id)) : is_array($id);
return $this->model->hasCompositeKey() ? is_array(H::array_first($id)) : is_array($id);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/H.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace BaoPham\DynamoDb;

/**
* Class H
*
* Short for "Helper".
* We often get breaking changes from Laravel Helpers, so to ensure this won't happen again, we port the helpers here.
*
* @package BaoPham\DynamoDb
*/
// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
class H
{
public static function array_first($array, callable $callback = null, $default = null)
{
if (is_null($callback)) {
if (empty($array)) {
return static::value($default);
}
foreach ($array as $item) {
return $item;
}
}
foreach ($array as $key => $value) {
if (call_user_func($callback, $value, $key)) {
return $value;
}
}
return static::value($default);
}

public static function value($value)
{
return $value instanceof \Closure ? $value() : $value;
}
}
// phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps

0 comments on commit a01d471

Please sign in to comment.