Merge remote-tracking branch 'eddiejaoude/master'

This commit is contained in:
Dominik Liebler 2013-09-03 14:38:46 +02:00
commit ba78aa87d1
10 changed files with 109 additions and 36 deletions

View File

@ -33,7 +33,7 @@ abstract class AbstractFactory
abstract public function createText($content);
/**
* Createss a picture component
* Creates a picture component
*
* @param string $path
* @param string $name

View File

@ -1,20 +1,24 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\AbstractFactory\Html;
use DesignPatterns\AbstractFactory\Picture as BasePicture;
/**
* Class Picture
*
* Picture is a concrete image for HTML rendering
*
* @package DesignPatterns\AbstractFactory\Html
*/
class Picture extends BasePicture
{
// some crude rendering from HTML output
/**
* some crude rendering from HTML output
*
* @return string
*/
public function render()
{
return sprintf('<img src="%s" title="$s"/>', $this->_path, $this->_name);

View File

@ -1,22 +1,27 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\AbstractFactory\Html;
use DesignPatterns\AbstractFactory\Text as BaseText;
/**
* Class Text
*
* Text is a concrete text for HTML rendering
*
* @package DesignPatterns\AbstractFactory\Html
*/
class Text extends BaseText
{
/**
* some crude rendering from HTML output
*
* @return string
*/
public function render()
{
return "<div>" . htmlspecialchars($this->_text) . '</div>';
return '<div>' . htmlspecialchars($this->_text) . '</div>';
}
}

View File

@ -1,22 +1,35 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\AbstractFactory;
/**
* Class HtmlFactory
*
* HtmlFactory is a concrete factory for HTML component
*
* @package DesignPatterns\AbstractFactory
*/
class HtmlFactory extends AbstractFactory
{
/**
* Creates a picture component
*
* @param string $path
* @param string $name
* @return Html\Picture|Picture
*/
public function createPicture($path, $name = '')
{
return new Html\Picture($path, $name);
}
/**
* Creates a text component
*
* @param string $content
* @return Html\Text|Text
*/
public function createText($content)
{
return new Html\Text($content);

View File

@ -1,20 +1,24 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\AbstractFactory\Json;
use DesignPatterns\AbstractFactory\Picture as BasePicture;
/**
* Class Picture
*
* Picture is a concrete image for JSON rendering
*
* @package DesignPatterns\AbstractFactory\Json
*/
class Picture extends BasePicture
{
// some crude rendering from JSON output
/**
* some crude rendering from JSON output
*
* @return string
*/
public function render()
{
return json_encode(array('title' => $this->_name, 'path' => $this->_path));

View File

@ -1,19 +1,24 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\AbstractFactory\Json;
use DesignPatterns\AbstractFactory\Text as BaseText;
/**
* Class Text
*
* Text is a text component with a JSON rendering
*
* @package DesignPatterns\AbstractFactory\Json
*/
class Text extends BaseText
{
/**
* some crude rendering from JSON output
*
* @return string
*/
public function render()
{
return json_encode(array('content' => $this->_text));

View File

@ -1,23 +1,37 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\AbstractFactory;
/**
* Class JsonFactory
*
* JsonFactory is a factory for creating a family of JSON component
* (example for ajax)
*
* @package DesignPatterns\AbstractFactory
*/
class JsonFactory extends AbstractFactory
{
/**
* Creates a picture component
*
* @param string $path
* @param string $name
* @return Json\Picture|Picture
*/
public function createPicture($path, $name = '')
{
return new Json\Picture($path, $name);
}
/**
* Creates a text component
*
* @param string $content
* @return Json\Text|Text
*/
public function createText($content)
{
return new Json\Text($content);

View File

@ -3,11 +3,20 @@
namespace DesignPatterns\AbstractFactory;
/**
* Interface Media
*
* This contract is not part of the pattern, in general case, each component
* are not related
*
* @package DesignPatterns\AbstractFactory
*/
interface Media
{
/**
* some crude rendering from JSON or html output (depended on concrete class)
*
* @return string
*/
function render();
}

View File

@ -2,19 +2,31 @@
namespace DesignPatterns\AbstractFactory;
/**
* Class Picture
*
* @package DesignPatterns\AbstractFactory
*/
abstract class Picture implements Media
{
protected $_path;
protected $_name;
/**
*
* @var string
*/
protected $path;
/**
* @var string
*/
protected $name;
/**
* @param string $path
* @param string $name
*/
public function __construct($path, $name = '')
{
$this->_name = (string) $name;
$this->_path = (string) $path;
$this->name = (string) $name;
$this->path = (string) $path;
}
}

View File

@ -2,16 +2,23 @@
namespace DesignPatterns\AbstractFactory;
/**
* Class Text
*
* @package DesignPatterns\AbstractFactory
*/
abstract class Text implements Media
{
/**
*
* @var string
*/
protected $_text;
protected $text;
/**
* @param string $text
*/
public function __construct($text)
{
$this->_text = $text;
$this->_text = (string) $text;
}
}