Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow guzzle 7 #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
}
],
"require": {
"php": ">=5.6.0",
"guzzlehttp/guzzle": "^6.2",
"silverstripe/framework": "^4@dev"
"php": ">=8.1",
"guzzlehttp/guzzle": "^7",
"silverstripe/framework": "^5"
},
"autoload": {
"psr-4": {
Expand Down
120 changes: 59 additions & 61 deletions src/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,56 +40,54 @@ abstract class Backend
use Configurable;
use Extensible;
use Injectable;

/**
* An array of required JavaScript files.
*
* @var array
* @config
*/
private static $required_js = [];

/**
* An array of required CSS files.
*
* @var array
* @config
*/
private static $required_css = [];

/**
* The attribute configuration used for this backend.
*
* @var array
* @config
*/
private static $attribute = [];

/**
* The attribute mappings defined for this backend.
*
* @var array
* @config
*/
private static $mappings = [];

/**
* The validator frontend associated with this backend.
*
* @var Validator
*/
protected $frontend;

/**
* Constructs the object upon instantiation.
*/
public function __construct()
{
// Construct Extension Instances:

$this->constructExtensions();

}

/**
* Defines the value of the frontend attribute.
*
Expand All @@ -100,10 +98,10 @@ public function __construct()
public function setFrontend(Validator $frontend)
{
$this->frontend = $frontend;

return $this;
}

/**
* Answers the value of the Frontend attribute.
*
Expand All @@ -113,7 +111,7 @@ public function getFrontend()
{
return $this->frontend;
}

/**
* Answers an array of the validator classes for the given form.
*
Expand All @@ -125,7 +123,7 @@ public function getClassesForForm(Form $form)
{
return [$this->getHTMLClass()];
}

/**
* Answers an array of the validator attributes for the given form.
*
Expand All @@ -139,7 +137,7 @@ public function getAttributesForForm(Form $form)
'data-client-side' => ($this->frontend->getClientSide() ? 'true' : 'false')
];
}

/**
* Answers an array of the validator attributes for the given form field.
*
Expand All @@ -151,7 +149,7 @@ public function getAttributesForField(FormField $field)
{
return [];
}

/**
* Answers the HTML class name for the receiver.
*
Expand All @@ -161,7 +159,7 @@ public function getHTMLClass()
{
return strtolower(ClassInfo::shortName(static::class));
}

/**
* Initialises the validator backend (with extension hooks).
*
Expand All @@ -170,18 +168,18 @@ public function getHTMLClass()
public function doInit()
{
// Trigger Before Init Hook:

$this->extend('onBeforeInit');

// Perform Initialisation:

$this->init();

// Trigger After Init Hook:

$this->extend('onAfterInit');
}

/**
* Answers the appropriate validator attribute name for the given mapping name and arguments.
*
Expand All @@ -193,14 +191,14 @@ public function doInit()
public function attr($name, $args = [])
{
$attr = $this->hasMapping($name) ? $this->getMapping($name) : $name;

if (func_num_args() > 1) {
return $this->prefix(vsprintf($attr, (array) $args));
}

return $this->prefix($attr);
}

/**
* Prefixes the given attribute name (if required).
*
Expand All @@ -211,16 +209,16 @@ public function attr($name, $args = [])
public function prefix($name)
{
if ($prefix = $this->config()->attribute['prefix']) {

if (strpos($name, $prefix) !== 0) {
return sprintf('%s%s', $prefix, $name);
}

}

return $name;
}

/**
* Answers the attribute mapping with the given name.
*
Expand All @@ -231,14 +229,14 @@ public function prefix($name)
public function getMapping($name)
{
if ($mappings = $this->getMappings()) {

if (isset($mappings[$name])) {
return $mappings[$name];
}

}
}

/**
* Answers true if an attribute mapping exists with the given name.
*
Expand All @@ -250,7 +248,7 @@ public function hasMapping($name)
{
return (boolean) $this->getMapping($name);
}

/**
* Answers the configured attribute mappings for the receiver.
*
Expand All @@ -260,7 +258,7 @@ public function getMappings()
{
return $this->config()->mappings;
}

/**
* Answers the default attribute name from configuration.
*
Expand All @@ -270,7 +268,7 @@ public function getDefaultAttribute()
{
return $this->config()->attribute['default'];
}

/**
* Applies configuration to the provided validator rule.
*
Expand All @@ -281,24 +279,24 @@ public function getDefaultAttribute()
public function configureRule(Rule $rule)
{
if ($config = $this->getRuleConfig($rule)) {

if (isset($config['type'])) {
$rule->setType($config['type']);
}

if (isset($config['format'])) {
$rule->setFormat($config['format']);
}

if (isset($config['attribute'])) {
$rule->setAttribute($config['attribute']);
}

}

return $rule;
}

/**
* Answers an array of JavaScript files required by the receiver.
*
Expand All @@ -307,12 +305,12 @@ public function configureRule(Rule $rule)
public function getRequiredJS()
{
$js = $this->config()->required_js;

$this->extend('updateRequiredJS', $js);

return $js;
}

/**
* Answers an array of CSS files required by the receiver.
*
Expand All @@ -321,12 +319,12 @@ public function getRequiredJS()
public function getRequiredCSS()
{
$css = $this->config()->required_css;

$this->extend('updateRequiredCSS', $css);

return $css;
}

/**
* Loads the CSS and scripts required by the receiver.
*
Expand All @@ -335,18 +333,18 @@ public function getRequiredCSS()
public function loadRequirements()
{
// Load Required CSS:

foreach ($this->getRequiredCSS() as $css) {
Requirements::css($css);
}

// Load Required JavaScript:

foreach ($this->getRequiredJS() as $js) {
Requirements::javascript($js);
}
}

/**
* Initialises the validator backend.
*
Expand All @@ -355,10 +353,10 @@ public function loadRequirements()
protected function init()
{
// Load Requirements:

$this->loadRequirements();
}

/**
* Flattens the given array of attributes.
*
Expand All @@ -369,16 +367,16 @@ protected function init()
protected function flatten($attributes)
{
foreach ($attributes as $name => $value) {

if (is_array($value)) {
$attributes[$name] = implode(' ', array_filter($value));
}

}

return $attributes;
}

/**
* Answers the configuration array for the provided rule.
*
Expand All @@ -389,13 +387,13 @@ protected function flatten($attributes)
protected function getRuleConfig(Rule $rule)
{
if ($rules = $this->config()->rules) {

if (isset($rules[get_class($rule)])) {
return $rules[get_class($rule)];
}

}

return [];
}
}
Loading