Skip to content

Commit a68c072

Browse files
committed
Add source code
1 parent 07bd282 commit a68c072

File tree

9 files changed

+305
-0
lines changed

9 files changed

+305
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; top-most EditorConfig file
2+
root = true
3+
4+
; Unix-style newlines
5+
[*]
6+
end_of_line = LF
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 4

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
composer.lock
2+
.php_cs.cache
3+
.phpunit.result.cache
4+
vendor/

.php_cs.dist

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
$fileHeaderComment = <<<COMMENT
4+
This file is part of the scalar-values package.
5+
6+
(c) E-commit <contact@e-commit.fr>
7+
8+
For the full copyright and license information, please view the LICENSE
9+
file that was distributed with this source code.
10+
COMMENT;
11+
12+
$finder = PhpCsFixer\Finder::create()
13+
->in(__DIR__)
14+
;
15+
16+
return PhpCsFixer\Config::create()
17+
->setRiskyAllowed(true)
18+
->setRules([
19+
'@Symfony' => true,
20+
'@Symfony:risky' => true,
21+
'@DoctrineAnnotation' => true,
22+
'@PHP56Migration' => true,
23+
'@PHP56Migration:risky' => true,
24+
'@PHP71Migration' => true,
25+
'@PHP71Migration:risky' => true,
26+
'@PHP73Migration' => true,
27+
'array_syntax' => ['syntax' => 'short'],
28+
'fopen_flags' => true,
29+
'header_comment' => ['header' => $fileHeaderComment, 'separate' => 'both'],
30+
'linebreak_after_opening_tag' => true,
31+
'mb_str_functions' => true,
32+
'no_php4_constructor' => true,
33+
'no_unreachable_default_argument_value' => true,
34+
'no_useless_else' => true,
35+
'no_useless_return' => true,
36+
'ordered_imports' => true,
37+
'phpdoc_order' => true,
38+
'protected_to_private' => false,
39+
])
40+
->setFinder($finder)
41+
;

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
php:
3+
- 7.2
4+
- 7.3
5+
- 7.4
6+
7+
install:
8+
- composer install
9+
10+
script:
11+
- php vendor/phpunit/phpunit/phpunit
12+
- php vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --diff --dry-run -v

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Scalar Values Tools
2+
3+
## Installation ##
4+
5+
To install scalar-values with Composer just run :
6+
7+
```bash
8+
$ composer require ecommit/scalar-values
9+
```
10+
11+
## Usage ##
12+
13+
Test if an array contains only scalar values :
14+
15+
```php
16+
use Ecommit\ScalarValues\ScalarValues;
17+
18+
$array = ['str1', 2, 3];
19+
if (ScalarValues::containsOnlyScalarValues($array)) { //True
20+
//...
21+
} else {
22+
//...
23+
}
24+
25+
$array = ['str1', ['tab'], 3];
26+
if (ScalarValues::containsOnlyScalarValues($array)) { //False
27+
//...
28+
} else {
29+
//...
30+
}
31+
```
32+
33+
Returns the input array after deleting all non-scalar values (on root):
34+
35+
```php
36+
use Ecommit\ScalarValues\ScalarValues;
37+
38+
$array = ['str1', ['tab'], 3];
39+
$newArray = ScalarValues::filterScalarValues($array); //[0 => 'str1', 2 => 3]
40+
```
41+
42+
## License ##
43+
44+
This librairy is under the MIT license. See the complete license in *LICENSE* file.

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "ecommit/scalar-values",
3+
"description": "Test scalar values.",
4+
"keywords": ["scalar", "values"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Hubert Lecorche",
10+
"email": "contact@e-commit.fr"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": { "Ecommit\\ScalarValues\\": "src/" }
15+
},
16+
"autoload-dev": {
17+
"psr-4": { "Ecommit\\ScalarValues\\Tests\\": "tests/" }
18+
},
19+
"extra": {
20+
"branch-alias": {
21+
"dev-master": "1.0.x-dev"
22+
}
23+
},
24+
"require": {
25+
"php": "^7.2"
26+
},
27+
"require-dev": {
28+
"friendsofphp/php-cs-fixer": "^2.13",
29+
"phpunit/phpunit": "^8.0"
30+
},
31+
"config": {
32+
"sort-packages": true
33+
}
34+
}

