JMSSerializerBundleã¯ããããããçŸæç¹ã§ã®ã·ãªã¢ã«åã®ãã¬ã³ãã»ãã¿ãŒã§ãïŒFOSRestBundleã§ã宣äŒãããŠããŸãïŒã å€æ©èœã§ãããããŸããŸãªåœ¢åŒã§ã·ãªã¢ã«åã«ãŒã«ãä¿åããããŸããŸãªåœ¢åŒã§ããŒã¿ãã·ãªã¢ã«åããã³éã·ãªã¢ã«åãããã£ãã·ã¥ã䜿çšã§ããŸãã ãããã圌ã«ã¯ããã€ãã®å°ããªæªè§£æ±ºã®åé¡ãããããããã¯äœåºŠã觊ããããŠããããããã®è§£æ±ºçã¯æåŸ ãããŠããŸããã å€æ©èœæ§ãè¿œæ±ããäžã§ãã¢ããªã±ãŒã·ã§ã³ã¢ãŒããã¯ãã£ãåæ¢ããããã«æããŸãã
æåã®åé¡ã¯ãããããå€ãã®äŸåé¢ä¿ãåŒãåºããã³ãã«ã§ããããã¯Symfony 2ã®å€éšã§ã¯äœ¿çšã§ããŸãããããã¯éåžžã«å¥åŠã§ãããªããªãã質åã¯ããšããšã·ãªã¢ã«åã«é¢ãããã®ã§ãããã©ã€ãã©ãªã«ããªãçç±ã¯æããã§ã¯ãªãããã§ãã
2çªç®ã®åé¡ã¯ãnullå€ãã·ãªã¢ã«åã§ããªãããšã§ãã REST APIã«é¢é£ããŠ-ããã¯åãå ¥ããããŸããã éåžžã«å€§ããªåé¡ããããŸããã解決çã¯ãªããšç¢ºä¿¡ããŠããŸãã å®éãç¶æ³ã¯éåžžã«å¥åŠã§ãã json_decodeãšjson_encodeã䜿çšãããšããã®ã¿ã¹ã¯ãæ£ããåŠçããŸãã xml圢åŒã«ã€ããŠã¯ã1幎åã«doctrine-oxmãå®è£ ãããšãã«ãã®åé¡ã解決ããŸãã ã
3çªç®ã®åé¡ã¯ãæ¢åã®ããŒã¿ãªããžã§ã¯ãã«éã·ãªã¢ã«åã§ããªãããšã§ãã ããã«ãããéšåçãªããŒã¿æŽæ°ãå®è¡ããPOST / PATCHèŠæ±ã䜿çšã§ããªããªããŸãã äž»ãªè³ªåã¯ãSymfony 2ã§ã®RESTã®ç°¡åãªå®è£ ã«ã€ããŠãFOSRestBundleãããã«äººã ãã ãŸããŠãããã§ãã
é·ãéã誰ãSymfony2ã§RESTã®æ¬æ Œçãªå®è£ ãè¡ã£ãããšããªãããããã®åé¡ã«ééããŠããªãã£ããšã¯ä¿¡ããããŸããã§ããã ç§ãã¡ã¯å€ãã®æ±ºå®ã«ç®ãéããäžæ»ã®ãã§ãæã£ãããã«ãããã¹ãŠãæ£ãããªãããã¹ãŠãééã£ãŠããããšæããŸããã ããæŽããæ¥ã Benjamin Eberleiã«æºè¶³ããŸãããBenjaminEberlei㯠ãç¬èªã®ãã³ãã«SimpleThingsFormSerializerBundleãäœæããããšã§3çªç®ã®åé¡ã«æ³šç®ããŸããã ããããæ®å¿µãªãããä»ã®Symfony2ã³ãã¥ããã£ãšåæ§ã«ãããŒãžã§ã³2.1ã«ã¯ãåãã€ããããããŒã¿çããããããŸããã§ããã ããããããã¯å¥ã®è©±ã§ããã幞ããªããšã«ã2.0ãšäºææ§ã®ãã人ãããŸããã
ã ããã幞çŠã¯ãã§ã«è¿ãããã§ãã¢ãã«ãæŽæ°ããããšãã§ããŸãã ã©ããªã«å°é£ã§ãã£ãŠããDTOïŒããŒã¿è»¢éãªããžã§ã¯ãïŒãªããžã§ã¯ãã®FormTypeãäœæããJMSSerializerBundleãSimpleThingsFormSerializerBundleã«å®å šã«çœ®ãæããŸãã 圌ãã¯ãããããŸãããã圌ãã¯å¹žããèŠã€ããŸããã§ããã çµå±ã®ãšãããæ°ãããã³ãã«ã¯ãã¹ãŠã®æ å ±ãæååã«å€æããŸãã ã¯ã©ã€ã¢ã³ãã¯æ°å€ãããŒã«å€ã決ããŠèŠãããšã¯ãããŸããã ãªããããè¡ãããã®ããšãã質åã«å¯Ÿããåçã¯åŸãããŸããã§ãããJMSSerializerã䜿çšããŠãéã·ãªã¢ã«åããããã®ã·ãªã¢ã«å-FormSerializerã®ææ¡ãããããŸããã§ããã ããããããã§äœããééã£ãŠããããã«æããŸãã ããã«ãããŒãžã§ã³2.0ã®symfonyãã©ãŒã ã¯ãGETãŸãã¯POSTãªã¯ãšã¹ãã§ã®ã¿ãã€ã³ããçæã§ããæ®ãã¯ç¡èŠããŸãã ã¯ããRESTã®ãã©ãŒã ã®äœ¿çšã«é¢ããŠå€ãã® "fi"ããããŸãããããã¯èšäºã®ç¯å²å€ã§ãã äœããå€ããããšãæåŸ ããŠã2é±éäŒæããšããŸããã ããã...
ã¿ã¹ã¯ã¯åçŽã«æããŸãããREST APIã§ã¯ãjson圢åŒã§æ å ±ãæäŸããããšãä¿èšŒãããŠããŸãã ä»ã®åœ¢åŒã¯ç§ãã¡ã®èå³ãåŒããã®ã§ã¯ãããŸããïŒç§ã«ã¯ãææ°ã®ãããžã§ã¯ãã®ããã«ïŒã å±æ§ã®ã¡ãœãããåžžã«èšå®/ååŸããDTOãªããžã§ã¯ãããããšä»®å®ããŸãã ãããã®DTOãjsonã«å€æããããéã«å€æãããããŸãïŒäžèšã®åé¡ã®è§£æ±ºæ¹æ³ã䜿çšïŒã ã·ãªã¢ã«åã«ãŒã«ãyml圢åŒã§ä¿åããJMSSerializerBundleã®ããŸããŸãªèšå®ããã¹ãŠä¿åããæ©èœãæ¬åœã«æ°ã«å ¥ã£ãŠããŸãããã£ãŒã«ãã®ãããã³ã°ïŒãserialized_nameããšãtypeãïŒãšãã©ã°ãexposeãã®ã¿ãæå®ããå¿ èŠããããŸãã
ã©ã€ãã©ãªã®å®è£ ãé²ããåã«ãçŸåšã®ãœãªã¥ãŒã·ã§ã³ãããäžåºŠæ€çŽ¢ããŸããã 2ã€ã®èå³æ·±ããããžã§ã¯ããèŠã€ãããŸããã
FbsSerializer-ã©ããã ãJMSSerializerBundleã®ãå身ãã§ãã å®è£ ã®å¯èœæ§ãšã¢ã€ãã¢ã¯éåžžã«äŒŒãŠãããåãåé¡ãååšããã³ã¬ã¯ã·ã§ã³ãäžé©åã«ã·ãªã¢ã«åããŸãã
ObjectSerializerã¯éåžžã«åçŽãªå®è£ ã§ãããã©ã€ãã©ãªãèŠãæ¹æ³ãšã»ãŒäžèŽããŸããã äž»ãªæ¬ ç¹ã¯ãã¯ã©ã¹ãããã³ã°ãã³ã³ã¹ãã©ã¯ã¿ãŒãä»ããŠæž¡ãããããšã§ããããã¯åãå ¥ããããŸããã
æè¿ãå®è£ ãç°¡åã«ãªããŸããã ã³ãŒããåçŽã§ããã»ã©ã責任ãå°ãªããªããŸã-ããè¯ãã 誰ãå¿ èŠãšããªãæ©èœãå«ãã³ãŒããèšè¿°ãããã¯ãããŸãããã¢ããªã±ãŒã·ã§ã³ã¢ãŒããã¯ãã£ãæ éããŸãã
<?php class Serializer { public function serialize($object) { $array = $this->arrayAdapter->toArray($object); return $this->adapter->serialize($array); } public function unserialize($data, $object) { $array = $this->serializerAdapter->unserialize($data); return $this->arrayAdapter->toObject($array, $object); } } ?>
åºæ¬çãªèãæ¹ã¯ãããŸããŸãªã·ãªã¢ã«åã«ãŒã«ãæã€è€éãªãªããžã§ã¯ããé åã«å€æããããšã§ãã ããã¯ArrayAdapterã«ãã£ãŠè¡ãããããŒã¿å€æã®ããžãã¯å šäœãã«ãã»ã«åããŸãã çæãããé åã¯ç°¡åã«json圢åŒã«å€æãããŸããxmlã«åé¡ã¯ãªããšæããŸãã éã·ãªã¢ã«åã®éãéã®ããšãåœãŠã¯ãŸããŸããå ¥åããŒã¿ãé åã«å€æããArrayAdapterãç¬èªã®ã«ãŒã«ã«åŸã£ãŠããŒã¿ããªããžã§ã¯ãã«å€æããŸãã
ãã®å ŽåãserializerAdapterã¯ãjson_encodeãjson_decodeé¢æ°ã®åçŽãªã©ãããŒãè¡šããŸãã èå³ã®ããããžãã¯ã¯ãã¹ãŠArrayAdapterã«ãããŸãã ããã§ãæ§æããŒã¿ã®åŠçãšããã®ã©ã€ãã©ãªãšFbsSerializerãJMSSerializerBundleã®ã·ãªã¢ã«åã®ã€ããªãã®ãŒã®éããèŠã€ããŸãã
ç§ã®èŠè§£ã§ã¯ãããã€ãã®ã¹ããŒã¹ã®ããã€ãã®ãã¡ã€ã«ã«ãããªããžã§ã¯ããã·ãªã¢ã«åããããã®äžé£ã®ã«ãŒã«ããããŸãã ãŸãããã®ã¹ããŒã¹ããæ§æãååŸããå¿ èŠããããŸãã ããããæ§æãä¿åãããå€ãã®åœ¢åŒãååšããå¯èœæ§ãããã䜿ããããã®ããã«ããããªããžã§ã¯ãè¡šçŸã«å€æããããšã¯è«ççã§ãã Johannes Schmittã¯ã ã¡ã¿ããŒã¿ã©ã€ãã©ãªã䜿çšããŠãããè¡ããŸãã ããã¯éåžžã«åªããŠããŸããããªããžã§ã¯ãã®åå°ã«å¯æ¥ã«é¢é£ããŠããŸãã ãããã£ãŠãåŸã§ãã·ãªã¢ã«åäžã«ããã¹ãŠã®äœæ¥ã¯ã·ãªã¢ã«åå¯èœãªãªããžã§ã¯ããšãã®Reflectionããè¡ãããŸãã ç°¡åã«èšããšããªããžã§ã¯ãã§å¯èœãªãã¹ãŠã®é ç®ãå埩åŠçãããã®å±æ§ãŸãã¯ã¡ãœããã®èšå®ã確èªããŸãã ãã®ã¢ãããŒãã¯ãå€ã®è€éãªåŠçïŒããç°¡åã«ã§ããïŒãšçµã¿åãããããšã§ãæªè§£æ±ºã®åé¡ã®åå ã®1ã€ã§ãããšèããŠããŸãã ãããŠãç§ã®å®è£ ã§ã¯ãå察ã®ããšãè¡ããŸãã æ§æããã·ãªã¢ã«åå¯èœãªãªããžã§ã¯ãã«ã€ããŠäœãç¥ããªããªããžã§ã¯ãè¡šçŸã«å€æããã ãã§ãã 次ã«ãã¡ã¿ããŒã¿ãå埩åŠçããçŸåšã®å±æ§ããã®ã¿ã€ããããã³ãã®åŠçæ¹æ³ãã·ãªã¢ã«åã§ããååã§ã·ãªã¢ã«åã§ãããã©ããã確èªããŸãã ããŒã¿åŠçã®éçšã§ãå¿ èŠãªã²ãã¿ãŒ/ã»ãã¿ãŒãåŒã³åºããŸãã ãããã£ãŠããªããžã§ã¯ãããã§ã¯ãªããæå®ãããæ§æããé²ã¿ãŸãã 圌ããèšãããã«ã圌ãã¯å°ãã-圌ãã¯ãããåŸãã 以äžã¯ã³ãŒãã®ã¹ããããã§ãã
<?php class ArrayAdapter { public function toArray($object) { $result = array(); $className = $this->getFullClassName($object); $metadata = $this->metadataFactory->getMetadataForClass($className); foreach ($metadata->getProperties() as $property) { //handle value $result[$property->getSerializedName()] = $value; } return $result; } public function toObject(array $data, $object) { $className = $this->getFullClassName($object); $metadata = $this->metadataFactory->getMetadataForClass($className); foreach ($metadata->getProperties() as $property) { //handle value and set it to object } return $object; } } ?>
ã·ãªã¢ã«å/éã·ãªã¢ã«åã®å€ãåŠçããããã«ãçŸåšããã©ã€ããŒãhandleValueã¡ãœããïŒçŽ60è¡ïŒã䜿çšããŠããŸãã
æåã¯ãèšå®ãymlãã¡ã€ã«ãããªããžã§ã¯ãã«å€æããããã«ãã¿ã¹ã¯ãéåžžã«åçŽãªã®ã§ãèªåã§äœããæžããããšæããŸããã æ§æãã¡ã€ã«ã®å ŽæïŒãã¡ã€ã«ã·ã¹ãã ãã¡ã¢ãªãããŒã¿ããŒã¹ãªã©ãã©ãã«ã§ãä¿åã§ããŸãïŒãšãã®åœ¢åŒïŒymlãjsonãiniãxmlïŒã®2ã€ããæœè±¡åããå¿ èŠããããŸãã åæã«ãããŒã ãªãŒããŒããæ¯å* .ymlãã¡ã€ã«ã解æããããã§ã¯ãªãããšè¿°ã¹ãããã«ããã£ãã·ã¥ãå¿ èŠã§ãã ãã®ã€ã³ã¿ãŒãã§ãŒã¹ã¯ãputãgetãremoveã®ãã¹ãŠã«ç¥ãããŠããŸãã ããããç§ã¯æéå ã«åæ¢ããã¡ã¿ããŒã¿ã©ã€ãã©ãªãæãåºããåã³äžèŠãªReflectionã«ééããŸããã ãããã£ãŠããã€ããŒãªä¿®æ£ãå ããŠãã³ãŒãã®ãã®éšåã¯ããããåŒçšããããšããã¹ã«æ¬æãè¡šããŠããŸãã
ãã®çµæãããŒã ã®ããã«33æé倱ãããŸãããã simple-serializerã©ã€ãã©ãªãšOpensoftSimpleSerializerBundleãã³ãã«ãäœæãããŸãã ã
ã©ã€ãã©ãªã®äŸåé¢ä¿ïŒ
symfony / yamlã³ã³ããŒãã³ã
èŠä»¶ïŒ
-çŽååå¯èœãªãªããžã§ã¯ãã®å ŽåãçŽååèŠåãèšè¿°ããå¿ èŠããããŸãã
-ã·ãªã¢ã«åå¯èœãªãªããžã§ã¯ãã«ã¯ãå±æ§ã®ã»ãã¿ãŒãšã²ãã¿ãŒãå¿ èŠã§ãã
JMSSerializerBundleãšæ¯èŒããå©ç¹ïŒ
-次ã®ãããªå€ãã®åé¡ã¯ãããŸãããnullå€ãæã€å±æ§ã®åŠçãæ¢åã®ãªããžã§ã¯ããžã®ããŒã¿ã®éã·ãªã¢ã«åã
-æ§æã§ã®æ¥ä»ã®æžåŒèšå®ã
-symfony 2ãã¬ãŒã ã¯ãŒã¯ãžã®ãã€ã³ãã£ã³ã°ã¯ãããŸããã
åœç¶ãããã¯çæ³çã§ã¯ãããŸããã JMSSerializerBundleã¯ããæ©èœçã§ãããå¶éãå°ãªããªã£ãŠããŸãã ããããç§ãã¡ã®å ŽåãREST APIã®å®è£ ã§ã®äœ¿çšã«ã¯æãããªãã€ã¢ã¹ããããŸãã 倧èŠæš¡ãªã¢ããªã±ãŒã·ã§ã³ã§RESTãæ±ãå Žåãã¢ãã«ãAPIã«æ瀺çã«åæ ãããããšã¯ãªãããã DTOãã¿ãŒã³ãå®è£ ããã«è¡ãããšã¯ã§ããŸããã ãŸããããã«å¿ããŠãDTOãDomainObjectã«å€æãããšãã«ãã¢ã»ã³ãã©ãŒã¯ã»ãã¿ãŒ/ã²ãã¿ãŒã¡ãœããã䜿çšããå¯èœæ§ãé«ããªããŸãã ãããŠã2çªç®ã®èŠä»¶ã¯èªåçã«æ¶ããŸãã æåã¯ããã¹ãŠãè¡ããšæããŸãã çµå±ã®ãšãããAPIã顧客ã«æäŸããããšã«ãããç§ãã¡ã¯ã©ã®ãããªåœ¢åŒã§ãç§ãã¡ãåãå ¥ãããäžããããäŒããŸãã ãããŠããããã®ã«ãŒã«ããªããã°ãããã¯éè«ççãªããã§ãã ããŸããŸãªåœ¢åŒã®å¿çã«ã€ããŠã¯ããããžã§ã¯ãã§jsonãæ±ã£ãŠããŸãã éåžžã«ã·ã³ãã«ã§äŸ¿å©ã§ãã ç§ã®æèŠã§ã¯ãXMLã¯RESTã§äœ¿çšããã«ã¯ãã§ã«å°ãæ代é ãã§ãã FOSRestBundleãæäŸããHTML圢åŒã¯ããŸãã«ãæã«è² ããªãã®ã§ããã®äœ¿çšã¯æ³åã§ããŸããã åäœãã¹ãã¯REST APIã®ä¿¡é Œæ§ã«è²¬ä»»ããããŸãïŒããã«ã¡ã¯ãKonstantinå¥åeverzetã«æè¬ããŸã ïŒã ã³ãŒãã»ãã·ã§ã³ã¯æ¬åœã«ã¯ãŒã«ãªã®ã§ã davertãæ°åã害ããªãããšãé¡ã£ãŠããŸãã
ã·ãªã¢ã«åãæ§æããå¯èœæ§ã«ã€ããŠåå¥ã«è©±ã䟡å€ããããŸãã ãªãã·ã§ã³ã¯æå°éã«æããããŠããŸãããè£å®ããããšã¯ãŸã£ããé£ãããããŸããã æ§æãã¡ã€ã«ã¯éåžžãJMSSerializerBundleã«äŒŒãŠããŸãã
MyBundle\Model\Order properties: id: expose: true serialized_name: id type: integer
ãexposeããªãã·ã§ã³ã®ããã©ã«ãã¯ãfalseãã§ãããããã£ãŠãã·ãªã¢ã«åãããã¹ãŠã®å±æ§ã«ã€ããŠãæå®ããå¿ èŠããããŸãã ããã¯ç§ã®å人çãªå¥œã¿ã§ãããèš±å¯ãããŠããªããã®ã¯çŠæ¢ãããŠããŸãã ãããããè¥è ã®ACLã®åœ±é¿ã§ãããã ååãšããŠãå¿ èŠã«å¿ããŠæ¬æ Œçãªé€å€/å ¬éãå®è£ ã§ããŸãã Serialized_nameã¯ãªãã·ã§ã³ã®ãã©ã¡ãŒã¿ãŒã§ãããæå®ãããŠããªãå Žåãå±æ§ã¯ååã«åŸã£ãŠã·ãªã¢ã«åãããŸãã æåŸã®ãã¿ã€ãããªãã·ã§ã³ããªãã·ã§ã³ã§ãã æå®ãããŠããªãå Žåãå±æ§å€ãæã€ã¢ã¯ã·ã§ã³ã¯å®è¡ãããŸããã ãã以å€ã®å Žåã¯ãæå®ãããåã«ãã£ã¹ããããŸãã å¯èœãªã¿ã€ãïŒæŽæ°ãããŒã«ãããã«ãæååãé åãTãé å<T>ãDateTimeãDateTime <format>ã
JMSSerializerBundleãšã®äž»ãªéãã¯ãArrayCollectionãååšããªãããšãšãDateTime <format>ãååšããããšã§ãã åè ã¯åºæ¬çã«ååšããŸããããªããªãããã®ååšã¯Doctrine / Ormãžã®ã©ã€ãã©ãªäŸåãè¿œå ããããã§ãã symfony 2ãã¬ãŒã ã¯ãŒã¯ã䜿çšããå Žåã90ïŒ ã§æºè¶³ããŸãã ããããä»ã®10ããŒã»ã³ãããŸãã¯symfonyã䜿çšããªã人ã¯ã©ãã§ããããïŒ ããã«ãDTOãžã®é©çšã«ã€ããŠè©±ããŠããå Žåãã»ãšãã©ç¢ºå®ã«ã³ã¬ã¯ã·ã§ã³ã¯ãããŸãããæå³ããããŸããã ãããããã®åé¡ã¯å°æ¥è§£æ±ºãããå¯èœæ§ããããŸãã 2çªç®ã®æ©èœã¯ãæ¥ä»ã®ãã©ãŒãããæ¹æ³ãæå®ããæ©èœã§ãã DateTimeãšã³ããªã¯ãDateTimeãªããžã§ã¯ãããããšæ³å®ããŠããããšãæãåºããŠãã ããã ééã£ãŠããªããã°ãJMSSerializerBundleã§ã«ã¹ã¿ã æ¥ä»ãã³ãã©ãŒãäœæãããã®åœ¢åŒãæå®ã§ããŸãã ããã§ã¯ãã¹ãŠãããç°¡åã§ããæ§æã§çŽæ¥ãDateTimeã¯ã©ã¹ã®å®æ°ïŒããšãã°ããISO8601ãïŒããŸãã¯å¿ èŠãªæå»åœ¢åŒã®æååãæå®ã§ããŸãã
TODOãªã¹ããäœæã§ããŸãããæ£çŽãªãšãããç§ã¯ãããç·æ¥ã®å¿ èŠæ§ãšã¯æããŸããã
1ïŒã·ãªã¢ã«åæ§æãä»ã®åœ¢åŒïŒæ³šéãxmlïŒã§ä¿åããæ©èœãè¿œå ããŸãã
2ïŒJSON以å€ã®åœ¢åŒã«ããŒã¿ãã·ãªã¢ã«åããæ©èœãè¿œå ããŸãã
3ïŒæ§æã«ããŸããŸãªãªãã·ã§ã³ãè¿œå ããŸãã
4ïŒhandleValueã®ã³ãŒãããªãã¡ã¯ã¿ãªã³ã°ããVisitorãChain of ResponsobilityããŸãã¯ä»ã®äœããå®è£ ããŠãã«ã¹ã¿ã å€ãã³ãã©ãŒãäœæããæ©èœãè¿œå ããŸãã
5ïŒä»ã®äœãã
è¬èŸïŒç§ã¯ç¹ã«ãããŒã ãªãŒããŒã«ããããä¹ãè¶ãããããšãèš±å¯ããŠãããããšããããžã§ã¯ãã®ãªãªãŒã¹åã®å°é£ãªæéããããŠç§ãè³çŠã®éã«ãã°ãç Žå£ããããŒã ã«å¿èããŠãããããšã«æè¬ããŸãã ãã³ãžã£ãã³ã®ãšããã¹ã«æè¬ããŸã-ããªãã¯æ¬åœã«ã¯ãŒã«ãªäººã§ãããç§ãã¡ã¯è»èŒªãåçºæããã®ã¯å¥œãã§ã¯ãããŸããããç§ãã¡ã«ã¯éžæè¢ããããŸããã§ããã