Esempio n. 1
0
 public function test_parser_warning_unmatched_brackets()
 {
     $this->setExpectedException('PHPUnit_Framework_Error_Notice', 'Unmatched brackets, missing a "]" in path after "element"');
     $this->assertSame(PropertyPath::parse('[element'), array(array(PropertyPath::TYPE_ANY, '[element')));
 }
Esempio n. 2
0
 /**
  * Convert raw backend data into an object instance.
  *
  * @param mixed       $data
  * @param ModelConfig $config
  * @param string|null $index  (optional) speedoptim: Prevents resolving the index again.
  * @param bool        $reload true: Overwrite properties in the instance.
  *
  * @return stdClass
  */
 protected function convertToInstance($data, $config, $index = null, $reload = false)
 {
     if ($index === null) {
         $index = $this->resolveIndex($data, $config);
     } elseif (empty($this->objects[$config->name][$index])) {
         throw new Exception('Invalid index: "' . $index . '" for ' . $config->name);
     }
     if ($reload) {
         $instance = $this->objects[$config->name][$index]['instance'];
         if ($instance === null) {
             throw new Exception('No instance loaded');
         }
     } elseif ($this->objects[$config->name][$index]['instance'] !== null) {
         throw new Exception('Instance already loaded, use reload parameter to reload');
     } else {
         // new instance
         $class = $config->class;
         $instance = new $class();
     }
     // Validate the properties in the class.
     if ($this->validated[$config->name] === false) {
         // No validated?
         $properties = get_object_vars($instance);
         $paths = array_merge($config->properties, $config->ignoreProperties, array_keys($config->belongsTo), array_keys($config->hasMany));
         foreach ($paths as $path) {
             $tokens = PropertyPath::parse($path);
             if (in_array($tokens[0][0], array(PropertyPath::TYPE_ANY, PropertyPath::TYPE_ELEMENT))) {
                 unset($properties[$tokens[0][1]]);
             }
         }
         if (count($properties) !== 0) {
             $causes = array('1. The column is missing in the backend/database.', '2. The relation/foreign key is missing in the backend/database.', '3. The column has diffent name than the property. Set the ModelConfig->properties[columname] = propertyname.', '4. The property should be ignored by the repository. Add the property to the ModelConfig->ignoreProperties.', '5. The relation couldn\'t be detected. Add an entry to ModelConfig->hasMany or ModelConfig->belongsTo.');
             throw new InfoException('Unexpected property: ' . \Sledgehammer\quoted_human_implode(' and ', array_keys($properties)) . ' in ' . $config->class . ' class for "' . $config->name . '"', '<b>Possible causes:</b><br />' . implode('<br />', $causes));
         }
         $this->validated[$config->name] = true;
     }
     // Map the data onto the instance
     foreach ($config->properties as $sourcePath => $targetPath) {
         $value = PropertyPath::get($sourcePath, $data);
         if (isset($config->readFilters[$sourcePath])) {
             $value = \Sledgehammer\filter($value, $config->readFilters[$sourcePath]);
         }
         PropertyPath::set($targetPath, $value, $instance);
     }
     foreach ($config->belongsTo as $property => $relation) {
         if (isset($relation['convert'])) {
             $value = $this->convert($relation['model'], PropertyPath::get($relation['convert'], $data));
             PropertyPath::set($property, $value, $instance);
         } else {
             $belongsToId = $data[$relation['reference']];
             if ($belongsToId === null) {
                 PropertyPath::set($property, null, $instance);
             } else {
                 if (empty($relation['model'])) {
                     // No model given?
                     throw new Exception('Invalid config: ' . $config->name . '->belongsTo[' . $property . '][model] not set');
                 }
                 if ($relation['useIndex']) {
                     $belongsToIndex = $this->resolveIndex($belongsToId);
                     $belongsToInstance = @$this->objects[$relation['model']][$belongsToIndex]['instance'];
                 } else {
                     $belongsToInstance = null;
                 }
                 if ($belongsToInstance !== null) {
                     $instance->{$property} = $belongsToInstance;
                 } else {
                     $fields = array($relation['id'] => $belongsToId);
                     $instance->{$property} = new BelongsToPlaceholder($this->ref() . '/' . $config->name . '/' . $property, $instance, $fields);
                 }
             }
         }
     }
     foreach ($config->hasMany as $property => $relation) {
         if (isset($relation['convert'])) {
             $collection = new RepositoryCollection(PropertyPath::get($relation['convert'], $data), $relation['model'], $this->ref());
             PropertyPath::set($property, $collection, $instance);
         } else {
             $instance->{$property} = new HasManyPlaceholder($this->ref() . '/' . $config->name . '/' . $property, $instance);
         }
     }
     $this->_triggerEvent($instance, 'load', $instance, ['repository' => $this->ref(), 'model' => $config->name], $this);
     return $instance;
 }
Esempio n. 3
0
 /**
  * Convert a path to a (escaped) columnname.
  *
  * @param string $path
  */
 private function convertPathToColumn($path)
 {
     $compiled = PropertyPath::parse($path);
     if (count($compiled) > 1) {
         return false;
     }
     if (in_array($compiled[0][0], array(PropertyPath::TYPE_ANY, PropertyPath::TYPE_ELEMENT))) {
         $db = Connection::instance($this->dbLink);
         return $db->quoteIdentifier($compiled[0][1]);
     }
     return false;
 }