<?php

namespace PeytzMail\Entity;

use PeytzMail\Entity\Event;
use PeytzMail\Exception\InvalidArgumentException;

/**
 * @package PeytzMail
 */
class Events extends AbstractEntity
{
    protected $currentPage   = 0;
    protected $totalRecords  = 0;
    protected $totalPages    = 0;
    protected $nextPage      = '';
    protected $events        = array();

    /**
     * Create the events entity from the given JSON string.
     *
     * @param  string                   $json The JSON string.
     * @throws InvalidArgumentException
     */
    public function createFromJson($json)
    {
        $data = json_decode($json);

        if (!isset($data->events)) {
            throw new InvalidArgumentException('[Peytz Mail] We cannot create the entity with this JSON data.');
        }

        foreach ($data->events as $event) {
            $currentEvent = new Event;
            $currentEvent->createFromObject($event);

            $this->events[] = $currentEvent;
        }

        $this->currentPage  = $data->current_page;
        $this->totalRecords = $data->total_records;
        $this->totalPages   = $data->total_pages;
        $this->nextPage     = $data->next_page;
    }
}
