/** * Builds a link from the string and adds it to the builder * * String template: * [one] Target [as property] on field [=> alias] [, field [=> alias]] * * Example: * Tweets as tweets on twitter_username => username * * @param Atomik_Model_Builder $builder * @param string $string */ public static function addLinkFromString(Atomik_Model_Builder $builder, $string) { $regexp = '/^((?P<type>one)\s+|)(?P<target>.+)((\sas\s(?P<as>.+))|)\s+on\s+(?P<on>.+)$/U'; if (!preg_match($regexp, $string, $matches)) { require_once 'Atomik/Model/Builder/Exception.php'; throw new Atomik_Model_Builder_Exception('Link string is malformed: ' . $string); } $link = new Atomik_Model_Builder_Link(); $link->type = $matches['type'] == 'one' ? 'one' : 'many'; $link->target = trim($matches['target']); $link->name = $link->target; if (isset($matches['as']) && !empty($matches['as'])) { $link->name = trim($matches['as']); } $fields = explode(',', $matches['on']); foreach ($fields as $field) { if (!preg_match('/^(?P<name>.+)(\s+=\>\s(?P<alias>.+)|)$/U', trim($field), $fieldMatches)) { require_once 'Atomik/Model/Builder/Exception.php'; throw new Atomik_Model_Builder_Exception('Field definition in link string is malformed: ' . $string); } $alias = isset($fieldMatches['alias']) ? $fieldMatches['alias'] : $fieldMatches['name']; $link->fields[$alias] = $fieldMatches['name']; } $builder->addLink($link); }