/** * This method attempts to call the specified getter/setter. * * @access public * @param string $method the name of the method to be * called * @param array $args the arguments passed * @return mixed the result of the method */ public function __call($method, $args) { if (preg_match('/^get[_a-zA-Z0-9]+$/', $method)) { $properties = preg_split('/_+/', substr($method, 3)); $key = null; $value = $this->models; foreach ($properties as $property) { $key = $property; if (!property_exists($value, $key)) { return null; } $value = $value->{$key}; } $field = new MappingService\Data\Field(MappingService\Data\FormatType::canonical()); if (is_object($value)) { $properties = get_object_vars($value); foreach ($properties as $key => $value) { $field->putItem($key, $value); } } else { $field->putItem($key, $value); } return $field; } return null; }
/** * This method returns the field for the specified data format. * * @access public * @static * @param MappingService\Data\Field $field the data field to store the items * @param Core\Data\XML $translation the translation to be parsed */ public static function translate(MappingService\Data\Field $field, Core\Data\XML $translation) { $format = $field->getFormatType()->__name(); $nodes = $translation->xpath("./Field[@Format='{$format}']"); if (!empty($nodes)) { $children = $nodes[0]->children(); if (count($children) > 0) { foreach ($children as $child) { $name = $child->getName(); if ($name == 'Item') { $attributes = $child->attributes(); if (isset($attributes['Name'])) { $key = Core\Data\XML::valueOf($attributes['Name']); $value = Core\Data\XML::valueOf($child[0]); if (isset($attributes['Type'])) { $type = Core\Data\XML::valueOf($attributes['Type']); if (!(is_string($type) && preg_match('/^(bool(ean)?|int(eger)?|float|string)$/i', $type))) { $type = 'string'; } settype($value, $type); } else { settype($value, 'string'); } $field->putItem($key, $value); } } } } } }
/** * This method attempts to call a getter. * * @access protected * @param string $scope the scope of the getter * @param string $field the name of the field * @return \Unicity\MappingService\Data\Field the aggregated field data * @throws \Unicity\Throwable\Parse\Exception indicates that there is a parsing * problem */ protected function __getter($scope, $field) { $nodes = $this->resource->xpath("/translators/translator[@name='{$this->name}']/fields/field[@name='{$field}' and @scope='{$scope}']"); if (count($nodes) > 0) { $attributes = $nodes[0]->attributes(); $aggregated_field = new MappingService\Data\Field(MappingService\Data\FormatType::model()); $items = $nodes->children(); foreach ($items as $item) { $attributes = $item->attributes(); if (!isset($attributes['name'])) { throw new Throwable\Parse\Exception('Unable to parse ":scope" method in translator.', array(':scope' => $scope)); } $name = $this->__valueOf($attributes['name']); if (isset($attributes['location'])) { $segments = explode('.', $this->__valueOf($attributes['location'])); if (count($segments) > 0) { $property = $this->models; foreach ($segments as $segment) { $property = $property->{$segment}; } $aggregated_field->putItem($name, $property); } } } if (isset($attributes['translation'])) { $translation = $this->__valueOf($attributes['translation']); return $translation::factory($aggregated_field)->toCanonicalFormat(); } return $aggregated_field; } }