Skip to content

Commit 6642622

Browse files
committed
updated folder names
1 parent f617bd8 commit 6642622

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

src/Avatar/Loader.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/**
4+
* Class for getting random avatar image
5+
*
6+
* @author Daniel Sitek <dan.sitek@gmail.com>
7+
*/
8+
9+
namespace App\Avatar;
10+
11+
class Loader {
12+
13+
/** @var string $images_path Set base path to image files */
14+
private $images_path = '/images';
15+
16+
/** @var array $avatars Array with available images */
17+
private $avatars = array(
18+
'man' => array(
19+
'/man/001.png'
20+
),
21+
'woman' => array(
22+
'/woman/001.png'
23+
)
24+
);
25+
26+
/** @var string|null $gender Gender of avatar image */
27+
private $gender = null;
28+
29+
/** @var string|null $image_file_path Path to image file */
30+
private $image_file_path = null;
31+
32+
/** @var string|null $loaded_image Image loaded to memory */
33+
// private $loaded_image = null;
34+
35+
36+
/**
37+
* Set root path to image files
38+
*
39+
* @param string $path Root path to image files
40+
*/
41+
public function set_images_root_path( $path )
42+
{
43+
$this->images_path = $path;
44+
45+
return $this;
46+
}
47+
48+
49+
/**
50+
* Set array with images for loading
51+
*
52+
* @param array $images Array with image file names
53+
*/
54+
public function set_images_array( $images = array() )
55+
{
56+
$this->avatars = $images;
57+
58+
return $this;
59+
}
60+
61+
62+
/**
63+
* Set gender for avatar image
64+
*
65+
* @param string|null $gender Set avatar gender
66+
*/
67+
public function set_gender( $gender = null )
68+
{
69+
if ( ! isset( $gender ) ) {
70+
$this->gender = array_rand( $this->avatars );
71+
return $this;
72+
}
73+
74+
$this->gender = $gender;
75+
return $this;
76+
}
77+
78+
79+
/**
80+
* Load image from filesystem and return array with path and headers
81+
*/
82+
public function load_image()
83+
{
84+
$this->image_file_path = $this->get_image_file_path();
85+
86+
return array(
87+
'file' => $this->image_file_path,
88+
'headers' => array(
89+
'Accept-Ranges' => 'bytes',
90+
'Content-Type' => 'image/png',
91+
'Content-Length' => strlen( file_get_contents( $this->image_file_path ) ),
92+
'X-Avatar-Gender' => $this->gender,
93+
'Expires' => 0,
94+
'Cache-Control' => 'no-cache, no-store, must-revalidate',
95+
'Pragma' => 'no-cache'
96+
)
97+
);
98+
}
99+
100+
101+
/**
102+
* Get path to image file
103+
*/
104+
private function get_image_file_path()
105+
{
106+
return $this->images_path . $this->avatars[ $this->gender ][ array_rand( $this->avatars[ $this->gender ] ) ];
107+
}
108+
}

tests/Avatar/LoaderTests.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class LoaderTests extends TestCase {
6+
7+
private $root = __DIR__ . '/../images';
8+
9+
private $images = array(
10+
'man' => array(
11+
'/man/001.png'
12+
),
13+
'woman' => array(
14+
'/woman/001.png'
15+
)
16+
);
17+
18+
public function testLoadRandomAvatarImage()
19+
{
20+
$loader = new App\Avatar\Loader();
21+
$loader->set_images_root_path($this->root);
22+
$loader->set_images_array($this->images);
23+
$loader->set_gender();
24+
25+
$image = $loader->load_image();
26+
27+
$this->assertArrayHasKey('file', $image);
28+
$this->assertArrayHasKey('headers', $image);
29+
$this->assertArrayHasKey('X-Avatar-Gender', $image['headers']);
30+
$this->assertArrayHasKey('Content-Type', $image['headers']);
31+
$this->assertContains('images/', $image['file']);
32+
}
33+
34+
public function testLoadManAvatarImage()
35+
{
36+
$loader = new App\Avatar\Loader();
37+
$loader->set_images_root_path($this->root);
38+
$loader->set_images_array($this->images);
39+
$loader->set_gender('man');
40+
41+
$image = $loader->load_image();
42+
43+
$this->assertArrayHasKey('file', $image);
44+
$this->assertArrayHasKey('headers', $image);
45+
$this->assertArrayHasKey('X-Avatar-Gender', $image['headers']);
46+
$this->assertArrayHasKey('Content-Type', $image['headers']);
47+
$this->assertTrue($image['headers']['Content-Type'] === 'image/png');
48+
$this->assertTrue($image['headers']['X-Avatar-Gender'] === 'man');
49+
$this->assertContains('images/man/001.png', $image['file']);
50+
}
51+
52+
public function testLoadWomanAvatarImage()
53+
{
54+
$loader = new App\Avatar\Loader();
55+
$loader->set_images_root_path($this->root);
56+
$loader->set_images_array($this->images);
57+
$loader->set_gender('woman');
58+
59+
$image = $loader->load_image();
60+
61+
$this->assertArrayHasKey('file', $image);
62+
$this->assertArrayHasKey('headers', $image);
63+
$this->assertArrayHasKey('X-Avatar-Gender', $image['headers']);
64+
$this->assertArrayHasKey('Content-Type', $image['headers']);
65+
$this->assertTrue($image['headers']['Content-Type'] === 'image/png');
66+
$this->assertTrue($image['headers']['X-Avatar-Gender'] === 'woman');
67+
$this->assertContains('images/woman/001.png', $image['file']);
68+
}
69+
}

0 commit comments

Comments
 (0)