Permalink
Browse files

updating datamapper to be easier to use in a model, including example…

… of it as well
  • Loading branch information...
1 parent 56a1d13 commit 09e15a67cd1a8342617f5d5e759bb283cfb57899 @enygma committed Jun 3, 2011
Showing with 100 additions and 24 deletions.
  1. +58 −5 lib/DataMapper.php
  2. +42 −19 objectMap.php
View
@@ -3,15 +3,30 @@
class DataMapper
{
private $_mappings = null;
+ private $_sourceObj = null;
/**
* Set up object with configuration settings
- * @param $settings
+ * @param array $settings Settings array
+ * @param object $sourceObj[optional] If object is defined, used as source for mapping
* @return void
*/
- public function configure($settings)
+ public function configure($settings,$sourceObj=null)
{
$this->_mappings = $settings;
+ if($sourceObj != null){
+ $this->_sourceObj = $sourceObj;
+ }
+ }
+
+ /**
+ * Sets the source object to pull from
+ *
+ * @param object $object Source object (any type)
+ */
+ public function setSource($object)
+ {
+ $this->_sourceObj = $object;
}
/**
@@ -28,14 +43,38 @@ public function execute()
}
foreach($map->_settings as $name => $value){
- $methodName = '_'.$name;
+ $nameParts = explode(':',$name);
+ $isInput = false;
+ $methodName = '';
+
+ if(count($nameParts)>1){
+ // append them
+ if($nameParts[1]=='input'){ $isInput = true; }
+ $methodName = '_'.array_pop($nameParts);
+ foreach($nameParts as $part){
+ $methodName .= ucwords(strtolower($part));
+ }
+ }else{
+ $methodName = '_'.$name;
+ }
+
if(method_exists($this,$methodName)){
+ if(count($value)==1 && $this->_sourceObj!=null && $isInput){
+ $value = array($this->_sourceObj,$value);
+ }
+
$this->$methodName($this->_mappings[$property],$value);
}
}
}
}
}
+
+ /**
+ * Given a name, tries to match against actual properties and aliases in the map
+ *
+ * @param string $name Value to search for
+ */
public function __get($name)
{
foreach($this->_mappings as $property => $map){
@@ -62,22 +101,36 @@ public function __get($name)
public function _inputMap(&$mapObject,$mapValue)
{
$expand = new ExpandObject();
-
+
$returnValue = (is_array($mapValue)) ? $expand->expand($mapValue[0],$mapValue[1]) : $mapValue;
if(is_array($mapValue) && isset($mapValue[2])){
$returnValue = $returnValue->$mapValue[2]();
}
$mapObject->_value = $returnValue;
}
+
+ /**
+ * Callback: handle the output mapping given the current value
+ *
+ * @param object $mapObject Current map's object
+ * @param mixed $mapValue Mapping to apply value to
+ */
public function _outputMap(&$mapObject,$mapValue)
{
$expand = new ExpandObject();
$expand->apply($mapObject,$mapValue,$mapObject->_value);
}
+
+ /**
+ * Callback: handle the output and "decorate" it
+ *
+ * @param object $mapObject Current map's object
+ * @param mixed $mapValue Value for the current mapping
+ */
public function _outputDecorator(&$mapObject,$mapValue)
{
- //var_dump($mapValue);
+ //TODO: figure out decorators
}
}
View
@@ -28,37 +28,61 @@ public function output()
}
class Ticket
{
- public $foo = null;
- public $sample = null;
+ public $foo = null;
+ public $sample = null;
+ private $_objectMap = null;
public function __construct()
{
- $this->foo = new stdClass();
+ // set up the ticket object's data mapper
+ $this->_objectMap = new DataMapper();
+ $this->_objectMap->configure(array(
+ 'title' => new MapItem(array(
+ 'map:input' => 'tickets->name',
+ 'map:output' => 'mytitle',
+ 'alias' => 'mTitle'
+ ))
+ ));
+
+ //-------------------
+ /*$this->foo = new stdClass();
$this->foo->bar = 'baz';
-
$this->foo->myarr = array('blah');
- $this->sample = new Sample();
+ $this->sample = new Sample();*/
}
+
+ public function returnMapped($sample)
+ {
+ // return the data from our $responseObject mapped using our map
+ if($this->_objectMap != null){
+
+ $this->_objectMap->setSource($sample);
+ $this->_objectMap->execute();
+
+ //var_dump($this->_objectMap);
+ return $this->_objectMap;
+
+ }else{
+ return $responseObj;
+ }
+ }
public function foo()
{
echo 'Ticket::foo';
}
}
+$sample = new Sample();
+$sample->tickets = new stdClass();
+$sample->tickets->name = "i'll be mytitle";
+
+// remap tickets->name to $return->mTitle
$ticket = new Ticket();
+$return = $ticket->returnMapped($sample);
+echo 'end: '.$return->mTitle."\n";
-class mnDecorate extends MapDecorator
-{
- public function decorate($data)
- {
-
- }
-}
//--------------
-
-$decoratorOne = new mnDecorate();
-
$dm = new DataMapper();
$dm->configure(array(
@@ -76,11 +100,10 @@ public function decorate($data)
)),
'middle_name' => new MapItem(array(
'inputMap' => 'testing middle name',
- 'outputMap' => 'property',
- //'outputDecorator' => $decoratorOne
+ 'outputMap' => 'property'
))
));
-$dm->execute();
+//$dm->execute();
/*
echo 'fname:'.$dm->firstName."\n";
@@ -90,7 +113,7 @@ public function decorate($data)
echo 'fullpath: '; var_dump($dm->last_name->map->to->lname);
*/
-echo 'fullpath: '; var_dump($dm->last_name->map->to->lname);
+//echo 'fullpath: '; var_dump($dm->last_name->map->to->lname);
echo "//--------------------------\n";
echo "\n\n";

0 comments on commit 09e15a6

Please sign in to comment.