Пример #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;
    }
Пример #2
0
    /**
     * @test
     * @cover ClassMetadata::addField
     */
    public function testAddField()
    {
        $meta = new ClassMetadata('foo');

        $field = $this->getMockBuilder('Ariadne\Mapping\Element\Field')->disableOriginalConstructor()->disableOriginalClone()->getMock();

        $meta->addField('foo', $field);

        $this->assertEquals(array('foo' => $field), $meta->getFields(), 'field is properly added to the associated member');
    }
Пример #3
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;
    }
Пример #4
0
    /**
     * Export an object recursivly, using associated metadata
     *
     * @param ClassMetadata $metadata
     * @param stdClass $object
     * @return array object
     */
    protected function exportObject(ClassMetadata $metadata, $object)
    {
        $document = new Document();

        $idGetter = 'get' . self::camelize($metadata->getIndex()->getIdProperty());

        $document->addField(Field::unIndexed('_id', $object->$idGetter()));

        foreach ($metadata->getFields() as $property => $field) {

            $getter = 'get' . self::camelize($property);

            $value = $object->$getter();

            if ($value && 'date' === $field->getType()) {
                $value = $value->format($field->getFormat());
            }

            $type = $field->getType();

            $encoding = mb_detect_encoding($value);

            $isStored = $field->getStore() === 'yes';

            $isIndexed = $field->getIndex() != "no";

            $isTokenized = $field->getIndex() == "analyzed";

            $field = new Field($field->getIndexName(), $value, $encoding, $isStored, $isIndexed, $isTokenized);

            $document->addField($field);

            $doc[$property] = $value;
        }

        $document->addField(Field::unIndexed('_doc', json_encode($doc)));

        return $document;
    }