Skip to content

Commit c2a004e

Browse files
committed
Add initial version
1 parent 2e643f1 commit c2a004e

File tree

8 files changed

+220
-0
lines changed

8 files changed

+220
-0
lines changed

.gitattributes

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://php.watch/articles/composer-gitattributes
2+
3+
# Exclude build/test files from archive
4+
/.github export-ignore
5+
/build export-ignore
6+
/tests export-ignore
7+
/.gitattributes export-ignore
8+
/.gitignore export-ignore
9+
/phpunit.xml.dist export-ignore
10+
11+
# Configure diff output for .php and .phar files.
12+
*.php diff=php

.github/workflows/ci.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
on:
2+
- pull_request
3+
- push
4+
5+
name: CI
6+
7+
jobs:
8+
tests:
9+
name: Tests
10+
runs-on: ${{ matrix.os }}
11+
12+
strategy:
13+
matrix:
14+
os:
15+
- ubuntu-latest
16+
- windows-latest
17+
php:
18+
- "7.3"
19+
- "7.4"
20+
- "8.0"
21+
- "8.1"
22+
- "8.2"
23+
- "8.3"
24+
- "8.4"
25+
26+
steps:
27+
- name: Checkout
28+
uses: actions/checkout@v4
29+
30+
- name: Install PHP with extensions
31+
uses: shivammathur/setup-php@v2
32+
with:
33+
php-version: ${{ matrix.php }}
34+
coverage: pcov
35+
ini-values: assert.exception=1, zend.assertions=1
36+
37+
- name: Install composer dependencies
38+
run: composer update --no-ansi --no-interaction --no-progress
39+
40+
- name: Run tests with phpunit
41+
run: vendor/bin/simple-phpunit

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/build/
3+
composer.lock

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# `array_first` and `array_last` polyfills
2+
3+
[![Latest Stable Version](https://poser.pugx.org/polyfills/array-first-array-last/v)](https://packagist.org/packages/polyfills/array-first-array-last) [![License](https://poser.pugx.org/polyfills/array-first-array-last/license)](https://packagist.org/packages/polyfills/array-first-array-last) [![PHP Version Require](https://poser.pugx.org/polyfills/array-first-array-last/require/php)](https://packagist.org/packages/polyfills/array-first-array-last)
4+
5+
Provides user-land PHP polyfills for the `array_first` and `array_last` functions added in PHP 8.5.
6+
7+
Requires PHP 7.3 or later. Not supported on PHP 8.5 because these functions are natively implemented in PHP 8.5.
8+
9+
## Installation
10+
11+
```bash
12+
composer require polyfills/array-first-array-last
13+
```
14+
15+
## Usage
16+
17+
Simply use the `array_first` and `array_last` functions as if they were declared already.
18+
19+
## Contributions
20+
21+
Contributions are welcome either as a GitHub issue or a PR to this repo.
22+

composer.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "polyfills/array-first-array-last",
3+
"description": "PHP: Provides a user-land polyfill for `array_first` and `array_last` functions added in PHP 8.5.",
4+
"type": "library",
5+
"homepage": "https://php.watch/versions/8.5/array_first-array_last",
6+
"keywords": ["php85", "phpwatch", "polyfills", "polyfill", "compatibility", "compat", "shim"],
7+
"require": {
8+
"php": "^7.3 || ^8.0"
9+
},
10+
"require-dev": {
11+
"symfony/phpunit-bridge": "^5.3|^6.0"
12+
},
13+
"conflict": {
14+
"php": "^8.5"
15+
},
16+
"license": "MIT",
17+
"autoload": {
18+
"files": [
19+
"src/array_first_last.php"
20+
]
21+
},
22+
"authors": [
23+
{
24+
"name": "Ayesh Karunaratne",
25+
"email": "[email protected]"
26+
}
27+
]
28+
}

phpunit.xml.dist

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
executionOrder="depends,defects"
6+
beStrictAboutOutputDuringTests="true"
7+
colors="true"
8+
failOnWarning="true"
9+
failOnRisky="true"
10+
failOnIncomplete="true"
11+
failOnEmptyTestSuite="true"
12+
beStrictAboutChangesToGlobalState="true"
13+
testdox="true">
14+
<testsuites>
15+
<testsuite name="default">
16+
<directory>tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
<coverage>
20+
<report>
21+
<html outputDirectory="build/coverage-html"/>
22+
<text outputFile="php://stdout"/>
23+
</report>
24+
</coverage>
25+
</phpunit>

src/array_first_last.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
4+
if (\PHP_VERSION_ID >= 80000) {
5+
/**
6+
* Returns the first value of a given array.
7+
*
8+
* @param array $array The array to get the first value of.
9+
* @return mixed First value of the array, or null if the array is
10+
* empty. Note that null itself can also be a valid array value.
11+
*/
12+
function array_first(array $array): mixed {
13+
return empty($array) ? null : $array[array_key_first($array)];
14+
}
15+
16+
/**
17+
* Returns the first value of a given array.
18+
*
19+
* @param array $array The array to get the first value of.
20+
* @return mixed First value of the array, or null if the array is
21+
* empty. Note that null itself can also be a valid array value.
22+
*/
23+
function array_last(array $array): mixed {
24+
return empty($array) ? null : $array[array_key_last($array)];
25+
}
26+
}
27+
else {
28+
/**
29+
* Returns the first value of a given array.
30+
*
31+
* @param array $array The array to get the first value of.
32+
* @return mixed First value of the array, or null if the array is
33+
* empty. Note that null itself can also be a valid array value.
34+
*/
35+
function array_first(array $array) {
36+
return empty($array) ? null : $array[array_key_first($array)];
37+
}
38+
39+
/**
40+
* Returns the first value of a given array.
41+
*
42+
* @param array $array The array to get the first value of.
43+
* @return mixed First value of the array, or null if the array is
44+
* empty. Note that null itself can also be a valid array value.
45+
*/
46+
function array_last(array $array) {
47+
return empty($array) ? null : $array[array_key_last($array)];
48+
}
49+
}

tests/ArrayFirstLastTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Polyfills\MbTrimPolyfill\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class ArrayFirstLastTest extends TestCase {
8+
9+
public function testArrayFirst() {
10+
$obj = new \stdClass();
11+
self::assertSame(1, array_first([1, 2, 3]));
12+
self::assertSame(2, array_first([2, 3]));
13+
self::assertNull(array_first([null, 2, 3]));
14+
self::assertSame("null", array_first(["null", 2, 3]));
15+
self::assertSame($obj, array_first([$obj, 2, 3]));
16+
self::assertSame(1, array_first([1]));
17+
self::assertTrue(array_first([true]));
18+
self::assertFalse(array_first([false, true]));
19+
self::assertSame([], array_first([[], true]));
20+
self::assertSame([1, 2], array_first([[1, 2], true]));
21+
self::assertNull(array_first([null]));
22+
self::assertNull(array_first([]));
23+
}
24+
25+
public function testArrayLast() {
26+
$obj = new \stdClass();
27+
self::assertSame(3, array_last([1, 2, 3]));
28+
self::assertSame(3, array_last([2, 3]));
29+
self::assertNull(array_last([2, 3, null]));
30+
self::assertSame("null", array_last([2, 3, "null"]));
31+
self::assertSame($obj, array_last([2, 3, $obj]));
32+
self::assertSame(1, array_last([1]));
33+
self::assertTrue(array_last([true]));
34+
self::assertFalse(array_last([true, false]));
35+
self::assertSame([], array_last([true, []]));
36+
self::assertSame([1, 2], array_last([true, [1, 2]]));
37+
self::assertNull(array_last([null]));
38+
self::assertNull(array_last([]));
39+
}
40+
}

0 commit comments

Comments
 (0)