Esempio n. 1
0
 /**
  * This method processes the XML document using the "php-filter" processing instruction.
  *
  * @access public
  * @static
  * @param \Unicity\Core\Data\XML $xml                       the XML document to be processed
  */
 public static function process(Core\Data\XML $xml)
 {
     $directives = $xml->getProcessingInstruction('php-filter');
     if (isset($directives['invoke'])) {
         $filters = array_map('trim', preg_split('/,/', $directives['invoke']));
         foreach ($filters as $filter) {
             $object = new $filter($xml);
             $object->invoke();
         }
     }
 }
Esempio n. 2
0
 /**
  * This method attempts to call a setter.
  *
  * @access protected
  * @param string $scope                                     the scope of the setter
  * @param string $field                                     the name of the field
  * @param array $args                                       the arguments to be passed
  * @return \Unicity\MappingService\Data\Field               the aggregated field data
  * @throws \Unicity\Throwable\Parse\Exception               indicates that there is a parsing
  *                                                          problem
  */
 protected function __setter($scope, $field, $args)
 {
     $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 = $args[0];
         if (isset($attributes['translation'])) {
             $translation = $this->__valueOf($attributes['translation']);
             $aggregated_field = $translation::factory($aggregated_field)->toModelFormat();
         }
         $items = $nodes[0]->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};
                     }
                     $property = $aggregated_field->getValue($name);
                 }
             }
         }
     }
     return null;
 }
Esempio n. 3
0
 /**
  * This method renders the data for the writer.
  *
  * @access public
  * @return string                                           the data as string
  * @throws \Exception                                       indicates a problem occurred
  *                                                          when generating the template
  */
 public function render()
 {
     $metadata = $this->metadata;
     $declaration = $metadata['declaration'] ? Core\Data\XML::declaration($metadata['encoding'][1], $metadata['standalone']) . $metadata['eol'] : '';
     if (!empty($metadata['template'])) {
         $file = new IO\File($metadata['template']);
         $mustache = new \Mustache_Engine(array('loader' => new \Mustache_Loader_FilesystemLoader($file->getFilePath()), 'escape' => function ($string) use($metadata) {
             $string = Core\Data\Charset::encode($string, $metadata['encoding'][0], $metadata['encoding'][1]);
             $string = Core\Data\XML::entities($string);
             return $string;
         }));
         ob_start();
         try {
             echo $declaration;
             echo $mustache->render($file->getFileName(), $this->data);
         } catch (\Exception $ex) {
             ob_end_clean();
             throw $ex;
         }
         $template = ob_get_clean();
         if (!empty($metadata['minify'])) {
             $template = Minify\XML::minify($template, $metadata['minify']);
         }
         return $template;
     } else {
         ob_start();
         try {
             $document = new \DOMDocument();
             $document->formatOutput = true;
             $this->toXML($document, $document, $this->data);
             echo $declaration;
             echo $document->saveXML();
         } catch (\Exception $ex) {
             ob_end_clean();
             throw $ex;
         }
         $template = ob_get_clean();
         return $template;
     }
 }
Esempio n. 4
0
 /**
  * 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);
                     }
                 }
             }
         }
     }
 }
Esempio n. 5
0
 /**
  * This method returns the processed resource as a collection.
  *
  * @access public
  * @param string $path                                      the path to the value to be returned
  * @return mixed                                            the resource as a collection
  */
 public function read($path = null)
 {
     $buffer = file_get_contents($this->file);
     if ($this->metadata['bom']) {
         $buffer = preg_replace('/^' . pack('H*', 'EFBBBF') . '/', '', $buffer);
     }
     if (!preg_match('/^<\\?xml\\s+.+\\?>/', $buffer)) {
         $buffer = Core\Data\XML::declaration(Core\Data\Charset::UTF_8_ENCODING) . "\n" . $buffer;
     }
     $document = new \DOMDocument();
     $document->substituteEntities = false;
     $document->loadXML($buffer);
     $collection = $this->parseWDDXPacketElement($document);
     if ($path !== null) {
         $collection = Config\Helper::factory($collection)->getValue($path);
     }
     return $collection;
 }
Esempio n. 6
0
 /**
  * This method returns the processed resource as a collection.
  *
  * @access public
  * @param string $path                                      the path to the value to be returned
  * @return mixed                                            the resource as a collection
  */
 public function read($path = null)
 {
     $xml = Core\Data\XML::load($this->file);
     $directives = $xml->getProcessingInstruction('php-marshal');
     if (isset($directives['expandableProperties'])) {
         $this->directives->putEntry('expandableProperties', new Common\HashSet(preg_split('/\\s+/', $directives['expandableProperties'])));
     }
     $collection = $this->parseRootElement($xml);
     if ($path !== null) {
         $collection = Config\Helper::factory($collection)->getValue($path);
     }
     return $collection;
 }