      1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107

    <?php

    /**
     * @object MessageMulti
     *
     * MessageMulti provides batch based
     * message processing while always keeping
     * track of which messages were sent, errors
     *
     */
    namespace Catapult;

    class MessageMulti extends GenericResource {
        private $path = "messages";
          public static $args = array(
              "from",
              "to",
              "text",
              "callbackUrl",
              "timeout",
              "media"
          );

        public function __construct($messages=array())
        {
          $this->client = Client::Get();
          $this->messages = $messages;
          $this->complete = FALSE;
        }

        /**
         * Important this function does not
         * provide 'CollectionObject' thus cannot
         * be enumerated. TODO: fix
         */
        public function listMessages($arr)
        {
          $this->messages = array();
          $this->errors = array();

              return $this->messages;
        }

        /**
         * push a message to
         * the queue
         * @all params -> string to objects according to api.
         */
        public function pushMessage(/** polymorphic **/)
        {
          $args = func_get_args();
          $args = $args[0];
          $out = array();
          $cnt = 0;

          foreach ($args as $arg) {
              $out[self::$args[$cnt]] = $arg;
              $cnt ++;
          }

          $message = Ensure::Input($out);

          $this->messages[] = $message->get();
        }

        public function execute()
        {
          if ($this->complete)
            Throw new \CatapultApiException("You\'ve already done this");

          $msgs = $this->post_messages();
          $smsgs = array();

          foreach ($msgs as $msg) {
            $data = array_pop($this->messages);

            if (isset($msg->location)
               || isset($msg->location)) {
              $data['id'] = $msg->location;
              $data['state'] = MESSAGE_STATES::sent;
              $smsgs[] = $msg;
            } elseif (isset($msg->error)
               || isset($msg->error)) {
              $data['error_message'] = $msg->error->message;
              $data['state'] = MESSAGE_STATES::error;
              $smsgs[] = $msg;
            }
          }

          return $smsgs;
        }

        /**
         * multiform send
         * messages output should
         * satisfy array
         */
        protected function postMessages()
        {
          $url = URIResource::Make($this->path);

          $messages = $this->client->post($url, $this->messages);

          return $messages;
        }
    }