Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improve implementation #2

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.neon]
indent_size = 2

[*.xml.dist]
indent_size = 2
Comment on lines +1 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you base this off of what the current file was or did you just make it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This is based on how to easily autoformat the package files on IDE. It is used as a baseline so that PHPStorm or Vscode or any IDE can easily format the file on saving or on editing. Most PHP libraries comes with those to have a unify coding practice/style.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what .editorconfig is, I was just asking how you came up with the values for the configuration.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value are the on I use ... the default is set for PHP according to PHP standard coding style from PSR-1

The other also stem form standard practices in PHP world

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense.

11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto

/demo export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.github export-ignore
/phpstan.neon export-ignore
/phpunit.xml.dist export-ignore
/README.md export-ignore
/**/*Test.php export-ignore
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
name: build
on:
push: ~
pull_request: ~

jobs:
phpunit:
name: PHPUnit tests on ${{ matrix.php }} ${{ matrix.composer-flags }}
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.2', '7.3', '7.4', '8.0' ]
composer-flags: [ '' ]
phpunit-flags: [ '--coverage-text' ]
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
tools: composer:v2
- run: composer update --no-progress ${{ matrix.composer-flags }}
- run: composer phpstan
if: ${{ matrix.php == '7.4' }}
- run: vendor/bin/phpunit ${{ matrix.phpunit-flags }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
vendor
.idea
.phpunit.result.cache
composer.lock
655 changes: 0 additions & 655 deletions Multiavatar.php

This file was deleted.

101 changes: 86 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ Initially coded in JavaScript, this version of Multiavatar is re-created in PHP.

For more details about the Multiavatar Generator, please refer to the readme available in the JS [repository](https://github.com/multiavatar/Multiavatar).


### Installation ###

If you don't use composer, just include `Multiavatar.php` in your application.
If you don't use composer, just include the autoloader in the root of the directory in your application.

```
require_once('Multiavatar.php');
require_once '/path/to/directory/Multiavatar/autoload.php';
```

Via Composer:
Expand All @@ -25,27 +24,100 @@ Via Composer:
composer require multiavatar/multiavatar-php
```


### Usage ###

```
```php
<?php

use Multiavatar\Multiavatar;

$multiavatar = new Multiavatar();
$avatarId = "Binx Bond";
$multiavatar = new Multiavatar($avatarId, null, null);
echo($multiavatar->svgCode);
echo $multiavatar($avatarId);
```

For advanced usage, pass boolean `true` as the second parameter if you wish to generate an avatar without the environment part.
For advanced usage, you can pass an array as a second and last parameter as shown below:

```php
<?php

Pass an associative array as the third parameter to generate a specific avatar version.
use Multiavatar\Multiavatar;

$multiavatar = new Multiavatar();
$avatarId = "ANY_STRING_OR_INT";
$options = [
'ver' => [
'part' => '11',
'theme' => 'C',
],
'sansEnv' => true,
];

echo $multiavatar($avatarId, $options);
```
$avatarId = "ANY_STRING";
$multiavatar = new Multiavatar($avatarId, true, array("part" => 11, "theme" => "C"));
echo($multiavatar->svgCode);
The option array contains the following indexes:

- `sansEnv`: Tells whether the environment should be display or not. By default, the environment is display.
To remove the environment you should set the value to `true.

- `version`: Indicate which theme and shape should be used to generate the avatar.
There is 3 themes that you specify with the `theme` index named from `A` to `C`
and 16 shapes specified with the `part` index whose named ranged from `00` to `15`.

Each of those values can be set independently of each other.

```php
<?php

use Multiavatar\Multiavatar;

$multiavatar = new Multiavatar();

// Default usage
// The shape will be selected from the $avatarId
// The theme will be selected from the avatarId
// The environment is shown
$avatarId = "Starcrasher";
echo $multiavatar($avatarId);

// The shape will be selected from the $avatarId
// The theme will be selected from the avatarId
// Without the environment part
$avatarId = "Pandalion";
echo $multiavatar($avatarId, ['sansEnv' => true]);

// Generate a specific version
// version shape can be express in integer or in string containing only numbers.
$avatarId = "Pandalion";
echo $multiavatar($avatarId, ['sansEnv' => false, 'ver' => ['part' => 11, 'theme' => 'C']]);

// The avatarId can be a string or an integer
// version theme are case insensitive.
// The shape will be selected from the $avatarId
$avatarId = 123456789;
echo $multiavatar($avatarId, ['sansEnv' => false, 'ver' => ['theme' => 'b']]);

// Test with a specific shape
// The theme will be selected from the avatarId
$avatarId = "a86f755add37fe0b649c";
echo $multiavatar($avatarId, ['ver' => ['part' => '08']]);

$avatarId = "f7542474d54d2d2d97e4";
echo $multiavatar($avatarId);
```

See `index.php` for examples.
### Testing ###

The library has a :

- a [PHPUnit](https://phpunit.de) test suite
- a code analysis compliance test suite using [PHPStan](https://phpstan.org).

To run the tests, run the following command from the project folder.

``` bash
$ composer test
```

### API ###

Expand All @@ -65,7 +137,6 @@ fetch('https://api.multiavatar.com/v1/'
.then(svg => console.log(svg))
```


### License ###

You can use Multiavatar for free, as long as the conditions described in the [LICENSE](https://multiavatar.com/license) are followed.
Expand All @@ -88,4 +159,4 @@ For additional information and extended functionality, visit the [multiavatar.co

The app is based on static html for the home page, and on Laravel 8 + Vue.js for extended functionality, including the web store.

The product mockup generator for the [Merch Maker](https://multiavatar.com/merch-maker) is based on the ImageMagick PHP library.
The product mockup generator for the [Merch Maker](https://multiavatar.com/merch-maker) is based on the ImageMagick PHP library.
16 changes: 16 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

spl_autoload_register(function ($class) {

$prefix = 'Multiavatar\\';
if (0 !== strpos($class, $prefix)) {
return;
}

$file = __DIR__.'/src/'.str_replace('\\', '/', substr($class, strlen($prefix))).'.php';
if (!is_readable($file)) {
return;
}

require $file;
});
23 changes: 22 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@
}
],
"autoload": {
"classmap": ["Multiavatar.php"]
"psr-4": {
"Multiavatar\\": "src/"
}
},
"require-dev": {
"php": ">=7.1",
"phpunit/phpunit": "^8.0 || ^9.5",
"phpstan/phpstan": "^0.12.63",
"phpstan/phpstan-strict-rules": "^0.12.7",
"phpstan/phpstan-phpunit": "^0.12.17"
},
"scripts": {
"phpstan": "phpstan analyse src -c phpstan.neon --ansi",
"phpunit": "phpunit --coverage-text",
"test": [
"@phpunit",
"@phpstan"
]
},
"scripts-descriptions": {
"phpstan": "Static analysis of PHP scripts",
"phpunit": "Runs unit and functional testing"
}
}
61 changes: 61 additions & 0 deletions demo/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

use Multiavatar\Multiavatar;

require dirname(__DIR__).'/autoload.php';

$multiavatar = new Multiavatar();
$avatarList = [];
foreach (range(0, 15) as $part) {
foreach (['a', 'b', 'c'] as $theme) {
$avatarList[$part.'-'.$theme] = $multiavatar('a', ['ver'=> ['part' => $part, 'theme' => $theme]]);
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Multiavatar - All 48 Initial Avatar Designs</title>
<style>
body, html {
width:100%;
height:100%;
}

body {
background-color: #fff;
overflow-x: hidden;
padding:0px;
margin:0px;
}

* {
box-sizing: border-box;
}

.container {
width: 100%;
height: 100%;
padding: 20px;
}

.avatar {
width: 110px;
height:110px;
float:left;
margin: 10px;
}
</style>
</head>
<body>
<div id="container" class="container">
<?php foreach ($avatarList as $id => $svg): ?>
<div id="<?=$id?>" class="avatar"><?=$svg?></div>
<?php endforeach; ?>
</div>
<div style="height:40px;clear:both;"></div>
</body>
</html>
36 changes: 0 additions & 36 deletions index.php

This file was deleted.

8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
parameters:
level: max
checkMissingIterableValueType: false
ignoreErrors:
reportUnmatchedIgnoredErrors: true
36 changes: 36 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix="Test.php">src</directory>
</exclude>
<report>
<clover outputFile="build/clover.xml"/>
<html outputDirectory="build/coverage"/>
<xml outputDirectory="build/coverage-xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="Multiavatar Test Suite">
<directory suffix="Test.php">src</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/junit.xml"/>
</logging>
</phpunit>
Loading