예제 #1
0
 /**
  * @param stdClass    $from   The instance
  * @param array       $to     The raw data
  * @param ModelConfig $config
  */
 protected function convertToData($instance, $config)
 {
     $to = [];
     $from = $instance;
     // Put the belongsTo columns at the beginning of the array
     foreach ($config->belongsTo as $property => $relation) {
         $to[$relation['reference']] = null;
         // Dont set the value yet. (could be overwritten with an mapping?)
     }
     // Map to data
     foreach ($config->properties as $element => $property) {
         $value = PropertyPath::get($property, $from);
         if (isset($config->writeFilters[$element])) {
             $value = \Sledgehammer\filter($value, $config->writeFilters[$element]);
         }
         PropertyPath::set($element, $value, $to);
     }
     // Map the belongTo to the "*_id" columns.
     foreach ($config->belongsTo as $property => $relation) {
         if ($from->{$property} === null) {
             $to[$relation['reference']] = null;
         } else {
             $column = $relation['id'];
             $belongsToConfig = $this->_getConfig($relation['model']);
             $to[$relation['reference']] = PropertyPath::get($belongsToConfig->properties[$column], $from->{$property});
         }
     }
     return $to;
 }
예제 #2
0
 public function test_PropertyPath_set()
 {
     $array = array('id' => '1');
     $object = (object) array('id' => '2');
     PropertyPath::set('id', 3, $array);
     $this->assertSame($array['id'], 3);
     PropertyPath::set('id', 4, $object);
     $this->assertSame($object->id, 4);
     PropertyPath::set('->id', 5, $object);
     $this->assertSame($object->id, 5);
     PropertyPath::set('[id]', 6, $array);
     $this->assertSame($array['id'], 6);
     $array['object'] = (object) array('id' => 7);
     PropertyPath::set('object->id', 8, $array);
     $this->assertSame($array['object']->id, 8);
     PropertyPath::set('[object]->id', 9, $array);
     $this->assertSame($array['object']->id, 9);
     $array['element'] = array('id' => 1);
     PropertyPath::set('element[id]', 10, $array);
     $this->assertSame($array['element']['id'], 10);
 }
예제 #3
0
 /**
  * Instead of deleting set the flag.
  *
  * @param array $data
  * @param array $config
  */
 public function delete($data, $config)
 {
     $old = $data;
     PropertyPath::set($this->path, 1, $data);
     parent::update($data, $old, $config);
 }
예제 #4
0
 public function add($data, $config)
 {
     $key = PropertyPath::get($config['key'], $data);
     if ($key === null) {
         $this->items[] = $data;
         $keys = array_keys($this->items);
         $key = array_pop($keys);
         $key = PropertyPath::set($config['key'], $key, $data);
         return $data;
     }
     $this->getKey($data, $config);
     return $data;
 }