Skip to content

Commit

Permalink
Merge pull request #12 from SoapBox/performance/improvements
Browse files Browse the repository at this point in the history
[Performance] Make improvements to key validation
  • Loading branch information
Justin Hayes authored Apr 24, 2019
2 parents 5e08c53 + b916196 commit dd80a1d
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 91 deletions.
17 changes: 7 additions & 10 deletions src/Builders/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace SoapBox\Settings\Builders;

use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use SoapBox\Settings\Models\SettingValue;
use SoapBox\Settings\Utilities\KeyValidator;
use SoapBox\Settings\Models\SettingDefinition;
Expand All @@ -19,7 +16,7 @@ class Settings
/**
* Create a new text setting
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group, key or default value fail to pass validation. The
* group and key must only contain characters in the set
* [a-zA-Z0-9-_]. The default value must pass the custom validation
Expand All @@ -45,7 +42,7 @@ public static function text(string $group, string $key, string $default, string
/**
* Create a new boolean setting
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group or key fail to pass validation. The group and key
* must only contain characters in the set [a-zA-Z0-9-_].
*
Expand All @@ -68,7 +65,7 @@ public static function boolean(string $group, string $key, bool $default): void
/**
* Create a new single select setting
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group, key, options or default value fail to pass
* validation. The group, key and each option must only contain
* characters in the set [a-zA-Z0-9-_]. The default value must be in
Expand All @@ -94,7 +91,7 @@ public static function singleSelect(string $group, string $key, array $options,
/**
* Create a new multi select setting
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group, key, options or default value fail to pass
* validation. The group, key and each option must only contain
* characters in the set [a-zA-Z0-9-_]. Each valid in the default
Expand All @@ -121,7 +118,7 @@ public static function multiSelect(string $group, string $key, array $options, a
* Ensure that there are overrides for each of the given identifiers for the
* the setting identified by the given group and key
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group or key fail to pass validation. The group and key
* must only contain characters in the set [a-zA-Z0-9-_].
* @throws \SoapBox\Settings\Exceptions\InvalidGroupException
Expand Down Expand Up @@ -157,7 +154,7 @@ public static function ensureHasOverride(string $group, string $key, Collection
* Update a setting definition for the given group and key and delete any
* invalid override values
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group or key fail to pass validation. The group and key
* must only contain characters in the set [a-zA-Z0-9-_].
* @throws \SoapBox\Settings\Exceptions\InvalidGroupException
Expand Down Expand Up @@ -194,7 +191,7 @@ public static function update(string $group, string $key, callable $callback): v
/**
* Delete a setting definition for the given group and key
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group or key fail to pass validation. The group and key
* must only contain characters in the set [a-zA-Z0-9-_].
* @throws \SoapBox\Settings\Exceptions\InvalidGroupException
Expand Down
10 changes: 5 additions & 5 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(Settings $settings)
* Ensure the setting collection for the given identifier in the given group
* is loaded in the cache
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group or identifier fail to pass validation. The group
* and identifier must only contain characters in the set
* [a-zA-Z0-9-_].
Expand All @@ -40,7 +40,7 @@ public function load(string $group, string $identifier): void
* Ensure the setting collections for the given identifiers in the given
* group are loaded in the cache
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group or identifiers fail to pass validation. The group
* and identifiers must only contain characters in the set
* [a-zA-Z0-9-_].
Expand All @@ -53,14 +53,13 @@ public function load(string $group, string $identifier): void
*/
public function loadMultiple(string $group, Collection $identifiers): void
{
KeyValidator::validate(array_merge([$group], $identifiers->all()));
$this->getMultiple($group, $identifiers);
}

