Skip to content

Instantly share code, notes, and snippets.

@texdc
Last active August 29, 2015 13:56
Show Gist options
  • Save texdc/8891863 to your computer and use it in GitHub Desktop.
Save texdc/8891863 to your computer and use it in GitHub Desktop.
<?php
namespace Barcode;
use Traversable;
use Barcode\Builder\ObjectBuilderInterface as ObjectBuilder;
use Barcode\Builder\RendererBuilderInterface as RendererBuilder;
use Barcode\Object\SpecificationInterface as ObjectSpecification;
use Barcode\Renderer\SpecificationInterface as RenderSpecification;
/**
* The application service should provide a simple interface which hides the
* domain-specific implementation details.
*/
interface ServiceInterface
{
/**
* Encode the text into a rendered barcode
*
* @param string $text the text to encode
* @param ObjectSpecification|array|Traversable $objectSpec the object specification
* @param RenderSpecification|array|Traversable $renderSpec the render specification
* @return mixed
*/
public function encode($text, $objectSpec, $renderSpec);
/**
* Get the object builder
*
* @return ObjectBuilder
*/
public function objectBuilder();
/**
* Get the renderer builder
*
* @return RendererBuilder
*/
public function rendererBuilder();
}
<?php
namespace Barcode;
use Util\ValueTrait;
final class Format
{
use ValueTrait;
const ASCII = 'ascii';
const GIF = 'gif';
const JPEG = 'jpeg';
const PDF = 'pdf';
const PNG = 'png';
const SVG = 'svg';
protected static $validValues = [
self::ASCII,
self::GIF,
self::JPEG,
self::PDF,
self::PNG,
self::SVG
];
}
<?php
namespace Barcode\Builder;
use Traversable;
use Barcode\Object\ObjectInterface;
use Barcode\Object\SpecificationInterface;
interface ObjectBuilderInterface extends BuilderInterface
{
/**
* Build a barcode object
*
* @param SpecificationInterface|array|Traversable $objectSpec the object specification
* @param string|null $text optional barcode text
* @return ObjectInterface
*/
public function build($objectSpec, $text = null);
}
<?php
namespace Barcode\Builder;
use Traversable;
use Barcode\Renderer\RendererInterface;
use Barcode\Renderer\SpecificationInterface;
interface RendererBuilderInterface extends BuilderInterface
{
/**
* Build a barcode renderer
*
* @param SpecificationInterface|array|Traversable $renderSpec the render specification
* @return RendererInterface
*/
public function build($renderSpec);
}
<?php
namespace Barcode;
use Util\ValueTrait;
final class Type
{
use ValueTrait;
const CODABAR = 'codabar';
const CODE25 = 'code25';
const CODE25I = 'code25i';
const CODE39 = 'code39';
const CODE128 = 'code128';
const EAN2 = 'ean2';
const EAN8 = 'ean8';
const EAN13 = 'ean13';
// etc.
protected static $validValues = [
self::CODABAR,
self::CODE25,
self::CODE25I,
self::CODE39,
self::CODE128,
self::EAN2,
self::EAN8,
self::EAN13,
// etc.
];
}
<?php
namespace Util;
use DomainException;
trait ValueTrait
{
private $value;
public function __construct($value)
{
if (!in_array($values, static::$validValues)) {
throw new DomainException("Invalid value [$value]");
}
$this->value = $value;
}
public static function __callStatic($value, array $arguments = [])
{
return new static(strtolower($value));
}
public function is($other)
{
if (is_string($other)) {
$other = static::__callStatic($other);
}
return $this == $other;
}
public function value()
{
return $this->value;
}
public function __toString()
{
return $this->value();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment