Skip to content

Commit

Permalink
(#11) separate Config
Browse files Browse the repository at this point in the history
  • Loading branch information
totoprayogo1916 authored Oct 16, 2023
1 parent b9e9122 commit a347a4b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.github export-ignore
.gitattributes export-ignore
.gitignore export-ignore
31 changes: 31 additions & 0 deletions src/Config/Cart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Totoprayogo1916\CodeIgniter\Config;

use CodeIgniter\Config\BaseConfig;

class Cart extends BaseConfig
{
/**
* These are the regular expression rules that we use to validate the product ID and product name
* alpha-numeric, dashes, underscores, or periods
*
* @var string
*/
public $product_id_rules = '\.a-z0-9_-';

/**
* These are the regular expression rules that we use to validate the product ID and product name
* alpha-numeric, dashes, underscores, colons or periods
*
* @var string
*/
public $product_name_rules = '\w \-\.\:';

/**
* only allow safe product names
*
* @var bool
*/
public $product_name_safe = true;
}
31 changes: 4 additions & 27 deletions src/Libraries/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,6 @@
*/
class Cart
{
/**
* These are the regular expression rules that we use to validate the product ID and product name
* alpha-numeric, dashes, underscores, or periods
*
* @var string
*/
public $product_id_rules = '\.a-z0-9_-';

/**
* These are the regular expression rules that we use to validate the product ID and product name
* alpha-numeric, dashes, underscores, colons or periods
*
* @var string
*/
public $product_name_rules = '\w \-\.\:';

/**
* only allow safe product names
*
* @var bool
*/
public $product_name_safe = true;

/**
* Contents of the cart
*
Expand Down Expand Up @@ -139,16 +116,16 @@ protected function _insert(array $items = [])

// Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
// Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
// Note: These can be user-specified by setting the $this->product_id_rules variable.
if (! preg_match('/^[' . $this->product_id_rules . ']+$/i', $items['id'])) {
// Note: These can be user-specified by setting the config(''Cart')->product_id_rules variable.
if (! preg_match('/^[' . config('Cart')->product_id_rules . ']+$/i', $items['id'])) {
log_message('error', 'Invalid product ID. The product ID can only contain alpha-numeric characters, dashes, and underscores');

return false;
}

// Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
// Note: These can be user-specified by setting the $this->product_name_rules variable.
if ($this->product_name_safe && ! preg_match('/^[' . $this->product_name_rules . ']+$/iu', $items['name'])) {
// Note: These can be user-specified by setting the config(''Cart')->product_name_rules variable.
if (config('Cart')->product_name_safe && ! preg_match('/^[' . config('Cart')->product_name_rules . ']+$/iu', $items['name'])) {
log_message('error', 'An invalid name was submitted as the product name: ' . $items['name'] . ' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');

return false;
Expand Down
35 changes: 35 additions & 0 deletions src/Publishers/ConfigPublisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Totoprayogo1916\CodeIgniter\Publishers;

use CodeIgniter\Publisher\Publisher;

class ConfigPublisher extends Publisher
{
/**
* Tell Publisher where to get the files.
* Since we will use Composer to download
* them we point to the "vendor" directory.
*
* @var string
*/
protected $source = VENDORPATH . 'totoprayogo1916/codeigniter4-cart/src/Config/';

/**
* Target to save
*
* @var string
*/
protected $destination = APPPATH . 'Config';

/**
* Use the "publish" method to indicate that this
* class is ready to be discovered and automated.
*/
public function publish(): bool
{
return $this
->addPath('Cart.php')
->copy();
}
}

0 comments on commit a347a4b

Please sign in to comment.