/**
* Get the setting collection for the given identifier in the given group
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group or identifier fail to pass validation. The group
* and identifier must only contain characters in the set
* [a-zA-Z0-9-_].
Expand All @@ -81,7 +80,7 @@ public function get(string $group, string $identifier): Collection
/**
* Get all settings for the given identifiers in the given group
*
* @throws \Illuminate\Validation\ValidationException
* @throws \InvalidArgumentException
* When the group or identifiers fail to pass validation. The group
* and identifiers must only contain characters in the set
* [a-zA-Z0-9-_].
Expand Down Expand Up @@ -113,6 +112,7 @@ public function getMultiple(string $group, Collection $identifiers): Collection
*/
public function store(Setting $setting): Setting
{
KeyValidator::validate([$setting->getGroup(), $setting->getKey(), $setting->getIdentifier()]);
return $this->settings->store($setting);
}
}
6 changes: 0 additions & 6 deletions src/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ class Setting
/**
* Create a new Setting object
*
* @throws \Illuminate\Validation\ValidationException
* When the group, key or identifier fail to pass validation. These
* values must only contain characters in the set [a-zA-Z0-9-_].
*
* @param string $group
* @param string $key
* @param string $identifier
* @param mixed $value
*/
public function __construct(string $group, string $key, string $identifier, $value)
{
KeyValidator::validate([$group, $key, $identifier]);

$this->group = $group;
$this->key = $key;
$this->identifier = $identifier;
Expand Down
8 changes: 6 additions & 2 deletions src/Utilities/KeyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SoapBox\Settings\Utilities;

use Illuminate\Support\Facades\Validator;
use InvalidArgumentException;

class KeyValidator
{
Expand All @@ -19,6 +19,10 @@ public static function validate($keys): void
$keys = [$keys];
}

Validator::make(['keys' => $keys], ['keys.*' => 'alpha-dash'])->validate();
foreach ($keys as $key) {
if (!preg_match('/^[\pL\pM\pN_-]+$/u', $key)) {
throw new InvalidArgumentException();
}
}
}
}
26 changes: 13 additions & 13 deletions tests/Integration/Builders/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Integration\Builders;

use Tests\TestCase;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use SoapBox\Settings\Builders\Settings;
use SoapBox\Settings\Models\SettingValue;
Expand All @@ -12,7 +13,6 @@
use SoapBox\Settings\Exceptions\InvalidKeyException;
use SoapBox\Settings\Models\BooleanSettingDefinition;
use SoapBox\Settings\Exceptions\InvalidGroupException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use SoapBox\Settings\Models\MultiSelectSettingDefinition;
use SoapBox\Settings\Models\SingleSelectSettingDefinition;

Expand Down Expand Up @@ -318,37 +318,37 @@ public function itCanDeleteASettingDefinition()
/**
* @test
*/
public function ensureHasOverrideThrowsAValidationExceptionWhenTheGroupHasADot()
public function ensureHasOverrideThrowsAnInvalidArgumentExceptionWhenTheGroupHasADot()
{
$this->expectException(ValidationException::class);
$this->expectException(InvalidArgumentException::class);
Settings::ensureHasOverride('settings.wat', 'key', new Collection('1'));
}

/**
* @test
*/
public function ensureHasOverrideThrowsAValidationExceptionWhenTheKeyHasADot()
public function ensureHasOverrideThrowsAnInvalidArgumentExceptionWhenTheKeyHasADot()
{
$this->expectException(ValidationException::class);
$this->expectException(InvalidArgumentException::class);
Settings::ensureHasOverride('settings', 'invalid.key', new Collection('1'));
}

/**
* @test
*/
public function updateThrowsAValidationExceptionWhenTheGroupHasADot()
public function updateThrowsAnInvalidArgumentExceptionWhenTheGroupHasADot()
{
$this->expectException(ValidationException::class);
$this->expectException(InvalidArgumentException::class);
Settings::update('settings.wat', 'key', function () {
});
}

/**
* @test
*/
public function updateThrowsAValidationExceptionWhenTheKeyHasADot()
public function updateThrowsAnInvalidArgumentExceptionWhenTheKeyHasADot()
{
$this->expectException(ValidationException::class);
$this->expectException(InvalidArgumentException::class);
Settings::update('settings', 'invalid.key', function () {
});
}
Expand Down Expand Up @@ -377,18 +377,18 @@ public function updateThrowsInvalidKeyExceptionWhenTheKeyDoesNotExist()
/**
* @test
*/
public function deleteThrowsAValidationExceptionWhenTheGroupHasADot()
public function deleteThrowsnInvalidArgumentExceptionWhenTheGroupHasADot()
{
$this->expectException(ValidationException::class);
$this->expectException(InvalidArgumentException::class);
Settings::delete('settings.wat', 'key');
}

/**
* @test
*/
public function deleteThrowsAValidationExceptionWhenTheKeyHasADot()
public function deleteThrowsnInvalidArgumentExceptionWhenTheKeyHasADot()
{
$this->expectException(ValidationException::class);
$this->expectException(InvalidArgumentException::class);
Settings::delete('settings', 'invalid.key');
}

Expand Down
Loading

0 comments on commit dd80a1d

Please sign in to comment.