CustomFieldsResponseBuilder.php
11 months ago
DynamicSegmentsResponseBuilder.php
2 months ago
FormsResponseBuilder.php
11 months ago
NewsletterTemplatesResponseBuilder.php
11 months ago
NewslettersResponseBuilder.php
3 weeks ago
ScheduledTaskSubscriberResponseBuilder.php
3 years ago
SegmentsResponseBuilder.php
2 months ago
SendingQueuesResponseBuilder.php
2 months ago
SubscribersResponseBuilder.php
2 weeks ago
index.php
3 years ago
FormsResponseBuilder.php
55 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON\ResponseBuilders; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\FormEntity; |
| 9 | use MailPoet\Statistics\StatisticsFormsRepository; |
| 10 | |
| 11 | class FormsResponseBuilder { |
| 12 | const DATE_FORMAT = 'Y-m-d H:i:s'; |
| 13 | |
| 14 | /** @var StatisticsFormsRepository */ |
| 15 | private $statisticsFormsRepository; |
| 16 | |
| 17 | public function __construct( |
| 18 | StatisticsFormsRepository $statisticsFormsRepository |
| 19 | ) { |
| 20 | $this->statisticsFormsRepository = $statisticsFormsRepository; |
| 21 | } |
| 22 | |
| 23 | public function build(FormEntity $form) { |
| 24 | return [ |
| 25 | 'id' => (string)$form->getId(), // (string) for BC |
| 26 | 'name' => $form->getName(), |
| 27 | 'status' => $form->getStatus(), |
| 28 | 'body' => $form->getBody(), |
| 29 | 'settings' => $form->getSettings(), |
| 30 | 'styles' => $form->getStyles(), |
| 31 | 'created_at' => ($createdAt = $form->getCreatedAt()) ? $createdAt->format(self::DATE_FORMAT) : null, |
| 32 | 'updated_at' => ($updatedAt = $form->getUpdatedAt()) ? $updatedAt->format(self::DATE_FORMAT) : null, |
| 33 | 'deleted_at' => ($deletedAt = $form->getDeletedAt()) ? $deletedAt->format(self::DATE_FORMAT) : null, |
| 34 | ]; |
| 35 | } |
| 36 | |
| 37 | public function buildForListing(array $forms) { |
| 38 | $data = []; |
| 39 | |
| 40 | foreach ($forms as $form) { |
| 41 | $form = $this->build($form); |
| 42 | $form['signups'] = $this->statisticsFormsRepository->getTotalSignups($form['id']); |
| 43 | $form['segments'] = ( |
| 44 | !empty($form['settings']['segments']) |
| 45 | ? $form['settings']['segments'] |
| 46 | : [] |
| 47 | ); |
| 48 | |
| 49 | $data[] = $form; |
| 50 | } |
| 51 | |
| 52 | return $data; |
| 53 | } |
| 54 | } |
| 55 |