public function testGetData() { $user = $this->createUser(); $this->transformer->getMapsManager()->setMapDir(UserModel::class, __DIR__ . '/data'); $dto = $this->transformer->getData($user, 'dto'); self::assertCount(5, $dto); self::assertInstanceOf('DateTime', $dto['creAt']); self::assertEquals('*****@*****.**', $dto['email']); self::assertEquals('name', $dto['name']); self::assertEquals('login', $dto['login']); self::assertTrue($dto['active']); }
/** * @param mixed[] $value * @param PropRule $propRule * @param DataTransformer $transformer * @return array * * @throws ParseException */ public function toData(array $value, PropRule $propRule, DataTransformer $transformer) { $newValue = []; $type = $propRule->getKey('rel')['map']; foreach ($value as $model) { $newValue[] = $transformer->getData($model, $type); } return $newValue; }
public function testToData() { $user = new UserModel(); $user->setId(99); $user->setName('user'); $user->setLogin('user'); $user->setEmail('*****@*****.**'); $user->setActive(true); $user->setCreAt(new \DateTime()); $content = new ContentModel(); $content->setActive(true); $content->setTitle('title'); $content->setCreAt(new \DateTime()); $content->setCats([1, 2, 4]); $content->setAny('any'); $content->setFloat(12.345); $content->setCount(23); $content->setUser($user); $content->setPrevUsers([$user]); $content->setSomeRelIds([12, 31]); $similarContent = clone $content; $similarContent->setId('content1'); $similarContent2 = clone $content; $similarContent2->setId('content2'); $content->setSimilarContent([$similarContent, $similarContent2]); $this->transformer->getMapsManager()->setMapDir(ContentModel::class, __DIR__ . '/data'); $this->transformer->getMapsManager()->setMapDir(UserModel::class, __DIR__ . '/data'); $dto = $this->transformer->getData($content, 'content'); self::assertInstanceOf('DateTime', $dto['creAt']); self::assertEquals('title', $dto['title']); self::assertEquals([1, 2, 4], $dto['cats']); self::assertEquals('any', $dto['any']); self::assertEquals(12.345, $dto['float']); self::assertEquals(23, $dto['count']); self::assertEquals(99, $dto['user']); self::assertEquals([12, 31], $dto['someRelIds']); self::assertEquals(['content1', 'content2'], $dto['similarContent']); self::assertTrue($dto['active']); self::assertCount(1, $dto['prevUsers']); self::assertEquals($dto['prevUsers'][0]['id'], $user->getId()); self::assertEquals($dto['prevUsers'][0]['email'], $user->getEmail()); self::assertEquals($dto['prevUsers'][0]['name'], $user->getName()); self::assertEquals($dto['prevUsers'][0]['login'], $user->getLogin()); self::assertInstanceOf('DateTime', $dto['prevUsers'][0]['creAt']); }
use PTS\DataTransformer\Types\IntType; use PTS\DataTransformer\Types\RefModelsToArrayStringIdType; use PTS\DataTransformer\Types\RefModelsType; use PTS\DataTransformer\Types\RefModelType; use PTS\DataTransformer\Types\StringType; use Symfony\Component\Yaml\Parser; include_once __DIR__ . '/../../vendor/autoload.php'; include_once __DIR__ . '/../models/Post/Post.php'; include_once __DIR__ . '/../models/Tag/Tag.php'; $transformer = new DataTransformer(new TypeConverter(), new MapsManager(new Parser()), new ModelClosure()); $converter = $transformer->getConverter(); $converter->addType('proxy', new BaseType()); $converter->addType('int', new IntType()); $converter->addType('string', new StringType()); $converter->addType('array', new ArrayType()); $converter->addType('date', new DateType()); $converter->addType('float', new FloatType()); $converter->addType('bool', new BoolType()); $converter->addType('refModelsToArrayStringId', new RefModelsToArrayStringIdType()); $converter->addType('refModels', new RefModelsType()); $converter->addType('refModel', new RefModelType()); $transformer->getMapsManager()->setMapDir(Tag::class, __DIR__ . '/../models/Tag/'); $transformer->getMapsManager()->setMapDir(Post::class, __DIR__ . '/../models/Post/'); $tag1 = (new Tag())->setId(1)->setTitle('music'); $tag2 = (new Tag())->setId(2)->setTitle('js'); $post = (new Post())->setId(1)->setTitle('Title')->setTags([$tag1, $tag2]); $post2 = new Post(); $postDto = $transformer->getData($post, 'dto'); // model to data $transformer->fillModel($postDto, $post2, 'dto'); // data to model