Example #1
0
    /**
     * Export an object recursivly, using associated metadata
     *
     * @param ClassMetadata $metadata
     * @param stdClass $object
     * @return array object
     */
    public function exportObject(ClassMetadata $metadata, $object)
    {
        $fields = $metadata->getFields();

        $properties = array();

        foreach ($fields as $property => $field) {
            $key = $field->getIndexName();
            $getter = 'get' . self::camelize($property);
            $value = $object->$getter();
            if ($value && 'date' === $field->getType()) {
                $value = $value->format($field->getFormat());
            }
            $properties[$key] = $value;
        }

        foreach ($metadata->getEmbeds() as $property => $embed) {
            $getter = 'get' . self::camelize($property);
            $value = $object->$getter();
            if (false === empty($value)) {
                $properties[$property] = $this->exportObject($metadata->getEmbeddedMetadata($property), $value);
            }
        }

        return $properties;
    }
Example #2
0
    /**
     * Extract and map properties for the metadata
     *
     * @param ClassMetadata $metadata
     */
    protected function exportProperties(ClassMetadata $metadata)
    {
        $map = array();

        foreach ($metadata->getFields() as $name => $field) {
            $ref = new \ReflectionObject($field);
            $class = $ref->getShortName();
            $method = sprintf('exportField%s', $class);
            $map[$name] = $this->$method($field);
        }

        foreach ($metadata->getEmbeds() as $name => $embed) {
            $embedMetadata = $metadata->getEmbeddedMetadata($name);
            $map[$name]['type'] = 'object';
            $map[$name]['properties'] = $this->exportProperties($embedMetadata);
        }

        return $map;
    }