/** * @param DocumentManager $objectManager * @param DocumentLoader $loader * @param string $class * @return DocumentRepository */ private function createRepository(DocumentManager $objectManager, DocumentLoader $loader, $class) { /* @var $metadata ClassMetadata */ $metadata = $objectManager->getMetadataFor($class); $repositoryClassName = $metadata->repository ?: DocumentRepository::class; return new $repositoryClassName($objectManager, $metadata, $loader); }
/** * @param string $class * @param array $data * @return object * @throws \Exception */ private function hydrate($class, $data) { $object = $this->instantiator->instantiate($class); $metadata = $this->manager->getMetadataFor($class); /** @var PropertyMetadata $property */ foreach ($metadata->propertyMetadata as $property) { $value = isset($data[$property->name]) ? $data[$property->name] : null; if ($property->type) { $type = $this->manager->getTypeRegisty()->get($property->type); $property->setValue($object, $type->transformToPhp($value)); } if (!$value) { continue; } if ($property->reference === PropertyMetadata::REFERENCE_ONE) { $property->setValue($object, $this->manager->find($property->target, $value)); } if ($property->reference === PropertyMetadata::REFERENCE_MANY) { $result = []; foreach ($value as $k => $v) { $result[] = $this->manager->find($property->target, $v); } $property->setValue($object, $result); } if ($property->reference === PropertyMetadata::REFERENCE_KEY) { $result = new ObjectCollection(); $type = $this->manager->getTypeRegisty()->get($property->value['name']); foreach ($value as $k => $v) { $result[$this->manager->find($property->target, $k)] = $type->transformToPhp($v); } $property->setValue($object, $result); } if ($property->embed === PropertyMetadata::EMBED_ONE) { if ($property->mapping) { $value = $this->mapping($value, $property->mapping); } $property->setValue($object, $this->hydrate($property->target, $value)); } if ($property->embed === PropertyMetadata::EMBED_MANY) { $result = []; foreach ($value as $k => $v) { if ($property->mapping) { $v = $this->mapping($v, $property->mapping); } $result[] = $this->hydrate($property->target, $v); } $property->setValue($object, $result); } } $event = new DocumentEvent($this->manager, $metadata, $object); $this->eventDispatcher->dispatch(Events::POST_LOAD, $event); return $object; }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use DavidBadura\OrangeDb\Adapter\YamlAdapter; use DavidBadura\OrangeDb\Annotation as DB; use DavidBadura\OrangeDb\DocumentManager; class UserRepository extends \DavidBadura\OrangeDb\Repository\DocumentRepository { } $manager = new DocumentManager(new YamlAdapter(__DIR__ . '/data')); $repo = $manager->getRepository(User::class); $user = $repo->find('john'); foreach ($repo->findAll() as $user) { dump($user); } $users = $repo->createTraversable()->where(function (User $user) { return $user->getAge() == 0; })->getIterator(); foreach ($users as $user) { dump($user); } /** * @DB\Document(repository="UserRepository") */ class User { /** * @var string * * @DB\Id * @DB\Type(name="string")
<?php require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/Material.php'; require_once __DIR__ . '/Building.php'; use DavidBadura\OrangeDb\Adapter\YamlAdapter; use DavidBadura\OrangeDb\DocumentManager; use Model\Building; $manager = new DocumentManager(new YamlAdapter(__DIR__ . '/data')); /** @var Building $house */ $house = $manager->getRepository(Building::class)->find('house'); dump($house); foreach ($house->getConstructionCosts() as $material => $value) { dump($material); dump($value); }