Skip to content

Instantly share code, notes, and snippets.

@samacs
Created October 22, 2012 16:23
Show Gist options
  • Save samacs/3932354 to your computer and use it in GitHub Desktop.
Save samacs/3932354 to your computer and use it in GitHub Desktop.
A set of classes to demonstrate polymorphism.
<?php
require_once 'StudentProfile.php';
$studentProfile = new StudentProfile(
array(
'fields' => array(
ProfileField::factory('Name', 'Buddy'),
ProfileField::factory('Birth date', '1983/05/05'),
ProfileField::factory('Graduation date', '2000/01/01'),
),
)
);
echo $studentProfile->format();
<?php
/**
* Formatter interface.
*
* A contract to format.
*
* @category Formatter
* @package Formatter
* @author Saul Martinez <saul@sharkwebintelligence.com>
* @copyright 2012 Shark Web Intelligence
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version 1.0
* @link http://www.sharkwebintelligence.com
*/
interface IFormatter
{
/**
* Object formatting.
*
* @return string
*/
public function format();
}
<?php
require_once 'IFormatter.php';
require_once 'ProfileField.php';
/**
* Profile.
*
* Profile representation.
*
* @category Formatter
* @package Profile
* @author Saul Martinez <saul@sharkwebintelligence.com>
* @copyright 2012 Shark Web Intelligence
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version 1.0
* @link http://www.sharkwebintelligence.com
*/
abstract class Profile implements IFormatter
{
/**
* @var array $fields Profile fields.
*/
public $fields;
/**
* Constructor.
*
* - Set options.
*
* @param array $options Profile options.
*/
public function __construct($options)
{
if (is_array($options)) {
$this->setOptions($options);
}
}
/**
* Format implementation.
*
* @return string
*/
public function format()
{
$output = '';
foreach ($this->getFields() as $field) {
$output .= $field;
}
return $output;
}
/**
* Adds a field to the fields list.
*
* @param ProfileField $field Field to add.
*
* @return Profile Provides a fluent interface.
*/
public function addField(ProfileField $field)
{
$this->fields[] = $field;
return $this;
}
/**
* Set profile fields.
*
* @param array $fields Profile fields.
*
* @return Profile Provides a fluent interface.
*/
public function setFields(array $fields)
{
$this->fields = $fields;
return $this;
}
/**
* Get profile fields.
*
* @return array
*/
public function getFields()
{
return $this->fields;
}
/**
* Set profile options.
*
* @param array $options Profile options.
*
* @return Profile Provides a fluent interface.
*/
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $name => $value) {
$method = 'set' . ucfirst($name);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
}
<?php
require_once 'IFormatter.php';
/**
* Profile field.
*
* Holds profile properties.
*
* @category Formatter
* @package Profile
* @subpackage Field
* @author Saul Martinez <saul@sharkwebintelligence.com>
* @copyright 2012 Shark Web Intelligence
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version 1.0
* @link http://www.sharkwebintelligence.com
*/
class ProfileField
{
/**
* @var string $name Field name.
*/
public $name;
/**
* @var mixed $value Field value.
*/
public $value;
/**
* Factory method to create a profile field.
*
* @param string $name Field name.
* @param mixed $value Field value.
*
* @return ProfileField
*/
public static function factory($name, $value)
{
$field = new self();
$field->name = $name;
$field->value = $value;
return $field;
}
/**
* Format the profile field.
*
* @return string
*/
public function __toString()
{
return $this->name . ': ' . $this->value . PHP_EOL;
}
}
<?php
require_once 'Profile.php';
/**
* Student profile.
*
* Just a profile sample.
*
* @category Formatter
* @package Profile
* @subpackage Student
* @author Saul Martinez <saul@sharkwebintelligence.com>
* @copyright 2012 Shark Web Intelligence
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version 1.0
* @link http://www.sharkwebintelligence.com
*/
class StudentProfile extends Profile
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment