Commit 82f88474 authored by darrylcoder's avatar darrylcoder

initial commit

parents
/vendor
composer.phar
composer.lock
.DS_Store
language: php
php:
- 5.4
- 5.5
- 5.6
- hhvm
before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev
script: phpunit
{
"name": "darryldecode/cart",
"description": "Laravel 4 Shopping cart",
"keywords": ["laravel", "shopping cart", "cart"],
"license": "MIT",
"authors": [
{
"name": "Darryl Fernandez",
"email": "engrdarrylfernandez@gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.2.*@dev",
"illuminate/validation": "4.2.*@dev"
},
"require-dev": {
"mockery/mockery": "~0.9",
"phpunit/phpunit": "~4.0",
"symfony/var-dumper": "2.7.*@dev",
"illuminate/events": "4.2.*@dev"
},
"autoload": {
"psr-0": {
"Darryldecode\\Cart": "src/"
}
},
"minimum-stability": "dev"
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
This diff is collapsed.
<?php namespace Darryldecode\Cart;
use Illuminate\Support\Collection;
class CartCollection extends Collection {
/**
* check if the cart contains an item with the given ID
*
* @param $itemId
* @return bool
*/
public function hasItem($itemId)
{
foreach($this->items as $item)
{
if( $item['id'] === $itemId ) return true;
}
return false;
}
/**
* pull an item on cart content using item id and then remove it totally
*
* @param $itemId
* @return null
*/
public function pullItem($itemId)
{
$itemToBePulled = null;
foreach($this->items as $k => $v)
{
if( $v['id'] === $itemId )
{
$this->forget($k);
$itemToBePulled = $v;
}
}
return $itemToBePulled;
}
/**
* remove an item on the cart by item Id
*
* @param $itemId
*/
public function removeItem($itemId)
{
foreach($this->items as $k => $v)
{
if( $v['id'] === $itemId )
{
$this->forget($k);
}
}
}
}
\ No newline at end of file
<?php namespace Darryldecode\Cart;
use Darryldecode\Cart\Exceptions\InvalidConditionException;
use Darryldecode\Cart\Helpers\Helpers;
use Darryldecode\Cart\Validators\CartConditionValidator;
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/15/2015
* Time: 9:02 PM
*/
class CartCondition {
/**
* @var array
*/
private $args;
/**
* @param array $args (name, type, target, value)
*/
public function __construct(array $args)
{
$this->args = $args;
if( Helpers::isMultiArray($args) )
{
foreach($args as $arg)
{
$this->validate($arg);
}
}
else
{
$this->validate($this->args);
}
}
/**
* the target of where the condition is applied
*
* @param null $conditionName | if no conditionName provided, it will only pull the first item on conditions array
* @return mixed
*/
public function getTarget($conditionName = null)
{
if( Helpers::isMultiArray($this->args) )
{
if( $conditionName )
{
foreach($this->args as $condData)
{
if( $condData['name'] == $conditionName )
{
return $condData['target'];
}
}
}
else
{
return $this->args[0]['target'];
}
} else {
return $this->args['target'];
}
}
/**
* the name of the condition
*
* @param null $key
* @return mixed
*/
public function getName($key = null)
{
if( Helpers::isMultiArray($this->args) )
{
if( $key )
{
foreach($this->args as $k => $v)
{
if( $k == $key )
{
return $v['name'];
}
}
}
else
{
return $this->args[0]['name'];
}
} else {
return $this->args['name'];
}
}
/**
* the type of the condition, if no condition name provided it will just pull
* the first condition argument
*
* @param null $conditionName
* @return mixed
*/
public function getType($conditionName = null)
{
if( Helpers::isMultiArray($this->args) )
{
if( $conditionName )
{
foreach($this->args as $condData)
{
if( $condData['name'] == $conditionName )
{
return $condData['type'];
}
}
}
else
{
return $this->args[0]['type'];
}
} else {
return $this->args['type'];
}
}
/**
* the value of this the condition
*
* @param null $conditionName
* @return mixed
*/
public function getValue($conditionName = null)
{
if( Helpers::isMultiArray($this->args) )
{
if( $conditionName )
{
foreach($this->args as $condData)
{
if( $condData['name'] == $conditionName )
{
return $condData['value'];
}
}
}
else
{
return $this->args[0]['value'];
}
} else {
return $this->args['value'];
}
}
/**
* apply condition to total or subtotal
*
* @param $totalOrSubTotalOrPrice
* @return float
*/
public function applyCondition($totalOrSubTotalOrPrice)
{
if( Helpers::isMultiArray($this->args) )
{
$originalPrice = $totalOrSubTotalOrPrice;
$newPrice = 0.00;
$processed = 0;
foreach($this->args as $arg)
{
( $processed > 0 ) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
$newPrice = $this->apply($toBeCalculated, $arg['value']);
$processed++;
}
return $newPrice;
}
else
{
return $this->apply($totalOrSubTotalOrPrice, $this->getValue());
}
}
/**
* apply condition
*
* @param $totalOrSubTotalOrPrice
* @param $conditionValue
* @return float
*/
protected function apply($totalOrSubTotalOrPrice, $conditionValue)
{
// if value has a percentage sign on it, we will get first
// its percentage then we will evaluate again if the value
// has a minus or plus sign so we can decide what to do with the
// percentage, whether to add or subtract it to the total/subtotal/price
// if we can't find any plus/minus sign, we will assume it as plus sign
if( $this->valueIsPercentage($conditionValue) )
{
if( $this->valueIsToBeSubtracted($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
return floatval($totalOrSubTotalOrPrice - $valueToBeSubtracted);
}
else if ( $this->valueIsToBeAdded($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
return floatval($totalOrSubTotalOrPrice - $valueToBeSubtracted);
}
else
{
$value = Helpers::normalizePrice($conditionValue);
$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
return floatval($totalOrSubTotalOrPrice + $valueToBeSubtracted);
}
}
// if the value has no percent sign on it, the operation will not be a percentage
// next is we will check if it has a minus/plus sign so then we can just deduct it to total/subtotal/price
else
{
if( $this->valueIsToBeSubtracted($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
return floatval($totalOrSubTotalOrPrice - $value);
}
else if ( $this->valueIsToBeAdded($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
return floatval($totalOrSubTotalOrPrice + $value);
}
else
{
$value = Helpers::normalizePrice($conditionValue);
return floatval($totalOrSubTotalOrPrice + $value);
}
}
}
/**
* check if value is a percentage
*
* @param $value
* @return bool
*/
protected function valueIsPercentage($value)
{
return (preg_match('/%/', $value) == 1);
}
/**
* check if value is a subtract
*
* @param $value
* @return bool
*/
protected function valueIsToBeSubtracted($value)
{
return (preg_match('/\-/', $value) == 1);
}
/**
* check if value is to be added
*
* @param $value
* @return bool
*/
protected function valueIsToBeAdded($value)
{
return (preg_match('/\+/', $value) == 1);
}
/**
* removes some arithmetic signs (%,+,-) only
*
* @param $value
* @return mixed
*/
protected function cleanValue($value)
{
return str_replace(array('%','-','+'),'',$value);
}
/**
* validates condition arguments
*
* @param $args
* @throws InvalidConditionException
*/
protected function validate($args)
{
$rules = array(
'name' => 'required',
'type' => 'required',
'target' => 'required',
'value' => 'required',
);
$validator = CartConditionValidator::make($args, $rules);
if( $validator->fails() )
{
throw new InvalidConditionException($validator->messages()->first());
}
}
}
\ No newline at end of file
<?php namespace Darryldecode\Cart;
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/15/2015
* Time: 9:46 PM
*/
use Illuminate\Support\Collection;
class CartConditionCollection extends Collection {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart;
use Illuminate\Support\ServiceProvider;
class CartServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['cart'] = $this->app->share(function($app)
{
$storage = $app['session'];
$events = $app['events'];
$instanceName = 'main';
$session_key = '4yTlTDKu3oJOfzD';
return new Cart(
$storage,
$events,
$instanceName,
$session_key
);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}
}
<?php namespace Darryldecode\Cart\Exceptions;
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/15/2015
* Time: 9:24 PM
*/
class InvalidConditionException extends \Exception {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Exceptions;
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/12/2015
* Time: 9:40 PM
*/
class InvalidItemException extends \Exception {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Exceptions;
class InvalidItemFieldException extends \Exception {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Facades;
use Illuminate\Support\Facades\Facade;
class CartFacade extends Facade {
protected static function getFacadeAccessor()
{
return 'cart';
}
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Helpers;
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/15/2015
* Time: 8:09 PM
*/
class Helpers {
/**
* normalize price
*
* @param $price
* @return float
*/
public static function normalizePrice($price)
{
return (is_string($price)) ? floatval($price) : $price;
}
/**
* check if array is multi dimensional array
* This will only check the first element of the array if it is still an array
* to decide that it is a multi dimensional, if you want to check the array strictly
* with all on its element, flag the second argument as true
*
* @param $array
* @param bool $recursive
* @return bool
*/
public static function isMultiArray($array, $recursive = false)
{
if( $recursive )
{
return (count($array) == count($array, COUNT_RECURSIVE)) ? false : true;
}
else
{
foreach ($array as $k => $v)
{
if (is_array($v))
{
return true;
}
else
{
return false;
}
}
}
}
/**
* check if variable is set and has value, return a default value
*
* @param $var
* @param bool|mixed $default
* @return null
*/
public static function issetAndHasValueOrAssignDefault(&$var, $default = false)
{
if( (isset($var)) && ($var!='') ) return $var;
return $default;
}
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Validators;
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/16/2015
* Time: 11:03 AM
*/
class CartConditionValidator extends Validator {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Validators;
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/16/2015
* Time: 11:00 AM
*/
class CartItemValidator extends Validator {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Validators;
use Symfony\Component\Translation\Translator;
use Illuminate\Validation\Factory;
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/16/2015
* Time: 10:59 AM
*/
abstract class Validator {
protected static $factory;
public static function instance()
{
if ( ! static::$factory)
{
$translator = new Translator('en');
static::$factory = new Factory($translator);
}
return static::$factory;
}
public static function __callStatic($method, $args)
{
$instance = static::instance();
switch (count($args))
{
case 0:
return $instance->$method();
case 1:
return $instance->$method($args[0]);
case 2:
return $instance->$method($args[0], $args[1]);
case 3:
return $instance->$method($args[0], $args[1], $args[2]);
case 4:
return $instance->$method($args[0], $args[1], $args[2], $args[3]);
default:
return call_user_func_array(array($instance, $method), $args);
}
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
<?php
/**
* Created by PhpStorm.
* User: darryl
* Date: 1/16/2015
* Time: 3:20 PM
*/
use Darryldecode\Cart\Cart;
use Mockery as m;