Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsposito committed Feb 27, 2015
0 parents commit 05e6ca9
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Daniel Sposito

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Argo

Argo is a useful utility for shipping-related tasks. In particular, it deduces the shipping carrier, provider and other information based on a provided package tracking number.

In [Greek mythology](http://en.wikipedia.org/wiki/Argo), Argo (in Greek, meaning 'swift') was the ship on which Jason and the Argonauts sailed from Iolcos to retrieve the Golden Fleece. Argo was said to have been planned with the help of Athena - constructed with magical pieces of timber from the sacred forest of Dodona.

## Usage
Simply provide a tracking number when initializing a new package instance.

```
$package = Argo\Package::instance('1Z 3W4 72Y 42 9990 3055');
print_r($package);
```

Example output:

```
Argo\Package Object
(
[tracking_code] => 1Z3W472Y4299903055
[carrier] => Argo\Carrier Object
(
[code] => ups
[name] => UPS
)
[provider] => Argo\Provider Object
(
[code] => ups
[name] => UPS
)
)
```
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "dsposito/argo",
"description": "A shipping utility.",
"keywords": ["argo", "shipping", "tracking number", "php"],
"type": "library",
"homepage": "https://github.com/dsposito/argo",
"license": "MIT",
"authors": [
{
"name": "Daniel Sposito",
"email": "daniel.g.sposito@gmail.com",
"homepage": "https://github.com/dsposito"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {"Argo\\": "src/"}
}
}
62 changes: 62 additions & 0 deletions src/Carrier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Argo;

/**
* Handles carrier interactions.
*/
class Carrier
{
/**
* Carrier code.
*
* @var string
*/
public $code;

/**
* Carrier display name.
*
* @var string.
*/
public $name;

/**
* Supported carriers.
*
* @var array
*/
private static $carriers = array(
'dhl' => 'DHL',
'fedex' => 'FedEx',
'ups' => 'UPS',
'usps' => 'USPS',
);

/**
* Initializes the class.
*
* @param string $code The carrier code.
*
* @return void
*/
public function __construct($code)
{
if (!array_key_exists($code, self::$carriers)) {
return false;
}

$this->code = $code;
$this->name = self::$carriers[$code];
}

/**
* Gets the supported carriers.
*
* @return array
*/
public static function getCarriers()
{
return self::$carriers;
}
}
109 changes: 109 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Argo;

/**
* The primary Argo class.
* Handles package interactions.
*
* @see https://github.com/dsposito/argo
*/
class Package
{
/**
* Tracking code.
*
* @var string
*/
public $tracking_code;

/**
* Carrier data.
*
* @var Argo\Carrier
*/
public $carrier;

/**
* Provider data.
*
* @var Argo\Provider
*/
public $provider;

/**
* Creates a package instance based on a tracking code.
*
* @param string $tracking_code The package tracking code.
*
* @return Argo\Package
*/
public static function instance($tracking_code)
{
$instance = new self();
$instance->tracking_code = preg_replace('/[^A-Z0-9]/i', '', $tracking_code);

return $instance->deduceTrackingCode();
}

/**
* Gets this package's carrier code.
*
* @return string
*/
public function carrierCode()
{
if (!$this->carrier instanceof Carrier) {
return false;
}

return $this->carrier->code;
}

/**
* Gets this package's provider code.
*
* @return string
*/
public function providerCode()
{
if (!$this->provider instanceof Provider) {
return false;
}

return $this->provider->code;
}

/**
* Determines the package's shipping details based on its tracking code.
*
* @return void
*/
private function deduceTrackingCode()
{
$tracking_code = $this->tracking_code;
$carrier_code = null;
$provider_code = null;

if (preg_match('/^[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{3}$/', $tracking_code)) {
$carrier_code = 'fedex';
}
else if (preg_match('/^1Z[A-Z0-9]{3}[A-Z0-9]{3}[0-9]{2}[0-9]{4}[0-9]{4}$/i', $tracking_code)) {
$carrier_code = 'ups';
}
else if (preg_match('/^[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{2}$/', $tracking_code)) {
$carrier_code = 'usps';
}
elseif (preg_match('/^420[0-9]{5}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{2}$/', $tracking_code)) {
$carrier_code = 'usps';
$provider_code = 'endicia';
}

if (!empty($carrier_code)) {
$this->carrier = new Carrier($carrier_code);
$this->provider = new Provider($provider_code ?: $carrier_code);
}

return $this;
}
}
58 changes: 58 additions & 0 deletions src/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Argo;

/**
* Handles provider interactions.
*/
class Provider
{
/**
* Supported providers.
*/
private static $providers = array(
'endicia' => 'Endicia',
);

/**
* Provider code.
*
* @var string
*/
public $code;

/**
* Provider display name.
*
* @var string.
*/
public $name;

/**
* Initializes the class.
*
* @param string $code The provider code.
*
* @return void
*/
public function __construct($code)
{
$providers = array_merge(self::$providers, Carrier::getCarriers());
if (!array_key_exists($code, $providers)) {
return false;
}

$this->code = $code;
$this->name = $providers[$code];
}

/**
* Gets the supported providers.
*
* @return array
*/
public static function getProviders()
{
return self::$providers;
}
}

0 comments on commit 05e6ca9

Please sign in to comment.