PBSAuth

class PBSAuth implements JsonSerializable

Class PBSAuth

Represents the result of a PBS Account authentication. Acts primarily as a container for the Owner and Token pair of objects. The owner property must represent a valid Owner object and the token property must represent a valid Token object.

Constants

REQUIRED = [

Methods

public static PBSAuthResult fromJSON(string $record) No description
public static PBSAuthResult fromArray(array $record) No description
public static PBSAuthResult fromStdClass(stdClass $record) No description
public array toArray() No description
public stdClass toStdClass() No description
public mixed jsonSerialize() No description
public Owner getOwner() Gets this inner Owner object.
public Token getToken() Gets the inner Token object.

Details

at line 46

fromJSON()

public static PBSAuthResult fromJSON(string $record)

Parameters

string $record

Return Value

PBSAuthResult
at line 59

fromArray()

public static PBSAuthResult fromArray(array $record)

Parameters

array $record

Return Value

PBSAuthResult
at line 74

fromStdClass()

public static PBSAuthResult fromStdClass(stdClass $record)

Parameters

stdClass $record

Return Value

PBSAuthResult
at line 103

toArray()

public array toArray()

Return Value

array
at line 113

toStdClass()

public stdClass toStdClass()

Return Value

stdClass
at line 120

jsonSerialize()

public mixed jsonSerialize()

Return Value

mixed
at line 129

getOwner()

public Owner getOwner()

Gets this inner Owner object.

Return Value

Owner
at line 138

getToken()

public Token getToken()

Gets the inner Token object.

Return Value

Token

Source code

<?php

namespace LibPBSAuth;

use LibPBSAuth\Result\PBSAuthResult;

/**
 * Class PBSAuth
 *
 * Represents the result of a PBS Account authentication. Acts primarily as a
 * container for the Owner and Token pair of objects. The owner property must
 * represent a valid Owner object and the token property must represent a valid
 * Token object.
 *
 * @package LibPBSAuth
 */
class PBSAuth implements \JsonSerializable {

  /**
   * @var array
   */
  const REQUIRED = [
    'owner', 'token'
  ];

  /**
   * @var Owner
   */
  private $_owner;

  /**
   * @var Token
   */
  private $_token;


  private function __construct(Owner $owner, Token $token) {
    $this->_owner = $owner;
    $this->_token = $token;
  }

  /**
   * @param string $record
   * @return PBSAuthResult
   */
  public static function fromJSON(string $record): PBSAuthResult {
    try {
      $parsed = ex_json_decode($record);
      return self::fromStdClass($parsed);
    } catch (\Exception $e) {
      return PBSAuthResult::err($e);
    }
  }

  /**
   * @param array $record
   * @return PBSAuthResult
   */
  public static function fromArray(array $record): PBSAuthResult {

    // Records do not get terribly large, so for simplicity we encode and then
    // decode from JSON at the cost of a little performance
    try {
      return self::fromJSON(ex_json_encode($record));
    } catch (\Exception $e) {
      return PBSAuthResult::err($e);
    }
  }

  /**
   * @param \stdClass $record
   * @return PBSAuthResult
   */
  public static function fromStdClass(\stdClass $record): PBSAuthResult {
    foreach (self::REQUIRED as $req) {
      if (!isset($record->{$req})) {
        return PBSAuthResult::err(new \InvalidArgumentException("Malformed PBS auth. {$req} field is missing."));
      }
    }

    $ownerRes = Owner::fromStdClass($record->owner);

    if ($ownerRes->isError()) {
      return PBSAuthResult::err($ownerRes->getErr());
    } else {
      $owner = $ownerRes->value();
    }

    $tokenRes = Token::fromStdClass($record->token);

    if ($tokenRes->isError()) {
      return PBSAuthResult::err($tokenRes->getErr());
    } else {
      $token = $tokenRes->value();
    }

    return PBSAuthResult::ok(new PBSAuth($owner, $token));
  }

  /**
   * @return array
   */
  public function toArray(): array {
    return [
      'owner' => $this->getOwner()->toArray(),
      'token' => $this->getToken()->toArray()
    ];
  }

  /**
   * @return \stdClass
   */
  public function toStdClass(): \stdClass {
    return json_decode(json_encode($this));
  }

  /**
   * @return mixed
   */
  public function jsonSerialize() {
    return $this->toArray();
  }

  /**
   * Gets this inner Owner object.
   *
   * @return Owner
   */
  public function getOwner(): Owner {
    return $this->_owner;
  }

  /**
   * Gets the inner Token object.
   *
   * @return Token
   */
  public function getToken(): Token {
    return $this->_token;
  }
}