mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-01 20:50:15 +02:00
Merge remote-tracking branch 'eddiejaoude/master'
This commit is contained in:
@@ -33,7 +33,7 @@ abstract class AbstractFactory
|
|||||||
abstract public function createText($content);
|
abstract public function createText($content);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Createss a picture component
|
* Creates a picture component
|
||||||
*
|
*
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param string $name
|
* @param string $name
|
||||||
|
@@ -1,20 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\AbstractFactory\Html;
|
namespace DesignPatterns\AbstractFactory\Html;
|
||||||
|
|
||||||
use DesignPatterns\AbstractFactory\Picture as BasePicture;
|
use DesignPatterns\AbstractFactory\Picture as BasePicture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Class Picture
|
||||||
|
*
|
||||||
* Picture is a concrete image for HTML rendering
|
* Picture is a concrete image for HTML rendering
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory\Html
|
||||||
*/
|
*/
|
||||||
class Picture extends BasePicture
|
class Picture extends BasePicture
|
||||||
{
|
{
|
||||||
|
|
||||||
// some crude rendering from HTML output
|
/**
|
||||||
|
* some crude rendering from HTML output
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
return sprintf('<img src="%s" title="$s"/>', $this->_path, $this->_name);
|
return sprintf('<img src="%s" title="$s"/>', $this->_path, $this->_name);
|
||||||
|
@@ -1,22 +1,27 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\AbstractFactory\Html;
|
namespace DesignPatterns\AbstractFactory\Html;
|
||||||
|
|
||||||
use DesignPatterns\AbstractFactory\Text as BaseText;
|
use DesignPatterns\AbstractFactory\Text as BaseText;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Class Text
|
||||||
|
*
|
||||||
* Text is a concrete text for HTML rendering
|
* Text is a concrete text for HTML rendering
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory\Html
|
||||||
*/
|
*/
|
||||||
class Text extends BaseText
|
class Text extends BaseText
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* some crude rendering from HTML output
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
return "<div>" . htmlspecialchars($this->_text) . '</div>';
|
return '<div>' . htmlspecialchars($this->_text) . '</div>';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -1,22 +1,35 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\AbstractFactory;
|
namespace DesignPatterns\AbstractFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Class HtmlFactory
|
||||||
|
*
|
||||||
* HtmlFactory is a concrete factory for HTML component
|
* HtmlFactory is a concrete factory for HTML component
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory
|
||||||
*/
|
*/
|
||||||
class HtmlFactory extends AbstractFactory
|
class HtmlFactory extends AbstractFactory
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a picture component
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param string $name
|
||||||
|
* @return Html\Picture|Picture
|
||||||
|
*/
|
||||||
public function createPicture($path, $name = '')
|
public function createPicture($path, $name = '')
|
||||||
{
|
{
|
||||||
return new Html\Picture($path, $name);
|
return new Html\Picture($path, $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a text component
|
||||||
|
*
|
||||||
|
* @param string $content
|
||||||
|
* @return Html\Text|Text
|
||||||
|
*/
|
||||||
public function createText($content)
|
public function createText($content)
|
||||||
{
|
{
|
||||||
return new Html\Text($content);
|
return new Html\Text($content);
|
||||||
|
@@ -1,20 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\AbstractFactory\Json;
|
namespace DesignPatterns\AbstractFactory\Json;
|
||||||
|
|
||||||
use DesignPatterns\AbstractFactory\Picture as BasePicture;
|
use DesignPatterns\AbstractFactory\Picture as BasePicture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Class Picture
|
||||||
|
*
|
||||||
* Picture is a concrete image for JSON rendering
|
* Picture is a concrete image for JSON rendering
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory\Json
|
||||||
*/
|
*/
|
||||||
class Picture extends BasePicture
|
class Picture extends BasePicture
|
||||||
{
|
{
|
||||||
|
|
||||||
// some crude rendering from JSON output
|
/**
|
||||||
|
* some crude rendering from JSON output
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
return json_encode(array('title' => $this->_name, 'path' => $this->_path));
|
return json_encode(array('title' => $this->_name, 'path' => $this->_path));
|
||||||
|
@@ -1,19 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\AbstractFactory\Json;
|
namespace DesignPatterns\AbstractFactory\Json;
|
||||||
|
|
||||||
use DesignPatterns\AbstractFactory\Text as BaseText;
|
use DesignPatterns\AbstractFactory\Text as BaseText;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Class Text
|
||||||
|
*
|
||||||
* Text is a text component with a JSON rendering
|
* Text is a text component with a JSON rendering
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory\Json
|
||||||
*/
|
*/
|
||||||
class Text extends BaseText
|
class Text extends BaseText
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* some crude rendering from JSON output
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
return json_encode(array('content' => $this->_text));
|
return json_encode(array('content' => $this->_text));
|
||||||
|
@@ -1,23 +1,37 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\AbstractFactory;
|
namespace DesignPatterns\AbstractFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Class JsonFactory
|
||||||
|
*
|
||||||
* JsonFactory is a factory for creating a family of JSON component
|
* JsonFactory is a factory for creating a family of JSON component
|
||||||
* (example for ajax)
|
* (example for ajax)
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory
|
||||||
*/
|
*/
|
||||||
class JsonFactory extends AbstractFactory
|
class JsonFactory extends AbstractFactory
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a picture component
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param string $name
|
||||||
|
* @return Json\Picture|Picture
|
||||||
|
*/
|
||||||
public function createPicture($path, $name = '')
|
public function createPicture($path, $name = '')
|
||||||
{
|
{
|
||||||
return new Json\Picture($path, $name);
|
return new Json\Picture($path, $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a text component
|
||||||
|
*
|
||||||
|
* @param string $content
|
||||||
|
* @return Json\Text|Text
|
||||||
|
*/
|
||||||
public function createText($content)
|
public function createText($content)
|
||||||
{
|
{
|
||||||
return new Json\Text($content);
|
return new Json\Text($content);
|
||||||
|
@@ -3,11 +3,20 @@
|
|||||||
namespace DesignPatterns\AbstractFactory;
|
namespace DesignPatterns\AbstractFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Interface Media
|
||||||
|
*
|
||||||
* This contract is not part of the pattern, in general case, each component
|
* This contract is not part of the pattern, in general case, each component
|
||||||
* are not related
|
* are not related
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory
|
||||||
*/
|
*/
|
||||||
interface Media
|
interface Media
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* some crude rendering from JSON or html output (depended on concrete class)
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
function render();
|
function render();
|
||||||
}
|
}
|
||||||
|
@@ -2,19 +2,31 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\AbstractFactory;
|
namespace DesignPatterns\AbstractFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Picture
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory
|
||||||
|
*/
|
||||||
abstract class Picture implements Media
|
abstract class Picture implements Media
|
||||||
{
|
{
|
||||||
protected $_path;
|
|
||||||
protected $_name;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $name;
|
||||||
|
|
||||||
|
/**
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public function __construct($path, $name = '')
|
public function __construct($path, $name = '')
|
||||||
{
|
{
|
||||||
$this->_name = (string) $name;
|
$this->name = (string) $name;
|
||||||
$this->_path = (string) $path;
|
$this->path = (string) $path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,16 +2,23 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\AbstractFactory;
|
namespace DesignPatterns\AbstractFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Text
|
||||||
|
*
|
||||||
|
* @package DesignPatterns\AbstractFactory
|
||||||
|
*/
|
||||||
abstract class Text implements Media
|
abstract class Text implements Media
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $_text;
|
protected $text;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $text
|
||||||
|
*/
|
||||||
public function __construct($text)
|
public function __construct($text)
|
||||||
{
|
{
|
||||||
$this->_text = $text;
|
$this->_text = (string) $text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user