phpunit.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.0/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
beStrictAboutTestsThatDoNotTestAnything="false"
9+
bootstrap="vendor/autoload.php"
10+
>
11+
12+
<testsuites>
13+
<testsuite name="Project Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>

src/ScalarValues.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the scalar-values package.
7+
*
8+
* (c) E-commit <contact@e-commit.fr>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ecommit\ScalarValues;
15+
16+
class ScalarValues
17+
{
18+
public static function containsOnlyScalarValues(array $array): bool
19+
{
20+
foreach ($array as $child) {
21+
if (!is_scalar($child)) {
22+
return false;
23+
}
24+
}
25+
26+
return true;
27+
}
28+
29+
public static function filterScalarValues(array $array): array
30+
{
31+
return array_filter($array, function ($child) {
32+
return is_scalar($child);
33+
});
34+
}
35+
}

tests/ScalarValuesTest.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the scalar-values package.
7+
*
8+
* (c) E-commit <contact@e-commit.fr>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ecommit\ScalarValues\Tests;
15+
16+
use Ecommit\ScalarValues\ScalarValues;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ScalarValuesTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider getTestContainsOnlyScalarValuesProdiver
23+
*/
24+
public function testContainsOnlyScalarValues($input, $expected): void
25+
{
26+
$this->assertEquals(
27+
$expected,
28+
ScalarValues::containsOnlyScalarValues($input)
29+
);
30+
}
31+
32+
public function getTestContainsOnlyScalarValuesProdiver(): array
33+
{
34+
//Colonne 1: Input
35+
//Colonne 2: Expected result
36+
37+
return [
38+
[[], true],
39+
[['value1', null, 'value3'], false],
40+
[['value1', '', 'value3'], true],
41+
[['value1', true, 'value3'], true],
42+
[['value1', new \stdClass(), 'value3'], false],
43+
[['value1', [], 'value3'], false],
44+
[['value1', ['value2'], 'value3'], false],
45+
[['value1', [1], 'value3'], false],
46+
[['value1', 1, 'value3'], true],
47+
[['value1', 'value2', 'value3'], true],
48+
];
49+
}
50+
51+
/**
52+
* @dataProvider getNotArrayInputProdiver
53+
*/
54+
public function testContainsOnlyScalarValuesInputError($input): void
55+
{
56+
$this->expectException(\TypeError::class);
57+
ScalarValues::containsOnlyScalarValues($input);
58+
}
59+
60+
public function getNotArrayInputProdiver(): array
61+
{
62+
return [
63+
[null],
64+
['value'],
65+
[1],
66+
[true],
67+
[new \stdClass()],
68+
];
69+
}
70+
71+
/**
72+
* @dataProvider getTestFilterScalarValuesProdiver
73+
*/
74+
public function testFilterScalarValues($input, $expected): void
75+
{
76+
$this->assertEquals(
77+
$expected,
78+
ScalarValues::filterScalarValues($input)
79+
);
80+
}
81+
82+
public function getTestFilterScalarValuesProdiver(): array
83+
{
84+
//Colonne 1: Input
85+
//Colonne 2: Expected result
86+
87+
return [
88+
[[], []],
89+
[['value1', null, 'value3'], [0 => 'value1', 2 => 'value3']],
90+
[['value1', '', 'value3'], ['value1', 1 => '', 'value3']],
91+
[['value1', true, 'value3'], [0 => 'value1', 1 => true, 2 => 'value3']],
92+
[['value1', new \stdClass(), 'value3'], [0 => 'value1', 2 => 'value3']],
93+
[['value1', [], 'value3'], [0 => 'value1', 2 => 'value3']],
94+
[['value1', ['value2'], 'value3'], [0 => 'value1', 2 => 'value3']],
95+
[['value1', [1], 'value3'], [0 => 'value1', 2 => 'value3']],
96+
[['value1', 1, 'value3'], [0 => 'value1', 1 => 1, 2 => 'value3']],
97+
[['value1', 'value2', 'value3'], [0 => 'value1', 1 => 'value2', 2 => 'value3']],
98+
];
99+
}
100+
101+
/**
102+
* @dataProvider getNotArrayInputProdiver
103+
*/
104+
public function testFilterScalarValuesInputError($input): void
105+
{
106+
$this->expectException(\TypeError::class);
107+
ScalarValues::filterScalarValues($input);
108+
}
109+
}

0 commit comments

Comments
 (0)