| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 | <?phpnamespace Aws\Sns;use Psr\Http\Message\RequestInterface;/** * Represents an SNS message received over http(s). */class Message implements \ArrayAccess, \IteratorAggregate{    private static $requiredKeys = [        'Message',        'MessageId',        'Timestamp',        'TopicArn',        'Type',        'Signature',        ['SigningCertURL', 'SigningCertUrl'],        'SignatureVersion',    ];    private static $subscribeKeys = [        ['SubscribeURL', 'SubscribeUrl'],        'Token'    ];    /** @var array The message data */    private $data;    /**     * Creates a Message object from the raw POST data     *     * @return Message     * @throws \RuntimeException If the POST data is absent, or not a valid JSON document     */    public static function fromRawPostData()    {        // Make sure the SNS-provided header exists.        if (!isset($_SERVER['HTTP_X_AMZ_SNS_MESSAGE_TYPE'])) {            throw new \RuntimeException('SNS message type header not provided.');        }        // Read the raw POST data and JSON-decode it into a message.        return self::fromJsonString(file_get_contents('php://input'));    }    /**     * Creates a Message object from a PSR-7 Request or ServerRequest object.     *     * @param RequestInterface $request     * @return Message     */    public static function fromPsrRequest(RequestInterface $request)    {        return self::fromJsonString($request->getBody());    }    /**     * Creates a Message object from a JSON-decodable string.     *     * @param string $requestBody     * @return Message     */    public static function fromJsonString($requestBody)    {        $data = json_decode($requestBody, true);        if (JSON_ERROR_NONE !== json_last_error() || !is_array($data)) {            throw new \RuntimeException('Invalid POST data.');        }        return new Message($data);    }    /**     * Creates a Message object from an array of raw message data.     *     * @param array $data The message data.     *     * @throws \InvalidArgumentException If a valid type is not provided or     *                                   there are other required keys missing.     */    public function __construct(array $data)    {        // Ensure that all the required keys for the message's type are present.        $this->validateRequiredKeys($data, self::$requiredKeys);        if ($data['Type'] === 'SubscriptionConfirmation'            || $data['Type'] === 'UnsubscribeConfirmation'        ) {            $this->validateRequiredKeys($data, self::$subscribeKeys);        }        $this->data = $data;    }    #[\ReturnTypeWillChange]    public function getIterator()    {        return new \ArrayIterator($this->data);    }    #[\ReturnTypeWillChange]    public function offsetExists($key)    {        return isset($this->data[$key]);    }    #[\ReturnTypeWillChange]    public function offsetGet($key)    {        return isset($this->data[$key]) ? $this->data[$key] : null;    }    #[\ReturnTypeWillChange]    public function offsetSet($key, $value)    {        $this->data[$key] = $value;    }    #[\ReturnTypeWillChange]    public function offsetUnset($key)    {        unset($this->data[$key]);    }    /**     * Get all the message data as a plain array.     *     * @return array     */    public function toArray()    {        return $this->data;    }    private function validateRequiredKeys(array $data, array $keys)    {        foreach ($keys as $key) {            $keyIsArray = is_array($key);            if (!$keyIsArray) {                $found = isset($data[$key]);            } else {                $found = false;                foreach ($key as $keyOption) {                    if (isset($data[$keyOption])) {                        $found = true;                        break;                    }                }            }            if (!$found) {                if ($keyIsArray) {                    $key = $key[0];                }                throw new \InvalidArgumentException(                    "\"{$key}\" is required to verify the SNS Message."                );            }        }    }}
 |