/** * Create a simple associative array with * an ArrayCollection of Role. * * @param Doctrine\Common\Collections\ArrayCollection $roles * * @return array */ public function toArray($roles) { $roleSerializer = new RoleJsonSerializer(); $data = []; foreach ($roles as $role) { $data[] = $roleSerializer->toArray($role); } return $data; }
/** * Deserializes a Json into readable datas * @param string $string * * @return RZ\Roadiz\Core\Entities\Group */ public static function deserialize($string) { if ($string == "") { throw new \Exception('File is empty.'); } $encoder = new JsonEncoder(); $nameConverter = new CamelCaseToSnakeCaseNameConverter(['name']); $normalizer = new GetSetMethodNormalizer(null, $nameConverter); $serializer = new Serializer([$normalizer], [$encoder]); $group = $serializer->deserialize($string, 'RZ\\Roadiz\\Core\\Entities\\Group', 'json'); /* * Importing Roles. * * We need to extract roles from group and to re-encode them * to pass to RoleJsonSerializer. */ $tempArray = json_decode($string, true); $data = []; if (!empty($tempArray['roles'])) { foreach ($tempArray['roles'] as $roleAssoc) { $role = RoleJsonSerializer::deserialize(json_encode($roleAssoc)); $role = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneByName($role->getName()); $group->addRole($role); } $data[] = $group; } return $data; }
/** * Create a simple associative array with * an ArrayCollection of Role. * * @param Doctrine\Common\Collections\ArrayCollection $roles * * @return array */ public static function toArray($roles) { $data = []; foreach ($roles as $role) { $data[] = RoleJsonSerializer::toArray($role); } return $data; }