├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── index.php └── src └── MatrixRef └── Services ├── MatrixReferralSystem.php └── RenderMatrix.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/* 2 | composer.lock 3 | /vendor -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 GlobalX 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Matrix 2 | Base matrix function for using in referral system 3 | 4 | # EN 5 | Used: PHP 8.1 6 | 7 | You can use my implementation as a foundation. And then you can integrate it into the referral system, well, here it's already like a fantasy. 8 | 9 | # RU 10 | Использовался: PHP 8.1 11 | 12 | Вы можете использовать мою реализацию в качестве фундамента. А затем можете интегрировать в реферальную систему, ну тут уже как фантазия представит. 13 | 14 | # Copyright 15 | 16 | Copyright (C) GlobalOneX, Inc - All Rights Reserved 17 | 18 | All rights reserved. No part of this project may be reproduced, distributed or 19 | transmitted in any form or by any means, including photocopying, recording or other 20 | electronic or mechanical methods, without the prior written permission of the 21 | copyright owner. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "globalonex/matrix_ref", 3 | "description": "Matrix Referral System", 4 | "license": "MIT", 5 | "autoload": { 6 | "psr-4": { 7 | "MatrixRef\\": "src/MatrixRef/", 8 | "MatrixRef\\Services\\": "src/MatrixRef/Services/" 9 | } 10 | }, 11 | "authors": [ 12 | { 13 | "name": "globalonex" 14 | } 15 | ], 16 | "require": {} 17 | } 18 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 14 | * * @date 10.03.2023 17:25 15 | * 16 | */ 17 | 18 | namespace GlobalOneX\MatrixRef; 19 | 20 | require_once __DIR__ . '/vendor/autoload.php'; 21 | 22 | use MatrixRef\Services\MatrixReferralSystem; 23 | use MatrixRef\Services\RenderMatrix; 24 | 25 | $referralSystem = new MatrixReferralSystem(); 26 | $referralSystem->addMember(null, 'A'); // add first member 27 | $referralSystem->addMember('A', 'B'); 28 | $referralSystem->addMember('A', 'C'); 29 | $referralSystem->addMember('B', 'D'); 30 | $referralSystem->addMember('B', 'E'); 31 | $referralSystem->addMember('G', 'F'); 32 | $referralSystem->addMember('A', 'G'); 33 | $referralSystem->addMember('G', 'F'); 34 | $referralSystem->addMember('F', 'T'); 35 | $referralSystem->addMember('H', 'R'); 36 | 37 | $render = new RenderMatrix(); 38 | 39 | echo $render->getVector($referralSystem, count($referralSystem->getMatrix()), 7); // Special function which render vector matrix pyramid -------------------------------------------------------------------------------- /src/MatrixRef/Services/MatrixReferralSystem.php: -------------------------------------------------------------------------------- 1 | 14 | * * @date 10.03.2023 17:25 15 | * 16 | */ 17 | 18 | namespace MatrixRef\Services; 19 | 20 | class MatrixReferralSystem 21 | { 22 | private array $matrix = array(); 23 | 24 | /** 25 | * 26 | * This class represents the matrix structure of the referral system. 27 | * The addMember() method adds a new member to the system using the ID of the parent member. 28 | * If the parent participant is not found, the method returns false. 29 | * If the participant already exists in the system, the method also returns false. 30 | * If the participant is successfully added to the system, the method returns true. 31 | * 32 | * @param $parent_id 33 | * @param $member_id 34 | * @return array|bool 35 | */ 36 | public function addMember($parent_id, $member_id): array|bool 37 | { 38 | if (empty($this->matrix)) { 39 | $this->matrix[0][0] = $member_id; 40 | return true; 41 | } 42 | $member_index = $this->findIndex($member_id); 43 | if ($member_index) { 44 | return false; 45 | } 46 | $parent_index = $this->findIndex($parent_id); 47 | if (!$parent_index) { 48 | return false; 49 | } 50 | $row = $parent_index[0]; 51 | $col = $parent_index[1] + 1; 52 | if (empty($this->matrix[$row][$col])) { 53 | $this->matrix[$row][$col] = $member_id; 54 | return true; 55 | } 56 | // If the next position is already filled, try to find a lower position in the next row 57 | $next_row = $row + 1; 58 | 59 | for ($i = $col; $i < count(@(array)$this->matrix[$next_row]); $i++) { 60 | if (empty($this->matrix[$next_row][$i])) { 61 | $this->matrix[$next_row][$i] = $member_id; 62 | return true; 63 | } 64 | } 65 | 66 | 67 | // If there's no available position in the current and next row, add a new row 68 | $this->matrix[$next_row][0] = $member_id; 69 | return true; 70 | } 71 | 72 | /** 73 | * A simple method for finding an index in a matrix array 74 | * 75 | * @param $member_id 76 | * @return bool|array 77 | */ 78 | private function findIndex($member_id): bool|array 79 | { 80 | foreach ($this->matrix as $row_index => $row) { 81 | foreach ($row as $col_index => $col) { 82 | if ($col == $member_id) { 83 | return array($row_index, $col_index); 84 | } 85 | } 86 | } 87 | return false; 88 | } 89 | 90 | public function getMatrix() : array { 91 | return $this->matrix; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /src/MatrixRef/Services/RenderMatrix.php: -------------------------------------------------------------------------------- 1 | 14 | * * @date 10.03.2023 17:25 15 | * 16 | */ 17 | 18 | namespace MatrixRef\Services; 19 | 20 | class RenderMatrix 21 | { 22 | 23 | public function getVector(MatrixReferralSystem $referralSystem, int $rows = 3, int $cols = 6): string 24 | { 25 | 26 | $arf = json_encode((array)$referralSystem->getMatrix()); 27 | 28 | return ' 29 |

Global Production

30 | 31 | 32 | 33 | 91 | Project Version 0.1 92 | 93 |
94 | All rights reserved. No part of this project may be reproduced, distributed or 95 | transmitted in any form or by any means, including photocopying, recording or other 96 | electronic or mechanical methods, without the prior written permission of the 97 | copyright owner. 98 | '; 99 | } 100 | 101 | } --------------------------------------------------------------------------------