Skip to content

Commit

Permalink
No longer use array key to lookup attributes.
Browse files Browse the repository at this point in the history
Rely on ::name() by default and ::alias() if provided.
  • Loading branch information
lukemorton committed May 2, 2012
1 parent b86372d commit 6fad3ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
19 changes: 5 additions & 14 deletions classes/Gignite/TheCure/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
/**
* A field
*
* // Enable setter method functionality
* new Field('name', array('setter' => TRUE))
* // A field with a name of "name"
* new Field('name');
*
* // A field with a name of "location" but aliased to "town"
* new Field('name', array('alias' => 'town'));
*
* // Provide a default value
* new Field('verified', array('value' => FALSE));
Expand All @@ -19,8 +22,6 @@ class Field extends Attribute {

protected $value;

protected $setter;

protected $rules;

/**
Expand All @@ -33,16 +34,6 @@ public function value()
return $this->value;
}

/**
* Is this field a setter?
*
* @return boolean
*/
public function is_setter()
{
return $this->setter;
}

/**
* Get field rules.
*
Expand Down
32 changes: 17 additions & 15 deletions classes/Gignite/TheCure/Models/Magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public function __container(Container $container = NULL)
$this->__container = $container;
}

private function field($fields, $method)
{
foreach ($fields as $_field)
{
if ($_field->alias() === $method)
{
return $_field;
}
}
}

/**
* @param $fields
* @param $method
Expand All @@ -53,25 +64,24 @@ public function __container(Container $container = NULL)
*/
private function relation_action($fields, $method, array $args = NULL)
{
if (isset($fields[$method]) AND $args)
if ($field = $this->field($fields, $method) AND $args)
{
$verb = 'Set';
$key = $method;
}
else
{
$verb = current(explode('_', $method));
$key = substr($method, strlen($verb) + 1);
$field = $this->field($fields, substr($method, strlen($verb) + 1));
}

$interface = ucfirst($verb);
$interface = "Gignite\\TheCure\\Relation\\{$interface}";

if (interface_exists($interface)
AND isset($fields[$key])
AND $fields[$key] instanceOf $interface)
AND $field
AND $field instanceOf $interface)
{
return array($fields[$key], $verb);
return array($field, $verb);
}
}

Expand All @@ -91,18 +101,10 @@ public function __call($method, $args)
list($field, $action) = $field_action;
$field->{$action}($this->__container(), $this, $args[0]);
}
elseif (isset($fields[$method]))
elseif ($field = $this->field($fields, $method))
{
$field = $fields[$method];

if ($args)
{
if ( ! $field->is_setter())
{
throw new \BadMethodCallException(
'You cannot pass arguments to a non-setter field.');
}

$object->{$field->name()} = $args[0];
return;
}
Expand Down

0 comments on commit 6fad3ee

Please sign in to comment.