Skip to content

Commit

Permalink
fixed composer dep
Browse files Browse the repository at this point in the history
  • Loading branch information
liuggio committed Jun 30, 2014
1 parent 322dfbb commit cbca156
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 33 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Flush an image
echo $poser->generate('license', 'MIT', '428F7E', 'svg');
// or
echo $poser->generateFromURI('license-MIT-428F7E.svg');
// or
$image = $poser->generate('license', 'MIT', '428F7E', 'svg');

echo $image->getFormat();
```

## Encoding
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"psr-4": { "PUGX\\Poser\\": "src/" }
},
"require": {
"symfony/console": "dev-master"
"symfony/console": "~2"
},
"require-dev": {
"phpspec/phpspec": "dev-master",
Expand Down
12 changes: 3 additions & 9 deletions spec/PUGX/Poser/PoserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,14 @@ function it_should_be_able_to_generate_an_svg_image_from_URI()
public function getMatchers()
{
return array(
'beAValidSVGImage' => function($subject) {

$regex = '/^<svg.*width="((.|\n)*)<\/svg>$/';
$matches = array();

return preg_match($regex, $subject, $matches, PREG_OFFSET_CAPTURE, 0);
},
'beAValidSVGImageContaining' => function($object, $subject, $status) {
'beAValidSVGImageContaining' => function($object, $subject, $status)
{

$regex = '/^<svg.*width="((.|\n)*)'.$subject.'((.|\n)*)'.$status.'((.|\n)*)<\/svg>$/';
$matches = array();

return preg_match($regex, $object, $matches, PREG_OFFSET_CAPTURE, 0);
},
},
);
}
}
11 changes: 2 additions & 9 deletions spec/PUGX/Poser/Render/SvgRenderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@ public function getMatchers()
$regex = '/^<svg.*width="((.|\n)*)<\/svg>$/';
$matches = array();

return preg_match($regex, $subject, $matches, PREG_OFFSET_CAPTURE, 0);
},
'beAValidSVGImageContaining' => function($object, $subject, $status) {

$regex = '/^<svg.*width="((.|\n)*)'.$subject.'((.|\n)*)'.$status.'((.|\n)*)<\/svg>$/';
$matches = array();

return preg_match($regex, $object, $matches, PREG_OFFSET_CAPTURE, 0);
},
return preg_match($regex, (string) $subject, $matches, PREG_OFFSET_CAPTURE, 0);
}
);
}
}
69 changes: 69 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the badge-poser package.
*
* (c) PUGX <http://pugx.github.io/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\Poser;

/**
* Class Image, an Image value Object
*
* @author Claudio D'Alicandro <[email protected]>
* @author Giulio De Donato <[email protected]>
*/
class Image
{
/**
* @var string $content
*/
private $content;
/**
* @var string $format
*/
private $format;

/**
* @param string $content
* @param string $format
*/
private function __construct($content, $format)
{
$this->content = $content;
$this->format = $format;
}

/**
* Returns the image content as binary string
*/
public function __toString()
{
return $this->content;
}

/**
* Factory method
*
* @param string $content
* @param string $format
*
* @return Image
*/
public static function createFromString($content, $format)
{
return new self($content, $format);
}

/**
* @return string
*/
public function getFormat()
{
return $this->format;
}
}
4 changes: 2 additions & 2 deletions src/Poser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct($renders)
* @param $color
* @param $format
*
* @return string
* @return Image
*/
public function generate($subject, $status, $color, $format)
{
Expand All @@ -47,7 +47,7 @@ public function generate($subject, $status, $color, $format)
* eg license-MIT-blue.svg or I_m-liuggio-yellow.svg.
*
* @param $string
* @return mixed
* @return Image
*/
public function generateFromURI($string)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Render/RenderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface RenderInterface
*
* @param Badge $badge
*
* @return string
* @return \PUGX\Poser\Image
*/
public function render(Badge $badge);

Expand Down
21 changes: 10 additions & 11 deletions src/Render/SvgRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PUGX\Poser\Badge;
use PUGX\Poser\Calculator\TextSizeCalculatorInterface;
use PUGX\Poser\Calculator\GDTextSizeCalculator;

use PUGX\Poser\Image;
/**
* Class SvgGenerator
*
Expand All @@ -24,9 +24,9 @@
class SvgRender implements RenderInterface
{
const VENDOR_COLOR = '#555';

private $textSizeCalculator;
private static $template = '<svg xmlns="http://www.w3.org/2000/svg" width="{{ totalWidth }}" height="18">
private static $template = <<<EOF
<svg xmlns="http://www.w3.org/2000/svg" width="{{ totalWidth }}" height="18">
<linearGradient id="smooth" x2="0" y2="100%">
<stop offset="0" stop-color="#fff" stop-opacity=".7"/>
<stop offset=".1" stop-color="#aaa" stop-opacity=".1"/>
Expand All @@ -43,7 +43,8 @@ class SvgRender implements RenderInterface
<text x="{{ valueStartPosition }}" y="13" fill="#010101" fill-opacity=".3">{{ value }}</text>
<text x="{{ valueStartPosition }}" y="12">{{ value }}</text>
</g>
</svg>';
</svg>
EOF;

/**
* Constructor.
Expand Down Expand Up @@ -78,15 +79,13 @@ public function render(Badge $badge)
$parameters['vendorStartPosition'] = round($parameters['vendorWidth'] / 2, 1) + 1;
$parameters['valueStartPosition'] = $parameters['vendorWidth'] + round($parameters['valueWidth'] / 2, 1) - 1;

return $this->renderSvg(self::$template, $parameters);
return $this->renderSvg(self::$template, $parameters, $badge->getFormat());
}

/**
* Render a badge.
*
* @param Badge $badge
* A list of all supported formats.
*
* @return string
* @return array
*/
public function supportedFormats()
{
Expand All @@ -98,12 +97,12 @@ private function stringWidth($text)
return $this->textSizeCalculator->calculateWidth($text);
}

private function renderSvg($render, $parameters)
private function renderSvg($render, $parameters, $format)
{
foreach ($parameters as $key => $variable) {
$render = str_replace(sprintf('{{ %s }}', $key), $variable, $render);
}

return $render;
return Image::createFromString($render, $format);
}
}

0 comments on commit cbca156

Please sign in to comment.