Skip to content

Commit a01d471

Browse files
committed
Port array_first implementation
Fixes #149
1 parent f41c927 commit a01d471

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ jobs:
2525
- stage: linter
2626
php: 7.2
2727
before_script: source ./scripts/dev-setup.sh
28-
script: phpcs {src/*,tests/*}
28+
script: phpcs -s {src/*,tests/*}
2929

src/ConditionAnalyzer/Analyzer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BaoPham\DynamoDb\ComparisonOperator;
66
use BaoPham\DynamoDb\DynamoDbModel;
7+
use BaoPham\DynamoDb\H;
78

89
/**
910
* Class ConditionAnalyzer
@@ -143,7 +144,7 @@ public function identifierConditionValues()
143144
*/
144145
private function getCondition($column)
145146
{
146-
return array_first($this->conditions, function ($condition) use ($column) {
147+
return H::array_first($this->conditions, function ($condition) use ($column) {
147148
return $condition['column'] === $column;
148149
});
149150
}

src/DynamoDbQueryBuilder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BaoPham\DynamoDb\Concerns\HasParsers;
66
use BaoPham\DynamoDb\ConditionAnalyzer\Analyzer;
77
use BaoPham\DynamoDb\Facades\DynamoDb;
8+
use BaoPham\DynamoDb\H;
89
use Closure;
910
use Illuminate\Contracts\Support\Arrayable;
1011
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -785,7 +786,7 @@ protected function isMultipleIds($id)
785786
}
786787

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

791792
/**

src/H.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BaoPham\DynamoDb;
4+
5+
/**
6+
* Class H
7+
*
8+
* Short for "Helper".
9+
* We often get breaking changes from Laravel Helpers, so to ensure this won't happen again, we port the helpers here.
10+
*
11+
* @package BaoPham\DynamoDb
12+
*/
13+
// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
14+
class H
15+
{
16+
public static function array_first($array, callable $callback = null, $default = null)
17+
{
18+
if (is_null($callback)) {
19+
if (empty($array)) {
20+
return static::value($default);
21+
}
22+
foreach ($array as $item) {
23+
return $item;
24+
}
25+
}
26+
foreach ($array as $key => $value) {
27+
if (call_user_func($callback, $value, $key)) {
28+
return $value;
29+
}
30+
}
31+
return static::value($default);
32+
}
33+
34+
public static function value($value)
35+
{
36+
return $value instanceof \Closure ? $value() : $value;
37+
}
38+
}
39+
// phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps

0 commit comments

Comments
 (0)