Skip to content

Commit 1481e51

Browse files
authored
Initial version (#1)
1 parent 89998c5 commit 1481e51

File tree

7 files changed

+477
-1
lines changed

7 files changed

+477
-1
lines changed

.github/workflows/phpcs.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: "CI"
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "**.php"
7+
- "phpcs.xml"
8+
- ".github/workflows/phpcs.yml"
9+
push:
10+
paths:
11+
- "**.php"
12+
- "phpcs.xml"
13+
- ".github/workflows/phpcs.yml"
14+
15+
jobs:
16+
phpcs:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v2
20+
with:
21+
fetch-depth: 0 # important!
22+
23+
- name: Install PHP_CodeSniffer
24+
run: |
25+
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
26+
php phpcs.phar --version
27+
28+
- uses: thenabeel/action-phpcs@v8
29+
with:
30+
files: "**.php" # you may customize glob as needed
31+
phpcs_path: php phpcs.phar
32+
standard: phpcs.xml

.github/workflows/test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Tests
2+
on: [push]
3+
jobs:
4+
related-users:
5+
runs-on: ubuntu-latest
6+
strategy:
7+
matrix:
8+
operating-system: ['ubuntu-latest']
9+
php-versions: ['7.4']
10+
redis-version: [ 4, 5, 6 ]
11+
phpunit-versions: ['latest']
12+
include:
13+
- operating-system: 'ubuntu-latest'
14+
php-versions: '7.4'
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: nanasess/setup-php@master
18+
with:
19+
php-version: '7.4'
20+
- name: Update Composer
21+
run: sudo composer self-update 1.10.15 --no-interaction
22+
- name: Run Composer Install
23+
run: composer install --no-interaction
24+
- name: run tests
25+
run: vendor/bin/phpunit

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{
12
"name": "abrouter/related-users",
23
"type": "library",
34
"description": "ABRouter internal tool for handling related user's ids",
@@ -17,7 +18,7 @@
1718
"php": "^7.4 || ^8.0"
1819
},
1920
"require-dev": {
20-
"phpunit/phpunit": "^9",
21+
"phpunit/phpunit": "^9"
2122
},
2223
"autoload-dev": {
2324
"psr-4": {

phpcs.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
3+
<ruleset name="ABRouter">
4+
<description>ABRouter Coding Standards</description>
5+
6+
<!-- Use PSR-2 as a base -->
7+
<rule ref="PSR12"/>
8+
<rule ref="Generic.Formatting.NoSpaceAfterCast"/>
9+
10+
<!-- We can extend it for everything we need -->
11+
<file>./src</file>
12+
<file>./tests</file>
13+
<exclude-pattern>./vendor/*</exclude-pattern>
14+
</ruleset>

phpunit.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" stopOnError="false" stopOnIncomplete="false" stopOnSkipped="false" bootstrap="vendor/autoload.php">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="all">
10+
<directory suffix="Test.php">tests/Unit/</directory>
11+
</testsuite>
12+
<testsuite name="unit">
13+
<directory suffix="Test.php">tests/Unit/</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Abrouter\RelatedUsers\Collections;
6+
7+
class RelatedUsersCollection
8+
{
9+
private array $relatedUsers;
10+
11+
public function __construct(array $initial = [])
12+
{
13+
$this->relatedUsers = $initial;
14+
}
15+
16+
public function append(string $userId, string $relatedUserId): self
17+
{
18+
$this->appendTo($userId, $relatedUserId);
19+
$this->handleChilds($userId, $relatedUserId);
20+
21+
//updating child's
22+
foreach ($this->relatedUsers as $key => $childs) {
23+
foreach ($childs as $child) {
24+
$this->handleChilds($key, $child);
25+
}
26+
}
27+
28+
return $this;
29+
}
30+
31+
private function handleChilds(string $userId, string $relatedUserId): void
32+
{
33+
foreach ($this->relatedUsers[$userId] as $childId) {
34+
$this->appendTo($childId, $userId);
35+
$this->appendTo($childId, $relatedUserId);
36+
}
37+
}
38+
39+
private function appendTo(string $userId, $relatedUserId): void
40+
{
41+
if ($userId === $relatedUserId) {
42+
return ;
43+
}
44+
45+
if (!isset($this->relatedUsers[$relatedUserId])) {
46+
$this->relatedUsers[$relatedUserId] = [$userId];
47+
} else {
48+
$this->relatedUsers[$relatedUserId][] = $userId;
49+
$this->relatedUsers[$relatedUserId] = array_unique($this->relatedUsers[$relatedUserId]);
50+
}
51+
52+
if (!isset($this->relatedUsers[$userId])) {
53+
$this->relatedUsers[$userId] = [$relatedUserId];
54+
} else {
55+
$this->relatedUsers[$userId][] = $relatedUserId;
56+
$this->relatedUsers[$userId] = array_unique($this->relatedUsers[$userId]);
57+
}
58+
}
59+
60+
public function getByUserId(string $userId): ?array
61+
{
62+
return $this->relatedUsers[$userId] ?? null;
63+
}
64+
65+
public function getAll(): array
66+
{
67+
return $this->relatedUsers;
68+
}
69+
}

0 commit comments

Comments
 (0)