Skip to content

Commit

Permalink
[8-1] InquiryCsvBuilderクラスを作成
Browse files Browse the repository at this point in the history
  • Loading branch information
hidenorigoto committed Dec 28, 2015
1 parent de36b68 commit 663e927
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/lists/ch08/08-01.txt
@@ -0,0 +1,38 @@
<?php
namespace AppBundle\Service\Csv;

use AppBundle\Entity\Inquiry;
use League\Csv\Writer;

class InquiryCsvBuilder
{
private $encoding;
private $inquiryRepository;

public function __construct($encoding, $inquiryRepository) {
$this->encoding = $encoding;
$this->inquiryRepository = $inquiryRepository;
}

/**
* CSV形式の文字列を作成する
*/
public function build($keyword) {
$inquiryList = $this->inquiryRepository->findAllByKeyword($keyword);

/** @var Writer $writer */
$writer = Writer::createFromString('','');
$writer->setNewline("\r\n");

foreach ($inquiryList as $inquiry) {
/** @var Inquiry $inquiry */
$writer->insertOne([
$inquiry->getId(),
$inquiry->getName(),
$inquiry->getEmail()
]);
}

return mb_convert_encoding((string)$writer, $this->encoding, 'UTF-8');
}
}
38 changes: 38 additions & 0 deletions src/AppBundle/Service/Csv/InquiryCsvBuilder.php
@@ -0,0 +1,38 @@
<?php
namespace AppBundle\Service\Csv;

use AppBundle\Entity\Inquiry;
use League\Csv\Writer;

class InquiryCsvBuilder
{
private $encoding;
private $inquiryRepository;

public function __construct($encoding, $inquiryRepository) {
$this->encoding = $encoding;
$this->inquiryRepository = $inquiryRepository;
}

/**
* CSV形式の文字列を作成する
*/
public function build($keyword) {
$inquiryList = $this->inquiryRepository->findAllByKeyword($keyword);

/** @var Writer $writer */
$writer = Writer::createFromString('','');
$writer->setNewline("\r\n");

foreach ($inquiryList as $inquiry) {
/** @var Inquiry $inquiry */
$writer->insertOne([
$inquiry->getId(),
$inquiry->getName(),
$inquiry->getEmail()
]);
}

return mb_convert_encoding((string)$writer, $this->encoding, 'UTF-8');
}
}

0 comments on commit 663e927

Please sign in to comment.