VPPA

class VPPA implements JsonSerializable

Class VPPA

Describes VPPA agreement information for the authenticated user and the authenticating station. Requires only that the vppa_accepted and vppa_last_updated fields are present. If vppa_last_updated contains a truthy value, then it must be able to be parsed into a \DateTime.

Constants

EXISTS = [

DATEFORMAT = 'Y-m-d H:i:s.uP';

Methods

public static VPPAResult fromJSON(string $record) No description
public static VPPAResult fromArray(array $record) No description
public static VPPAResult fromStdClass(stdClass $record) No description
public array toArray() No description
public stdClass toStdClass() No description
public mixed jsonSerialize() No description
public bool isVPPAAccepted() Gets the represented VPPA status. This does not perform validation as to when VPPA was accepted. The caller must use this in conjunction with getVPPALastUpdated() to determine if the acceptance is still valid.
public DateTime|null getVPPALastUpdated() Gets the last time that VPPA was accepted by the user. Returns a \DateTime value if an acceptance date was available. If this represents a non-accepted VPPA agreement, then null if returned.

Details

at line 55

fromJSON()

public static VPPAResult fromJSON(string $record)

Parameters

string $record

Return Value

VPPAResult
at line 68

fromArray()

public static VPPAResult fromArray(array $record)

Parameters

array $record

Return Value

VPPAResult
at line 83

fromStdClass()

public static VPPAResult fromStdClass(stdClass $record)

Parameters

stdClass $record

Return Value

VPPAResult
at line 112

toArray()

public array toArray()

Return Value

array
at line 122

toStdClass()

public stdClass toStdClass()

Return Value

stdClass
at line 129

jsonSerialize()

public mixed jsonSerialize()

Return Value

mixed
at line 147

isVPPAAccepted()

public bool isVPPAAccepted()

Gets the represented VPPA status. This does not perform validation as to when VPPA was accepted. The caller must use this in conjunction with getVPPALastUpdated() to determine if the acceptance is still valid.

Return Value

bool
at line 162

getVPPALastUpdated()

public DateTime|null getVPPALastUpdated()

Gets the last time that VPPA was accepted by the user. Returns a \DateTime value if an acceptance date was available. If this represents a non-accepted VPPA agreement, then null if returned.

Return Value

DateTime|null

Source code

<?php

namespace LibPBSAuth;

use LibPBSAuth\Result\VPPAResult;

/**
 * Class VPPA
 *
 * Describes VPPA agreement information for the authenticated user and the
 * authenticating station. Requires only that the vppa_accepted and
 * vppa_last_updated fields are present. If vppa_last_updated contains a truthy
 * value, then it must be able to be parsed into a \DateTime.
 *
 * @package LibPBSAuth
 */
class VPPA implements \JsonSerializable {

  /**
   * @var array
   */
  const EXISTS = [
    'vppa_accepted', 'vppa_last_updated'
  ];

  /**
   * @var string
   */
  const DATEFORMAT = 'Y-m-d H:i:s.uP';

  /**
   * @var bool
   */
  private $_vppaAccepted;

  /**
   * @var \DateTime
   */
  private $_vppaLastUpdated;

  /**
   * VPPA constructor.
   * @param bool|null $vppaAccepted
   * @param \DateTime|null $vppaLastUpdated
   */
  private function __construct(?bool $vppaAccepted, ?\DateTime $vppaLastUpdated) {
    $this->_vppaAccepted = $vppaAccepted;
    $this->_vppaLastUpdated = $vppaLastUpdated;
  }

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

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

    // 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 VPPAResult::err($e);
    }
  }

  /**
   * @param \stdClass $record
   * @return VPPAResult
   */
  public static function fromStdClass(\stdClass $record): VPPAResult {
    foreach (self::EXISTS as $req) {
      if (!property_exists($record, $req)) {
        return VPPAResult::err(new \InvalidArgumentException("Malformed VPPA data. {$req} field must be present."));
      }
    }

    // Last updated date must parse properly as a date if it is set
    $lastUpdated = null;

    if ($record->vppa_last_updated) {
      $lastUpdated = new \DateTime($record->vppa_last_updated);

      if ($lastUpdated === false) {
        return VPPAResult::err(new \InvalidArgumentException("Malformed VPPA data. Last updated date field is not correctly formatted."));
      }
    }

    return VPPAResult::ok(
      new VPPA(
        $record->vppa_accepted,
        $lastUpdated
      )
    );
  }

  /**
   * @return array
   */
  public function toArray(): array {
    return [
      'vppa_accepted' => $this->_getVPPAAccepted(),
      'vppa_last_updated' => $this->getVPPALastUpdated() === null ? null : $this->getVPPALastUpdated()->format(self::DATEFORMAT)
    ];
  }

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

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

  /**
   * @return bool|null
   */
  private function _getVPPAAccepted(): ?bool {
    return $this->_vppaAccepted;
  }

  /**
   * Gets the represented VPPA status. This does not perform validation as to
   * when VPPA was accepted. The caller must use this in conjunction with
   * getVPPALastUpdated() to determine if the acceptance is still valid.
   *
   * @return bool
   */
  public function isVPPAAccepted(): bool {
    if ($this->_getVPPAAccepted()) {
      return true;
    }

    return false;
  }

  /**
   * Gets the last time that VPPA was accepted by the user. Returns a \DateTime
   * value if an acceptance date was available. If this represents a
   * non-accepted VPPA agreement, then null if returned.
   *
   * @return \DateTime|null
   */
  public function getVPPALastUpdated(): ?\DateTime {
    return $this->_vppaLastUpdated;
  }
}