Skip to content

Commit

Permalink
refactored everything
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtlambda committed Feb 28, 2015
1 parent 783765b commit b34cdf6
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 234 deletions.
13 changes: 7 additions & 6 deletions src/fieldwork/components/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace fieldwork\components;

use fieldwork\core\Component;
use fieldwork\core\Node;
use fieldwork\core\Field;

class Group extends Component
class Group extends Node
{

public function getHTML ($before = '', $after = '', $prefix = '', $suffix = '')
{
$html = '';
foreach ($this->children as $component)
/* @var $component Component */
/* @var $component Node */
if ($component->isActive())
$html .= $this->renderChild($before, $after, $prefix, $suffix, $component);
return $html;
Expand All @@ -23,7 +23,7 @@ public function isValid (&$error = null)
if (!parent::isValid($error))
return false;
foreach ($this->children as $component)
/* @var $component Component */
/* @var $component Node */
if ($component->isActive() && !$component->isValid($error))
return false;
return true;
Expand All @@ -34,9 +34,10 @@ public function isValid (&$error = null)
* @param $after
* @param $prefix
* @param $suffix
* @param Component $component
* @param Node $component
*
* @return string
*@return string
*/
private function renderChild ($before, $after, $prefix, $suffix, $component)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace fieldwork\components;

use fieldwork\core\Component;
use fieldwork\core\Node;

/**
* Allows adding arbitrary markup to the form
* @package fieldwork\components
*/
class HTMLComponent extends Component
class HTMLNode extends Node
{

private $html;
Expand Down
153 changes: 44 additions & 109 deletions src/fieldwork/core/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@

namespace fieldwork\core;

/**
* Represents an HTML node
* @package fieldwork\core
* @identifier fieldwork.component
*/
abstract class Component extends Validatable
abstract class Component extends Stateful
{

protected $parent;
protected $parent = null;
protected $children = array();
protected $isWrapped = true;

private $slug;
private $active = true;
private $classes = array();
private $attributes = array();

const BASE_CLASS = 'fieldwork';
protected $slug;
protected $active = true;

public function __construct ($slug)
{
Expand All @@ -33,73 +22,25 @@ public function __construct ($slug)
public function reset ()
{
foreach ($this->getChildren(false) as $child)
/* @var $child Component */
/* @var $child Node */
$child->reset();
return $this;
}

abstract public function getHTML ();

protected function add (Component $component)
{
$this->children[] = $component;
}

/**
* Checks whether the component is visible on-screen and should thus be wrapped in whatever markup is provided by
* the parent component
* @return boolean
*/
public function isWrapped ()
{
return $this->isWrapped;
}

/**
* Adds component to given parent component
*
* @param Component $parent
* @param Node $parent
*
* @return static
*/
public function addTo (Component $parent)
public function addTo (Node $parent)
{
$parent->add($this);
$this->parent = $parent;
return $this;
}

/**
* Adds class(es) to this component's node
*
* @param string|array $class
*
* @return static
*/
public function addClass ($class)
{
$targetArray = &$this->classes;
if (!is_array($class))
$targetArray[] = $class;
else
$targetArray = array_merge($targetArray, $class);
return $this;
}

/**
* Sets a custom attribute for this component's node
*
* @param string $attr Attribute name
* @param string|null $value Attribtue value
*
* @return static
*/
public function setAttribute ($attr, $value = null)
{
$this->attributes[$attr] = $value !== null ? $value : $attr;
return $this;
}

/**
* Sets whether the component is active
*
Expand Down Expand Up @@ -134,7 +75,7 @@ public function getChildren ($recursive = true, $includeInactiveFields = false)
{
$children = array();
foreach ($this->children as $component)
/* @var $component Component */
/* @var $component Node */
if ($component->isActive() || $includeInactiveFields) {
array_push($children, $component);
if ($recursive)
Expand All @@ -151,12 +92,12 @@ public function getChildren ($recursive = true, $includeInactiveFields = false)
* @param boolean $recursive
* @param boolean $includeInactiveFields whether to include inactive fields
*
* @return Component|null
* @return Node|null
*/
public function getChildBySlug ($slug, $recursive = true, $includeInactiveFields = false)
{
$children = $this->getChildren($recursive, $includeInactiveFields);
/* @var $child Component */
/* @var $child Node */
foreach ($children as $child)
if ($child->getGlobalSlug() == $slug)
return $child;
Expand All @@ -169,74 +110,68 @@ public function getChildBySlug ($slug, $recursive = true, $includeInactiveFields
/**
* Check if given component is child
*
* @param Component $child component to search for
* @param boolean $recursive whether or not to search recursively
* @param Node $child component to search for
* @param boolean $recursive whether or not to search recursively
*
* @return boolean
*/
public function hasChild ($child, $recursive = true)
{
foreach ($this->children as $component)
/* @var $component Component */
/* @var $component Node */
if ($component == $child || ($recursive && $component->hasChild($child, true)))
return true;
return false;
}

/**
* Gets all HTML attributes
* @return array array of attributes
*/
public function getAttributes ()
public function getLocalSlug ()
{
return array_merge(
$this->attributes, array('class' => implode(' ', $this->getClasses())));
return $this->slug;
}

/**
* Gets all HTML attributes
* @return string attributes as string
*/
public function getAttributesString ()
public function getGlobalSlug ()
{
$attributePairs = array();
foreach ($this->getAttributes() as $attr => $value)
$attributePairs[] = "$attr=\"" . str_replace("\"", "\\\"", $value) . "\"";
return implode(' ', $attributePairs);
if ($this->parent instanceof Node)
return $this->parent->getGlobalSlug() . '-' . $this->slug;
else
return $this->slug;
}

/**
* Gets HTML class attribute
* @return array array of classes
* Gets the root to this component (first element in the array of ancestors). Returns null if the component has no
* parent
* @return Node|null
*/
public function getClasses ()
public function getRoot ()
{
return array_merge(array(
self::BASE_CLASS
), $this->classes);
$ancestors = $this->getAncestors();
return !(count($ancestors)) ? null : $ancestors[0];
}

public function getLocalSlug ()
/**
* Gets the parent to the component. Returns null if no parent is defined
* @return Node|null
*/
public function getParent ()
{
return $this->slug;
return $this->parent;
}

public function getGlobalSlug ()
/**
* Returns an array of ancestors, root-first
* @return array
*/
public function getAncestors ()
{
if ($this->parent instanceof Component)
return $this->parent->getGlobalSlug() . '-' . $this->slug;
else
return $this->slug;
$ancestors = [];
$component = $this;
while ($component->getParent() instanceof Node)
array_unshift($ancestors, ($component = $component->getParent()));
return $ancestors;
}

/**
* @return Component
*/
public function getRoot ()
protected function add (Node $component)
{
if ($this->parent instanceof Component)
return $this->parent->getRoot();
else
return $this;
$this->children[] = $component;
}
}
27 changes: 1 addition & 26 deletions src/fieldwork/core/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @package fieldwork\core
* @identifier field
*/
abstract class Field extends Component implements EventEmitterInterface
abstract class Field extends Node implements EventEmitterInterface
{

use EventEmitterTrait;
Expand Down Expand Up @@ -88,7 +88,6 @@ public function setValue ($value)
return $this;
}


public function getObjectData ()
{
$validators = [];
Expand Down Expand Up @@ -117,7 +116,6 @@ function describeObject ()
return 'field';
}


/**
* Gets the id attribute
* @return string
Expand Down Expand Up @@ -146,19 +144,6 @@ public function getClasses ()
));
}

/**
* Adds sanitizer
*
* @param Sanitizer $s
*
* @return Field
*/
public function addSanitizer (Sanitizer $s)
{
$this->sanitizers[] = $s;
return $this;
}

/**
* Forcibly sets this field to return false on validation
* @return static
Expand All @@ -168,14 +153,4 @@ public function forceInvalid ()
$this->forceInvalid = true;
return $this;
}

/**
* Applies each of the sanitizers to the current value
*/
public function sanitize ()
{
foreach ($this->sanitizers as $sanitizer)
/* @var $sanitizer Sanitizer */
$this->value = $sanitizer->sanitize($this->value);
}
}
Loading

0 comments on commit b34cdf6

Please sign in to comment.