Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleMinotto committed Jan 30, 2015
0 parents commit 715ad1e
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/composer.lock
/phpunit.xml
/vendor
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- composer install --dev

notifications:
email: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Emanuele Minotto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions PlaceholdItProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace EmanueleMinotto\Faker;

use Faker\Provider\Base as Base_Provider;

/**
* placehold.it provider for Faker.
*
* @author Emanuele Minotto <[email protected]>
*
* @link http://placehold.it/
*/
class PlaceholdItProvider extends Base_Provider
{
/**
* placehold.it image URL.
*
* @param integer|string|array $size Height is optional, if no height is specified the image will be a square.
* @param string $format Adding an image file extension will render the image in the proper format.
* @param array $colors An array containing background color and foreground color.
* @param string $text Custom text can be entered using a query string at the very end of the url.
*
* @return string
*/
public static function imageUrl($size, $format = 'gif', array $colors = array(), $text = null)
{
// $colors should be 100 or 100x100
// but can be ['height' => 100, 'width' => 100]
// or ['w' => 100, 'h' => 100]
if (is_array($size)) {
ksort($size);
$size = implode('x', $size);
}

$base = 'http://placehold.it/'.$size.'.'.trim($format, '.');

// $colors should be ['background', 'text']
// but can be ['text' => '000', 'background' => 'fff']
// or ['txt' => '000', 'bg' => 'fff']
// or ['foreground' => '000', 'background' => 'fff']
// or ['fg' => '000', 'bg' => 'fff']
ksort($colors);

if (2 === count($colors)) {
$base .= '/'.implode('/', $colors);
}

if ($text) {
$base .= '?'.http_build_query(array(
'text' => $text,
));
}

return $base;
}
}
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
PlaceholdIt Provider [![Build Status](https://travis-ci.org/EmanueleMinotto/PlaceholdItProvider.svg)](https://travis-ci.org/EmanueleMinotto/PlaceholdItProvider)
====================

[placehold.it](http://placehold.it/) provider for [Faker](https://github.com/fzaninotto/Faker).

## Install
Install Silex using [Composer](http://getcomposer.org/).

Install the PlaceholdItProvider adding `emanueleminotto/faker-placehold-it-provider` to your composer.json or from CLI:

```
$ composer require emanueleminotto/faker-placehold-it-provider
```

## Usage

```php
$faker = Faker\Factory::create();
$faker->addProvider(new EmanueleMinotto\Faker\PlaceholdItProvider($faker));

// size
$url = $faker->imageUrl(50); // http://placehold.it/50.gif
$url = $faker->imageUrl('50x100'); // http://placehold.it/50x100.gif
$url = $faker->imageUrl(array(50, 100)); // http://placehold.it/50x100.gif
$url = $faker->imageUrl(array('w' => 100, 'h' => 50)); // http://placehold.it/50x100.gif

// format
// can be gif, jpeg, jpg or png
$url = $faker->imageUrl(50); // http://placehold.it/50.gif
$url = $faker->imageUrl(50, 'jpeg'); // http://placehold.it/50.jpeg
$url = $faker->imageUrl(50, 'jpg'); // http://placehold.it/50.jpg
$url = $faker->imageUrl(50, 'png'); // http://placehold.it/50.png

// colors
$url = $faker->imageUrl(50, null, array('000', 'fff')); // http://placehold.it/50.gif/000/fff
$url = $faker->imageUrl(50, null, array('w' => 100, 'h' => 100)); // http://placehold.it/50.gif/000/fff

// text
$url = $faker->imageUrl(50, null, array(), 'lorem ipsum'); // http://placehold.it/50.gif?text=lorem+ipsum
```
78 changes: 78 additions & 0 deletions Tests/PlaceholdItProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace EmanueleMinotto\Faker\Tests;

use EmanueleMinotto\Faker\PlaceholdItProvider;
use PHPUnit_Framework_TestCase;

/**
* @coversDefaultClass \EmanueleMinotto\Faker\PlaceholdItProvider
*/
class PlaceholdItProviderTest extends PHPUnit_Framework_TestCase
{
/**
* @covers ::imageUrl
* @dataProvider imageUrlDataProvider
*/
public function testImageUrl($size, $format = 'gif', array $colors = array(), $text = null, $expected)
{
$url = PlaceholdItProvider::imageUrl($size, $format, $colors, $text);

$this->assertSame('http://placehold.it/'.$expected, $url);
}

/**
* Data provider containing different combinations.
*
* @return array
*/
public function imageUrlDataProvider()
{
return array(
array('50x50', 'gif', array('000', 'fff'), 'lorem ipsum', '50x50.gif/000/fff?text=lorem+ipsum'),
array('50x50', 'gif', array('000', 'fff'), null, '50x50.gif/000/fff'),
array('50x50', 'gif', array('bg' => '000', 'txt' => 'fff'), 'lorem ipsum', '50x50.gif/000/fff?text=lorem+ipsum'),
array('50x50', 'gif', array('bg' => '000', 'txt' => 'fff'), null, '50x50.gif/000/fff'),
array('50x50', 'gif', array('fg' => 'fff', 'bg' => '000'), 'lorem ipsum', '50x50.gif/000/fff?text=lorem+ipsum'),
array('50x50', 'gif', array('fg' => 'fff', 'bg' => '000'), null, '50x50.gif/000/fff'),
array('50x50', 'gif', array('foreground' => 'fff', 'background' => '000'), 'lorem ipsum', '50x50.gif/000/fff?text=lorem+ipsum'),
array('50x50', 'gif', array('foreground' => 'fff', 'background' => '000'), null, '50x50.gif/000/fff'),
array('50x50', 'gif', array(), 'lorem ipsum', '50x50.gif?text=lorem+ipsum'),
array('50x50', 'gif', array(), null, '50x50.gif'),
array(10, 'gif', array(), 'lorem ipsum', '10.gif?text=lorem+ipsum'),
array(10, 'gif', array(), null, '10.gif'),
array(100, 'gif', array('000', 'fff'), 'lorem ipsum', '100.gif/000/fff?text=lorem+ipsum'),
array(100, 'gif', array('000', 'fff'), null, '100.gif/000/fff'),
array(100, 'gif', array('bg' => '000', 'txt' => 'fff'), 'lorem ipsum', '100.gif/000/fff?text=lorem+ipsum'),
array(100, 'gif', array('bg' => '000', 'txt' => 'fff'), null, '100.gif/000/fff'),
array(100, 'gif', array('fg' => 'fff', 'bg' => '000'), 'lorem ipsum', '100.gif/000/fff?text=lorem+ipsum'),
array(100, 'gif', array('fg' => 'fff', 'bg' => '000'), null, '100.gif/000/fff'),
array(100, 'gif', array('foreground' => 'fff', 'background' => '000'), 'lorem ipsum', '100.gif/000/fff?text=lorem+ipsum'),
array(100, 'gif', array('foreground' => 'fff', 'background' => '000'), null, '100.gif/000/fff'),
array(100, 'gif', array(), 'lorem ipsum', '100.gif?text=lorem+ipsum'),
array(100, 'gif', array(), null, '100.gif'),
array(50, 'gif', array(), 'lorem ipsum', '50.gif?text=lorem+ipsum'),
array(50, 'gif', array(), null, '50.gif'),
array(50, 'jpeg', array(), 'lorem ipsum', '50.jpeg?text=lorem+ipsum'),
array(50, 'jpeg', array(), null, '50.jpeg'),
array(50, 'jpg', array(), 'lorem ipsum', '50.jpg?text=lorem+ipsum'),
array(50, 'jpg', array(), null, '50.jpg'),
array(50, 'png', array(), 'lorem ipsum', '50.png?text=lorem+ipsum'),
array(50, 'png', array(), null, '50.png'),
array(array('h' => 100, 'w' => 50), 'gif', array(), 'lorem ipsum', '100x50.gif?text=lorem+ipsum'),
array(array('h' => 100, 'w' => 50), 'gif', array(), null, '100x50.gif'),
array(array('height' => 100, 'width' => 50), 'gif', array(), 'lorem ipsum', '100x50.gif?text=lorem+ipsum'),
array(array('height' => 100, 'width' => 50), 'gif', array(), null, '100x50.gif'),
array(array(50, 100), 'gif', array('000', 'fff'), 'lorem ipsum', '50x100.gif/000/fff?text=lorem+ipsum'),
array(array(50, 100), 'gif', array('000', 'fff'), null, '50x100.gif/000/fff'),
array(array(50, 100), 'gif', array('bg' => '000', 'txt' => 'fff'), 'lorem ipsum', '50x100.gif/000/fff?text=lorem+ipsum'),
array(array(50, 100), 'gif', array('bg' => '000', 'txt' => 'fff'), null, '50x100.gif/000/fff'),
array(array(50, 100), 'gif', array('fg' => 'fff', 'bg' => '000'), 'lorem ipsum', '50x100.gif/000/fff?text=lorem+ipsum'),
array(array(50, 100), 'gif', array('fg' => 'fff', 'bg' => '000'), null, '50x100.gif/000/fff'),
array(array(50, 100), 'gif', array('foreground' => 'fff', 'background' => '000'), 'lorem ipsum', '50x100.gif/000/fff?text=lorem+ipsum'),
array(array(50, 100), 'gif', array('foreground' => 'fff', 'background' => '000'), null, '50x100.gif/000/fff'),
array(array(50, 100), 'gif', array(), 'lorem ipsum', '50x100.gif?text=lorem+ipsum'),
array(array(50, 100), 'gif', array(), null, '50x100.gif'),
);
}
}
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
{
"email": "[email protected]",
"name": "Emanuele Minotto"
}
],
"autoload": {
"psr-4": {
"EmanueleMinotto\\Faker\\": "."
}
},
"description": "placehold.it provider for Faker",
"license": "MIT",
"name": "emanueleminotto/faker-placehold-it-provider",
"require": {
"fzaninotto/faker": "~1"
},
"require-dev": {
"phpunit/phpunit": "~4.4"
}
}
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false">
<testsuites>
<testsuite name="PlaceholdItProvider Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit 715ad1e

Please sign in to comment.