Skip to content

Instantly share code, notes, and snippets.

@iambrianreich
Created October 20, 2017 04:25
Show Gist options
  • Save iambrianreich/cc7331d8ac4c5896c85154d9b0700091 to your computer and use it in GitHub Desktop.
Save iambrianreich/cc7331d8ac4c5896c85154d9b0700091 to your computer and use it in GitHub Desktop.
PHP 7 Assertsions
<?php
/**
* This file contains the Catalyst\OrderLine\Status class.
*
* PHP Version 7.1
*
* @category Catalyst
* @package Catalyst
* @subpackage OrderLine
* @author Brian Reich <[email protected]>
* @copyright 2017 Catalyst Fabric Solitions
* @license Private Use
* @link http://www.catalystfabricsolutions.com/
*/
namespace Catalyst\OrderLine;
/**
* Enumerates valid OrderLine status codes.
*
* PHP Version 7.1
*
* @category Catalyst
* @package Catalyst
* @subpackage OrderLine
* @author Brian Reich <[email protected]>
* @copyright 2017 Catalyst Fabric Solitions
* @license Private Use
* @link http://www.catalystfabricsolutions.com/
*/
class Status extends \SplType
{
/**
* Line item has been accepted into the API and is ready for manufacturing.
*
* @var string
*/
const PROCESSING = 'Processing';
/**
* Line item is a Print-On-Demand product that has been sent to a print job
* that has not been completed.
*
* @var string
*/
const PRINTING = 'Printing';
/**
* Line item is a Print-On-Demand product that has finished printing, or is
* a Pick and Pack Product that does not need printed.
*
* @todo Should there be a separate status for P&P?
* @var string
*/
const PRINTED = 'Printed';
/**
* Line item has been shipped to the customer. This means that the item has
* either been picked up by the carrier, or has been packaged, labeled, and
* is waiting for carrier pickup.
*
* @todo Should we have a separate status for "Ready for Carrier Pickup" vs "Shipped"? LIke Shutterfly?
* @var string
*/
const SHIPPED = 'Shipped';
/**
* Line item has been rejected (or cancelled).
*
* @todo Do we need a way to distinguish between Catalyst rejecting the item, versus the customer cancelling?
* @var string
*/
const REJECTED = 'Rejected';
/**
* Keep a cache of valid statuses so we don't need to reflect twice.
*
* @var array
* @access private
*/
private static $constants = null;
/**
* Returns true if the specified string is a valid status.
*
* @param string $status The string to check for valid status.
* @return bool Returns true if string is a valid status.
*/
public static function isValid(string $status) : bool
{
assert(! empty($status));
assert(is_string($status));
assert(is_array(self::$constants) || is_null(self::$constants));
if(is_null(self::$constants))
{
self::$constants = (new \ReflectionClass(__CLASS__))->getConstants();
}
return in_array(self::$constants, $status);
}
}
<?php
namespace Catalyst\Customers\Shutterfly;
use Meno\Hydration\JsonHydrateableAbstract;
use Meno\Hydration\HydrationException;
use Catalyst\OrderLine\Status;
class StatusUpdateMessage extends JsonHydrateableAbstract
{
/**
* The unique id the line item whose status has changed.
*
* @var int
* @access private
*/
private $orderLineId;
/**
* The original status of the line item.
*
* @var string
*/
private $oldStatus;
/**
* The new status of the line item.
*
* @var string
* @access private
*/
private $newStatus;
/**
* Sets the unique id of the line item whose status was updated.
*
* @param int $orderLineId The unique id of the line item whose status was updated.
*/
public function setOrderLineId(int $orderLineId) : void
{
$this->orderLineId = $orderLineId;
}
/** Returns the unique id of the line item whose status was updated.
*
* @return int the unique id of the line item whose status was updated.
*/
public function getOrderLineId() : int
{
assert(is_numeric($this->orderLineId));
return $this->orderLineId;
}
/**
* Sets the original status of the line item before the update.
*
* @param string $oldStatus the original status of the line item before the update.
*/
public function setOldStatus(string $oldStatus) : void
{
assert(is_string($oldStatus));
assert(Status::isValid($oldStatus));
$this->oldStatus = $oldStatus;
}
/**
* Returns the original status of the line item before the update.
*
* @return string the original status of the line item before the update.
*/
public function getOldStatus() : string
{
assert(is_string($this->oldStatus));
assert(Status::isValid($this->oldStatus));
return $this->oldStatus;
}
/**
* Sets the new status of the line item.
*
* @param string $newStatus the new status of the line item.
*/
public function setNewStatus(string $newStatus) : void
{
assert(is_string($newStatus));
assert(Status::isValid($newStatus));
$this->newStatus = $newStatus;
}
/**
* Returns the new status of the line item.
*
*
* @return string the new status of the line item.
*/
public function getNewStatus() : string
{
assert(is_string($this->newStatus));
assert(Status::isValid($this->Status));
return $this->newStatus;
}
/**
* Converts the instance to an associative array ready to convert to JSON.
*
* @return array Returns an associative array.
* @throws HydrationException if object cannot hydrate to JSON.
*/
public function toJsonArray() : array
{
return [
'orderLineId' => $this->getOrderLineId(),
'oldStatus' => $this->getOldStatus(),
'newStatus' => $this->getNewStatus()
];
}
/**
* Populates the object from a stdClass converted from JSON.
*
* @param \stdClass $json The json-converted object (from json_decode).
*
* @return void
* @throws HydrationException if object cannot dehydrate from object.
*/
public function fromJsonObject(\stdClass $json) : void
{
assert($json != null);
assert(isset($json->orderLineId));
assert(isset($json->oldStatus));
assert(isset($json->newStatus));
assert(is_numeric($json->orderLineId));
$this->setOrderLineId($json->orderLineId);
$this->setOldStatus($json->oldStatus);
$this->setNewStatus($json->newStatus);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment