ServiceContract
in package
Uses
AuthTrait, RequestActionTrait, ValidationTrait
This is the main class all other services must extend.
It contains the basic methods that all services will need for authentication and request processing
Tags
Table of Contents
Properties
- $actionPermissions : array<string|int, mixed>
- An associative array of actions and their required permissions
- $actionsRequiringAuth : array<string|int, mixed>
- $actionsRequiringAuth : array<string|int, mixed>
- This array contains the actions that require authentication
- $authMessage : string|null
- This message will be displayed when the entire service requires authentication
- $deactivatedActions : array<string|int, mixed>
- $deactivatedActions : array<string|int, mixed>
- An array of actions that are deactivated for the current service
- $ip_pattern : string
- $password_pattern : string
- $phone_pattern : string
- $request : Request
- $request : Request
- $serviceRequiresAuth : bool
- $serviceRequiresAuth : bool
- If true, the entire service requires authentication
- $slug_pattern : string
- $throwsExceptions : bool
Methods
- __construct() : mixed
- allShouldBe() : mixed
- asDomain() : bool|int
- asEmail() : bool|int
- Validates emails of all formats
- asInternationalPhone() : bool|int
- Will only validate international numbers if the code is provided, otherwise, will validate local only
- asIp() : bool|int
- asMac() : bool|int
- asNumber() : bool
- asNumeric() : bool|int
- asNumericInt() : bool|int
- asPassword() : bool|int
- Validates the rules as follows - At least one integer - At least one lowercase alpha letter - At least one Uppercase alpha letter - At least one special character
- asSlug() : bool|int
- asUrl() : bool|int
- auth() : ContextUserObject|null
- This method holds the currently logged in user object
- authExtraHas() : bool
- Checks if the auth extra data contains a key or not
- can() : bool
- Like CanAny but only check for one permission at a time
- canAll() : bool
- Similar to canAny only that this checks if the user has all the passed permissions
- canAny() : bool
- This method is used to check if the currently logged in user has the required permission to access a resource.
- getAuthExtraByKey() : mixed|null
- Returns the auth extra data by key
- mustAuthenticate() : bool
- This method ensures that only authenticated users can access a resource
- requires() : void
- This checks if the required fields are present in the request otherwise it throws an exception
- validate() : bool|int
- Use this to cover scenarios this contract does not cover
- _validateFilter() : int|bool
- Internal validator based on PHP filter_var validations
- check_one() : void
- Checks if a single field is present in the request data and is not null or empty
- shouldBe() : bool
Properties
$actionPermissions
An associative array of actions and their required permissions
public
array<string|int, mixed>
$actionPermissions
= []
$actionsRequiringAuth
public
array<string|int, mixed>
$actionsRequiringAuth
An array of actions that require authentication
$actionsRequiringAuth
This array contains the actions that require authentication
public
array<string|int, mixed>
$actionsRequiringAuth
= []
$authMessage
This message will be displayed when the entire service requires authentication
public
string|null
$authMessage
= null
$deactivatedActions
public
array<string|int, mixed>
$deactivatedActions
An array of actions that are deactivated for the current service
$deactivatedActions
An array of actions that are deactivated for the current service
public
array<string|int, mixed>
$deactivatedActions
= []
$ip_pattern
public
string
$ip_pattern
= "/^(\\d{1,3}\\.){3}\\d{1,3}\$/"
$password_pattern
public
string
$password_pattern
= "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*(_|[^\\w])).+\$/"
$phone_pattern
public
string
$phone_pattern
= "/^[+]{1}(?:[0-9\\-\\(\\)\\/.]\\s?){6,15}[0-9]{1}\$/"
$request
public
Request
$request
The request object
$request
public
Request
$request
The request object
$serviceRequiresAuth
public
bool
$serviceRequiresAuth
If true, the entire service requires authentication
$serviceRequiresAuth
If true, the entire service requires authentication
public
bool
$serviceRequiresAuth
= false
$slug_pattern
public
string
$slug_pattern
= "/^[a-z0-9-]+\$/"
$throwsExceptions
private
bool
$throwsExceptions
= true
Methods
__construct()
public
__construct([mixed $throws = true ]) : mixed
Parameters
- $throws : mixed = true
allShouldBe()
public
allShouldBe(iterable<string|int, mixed> $value, string $expected) : mixed
Parameters
- $value : iterable<string|int, mixed>
- $expected : string
Tags
asDomain()
public
asDomain(mixed $domain[, string|null $regex = null ]) : bool|int
Parameters
- $domain : mixed
-
The domain we are testing its validity
- $regex : string|null = null
-
Your custom regular expression we can depend on instead.
Tags
Return values
bool|intasEmail()
Validates emails of all formats
public
asEmail(string $email) : bool|int
Parameters
- $email : string
-
The email address we're currently testing in the core.
Tags
Return values
bool|intasInternationalPhone()
Will only validate international numbers if the code is provided, otherwise, will validate local only
public
asInternationalPhone(string $phone[, string|null $code = null ][, string|null $regex = null ]) : bool|int
Parameters
- $phone : string
-
The phone number we are testing
- $code : string|null = null
-
International country that you want to check against
- $regex : string|null = null
-
Your custom regular expression we can depend on instead.
Tags
Return values
bool|intasIp()
public
asIp(mixed $ip[, string|null $regex = null ]) : bool|int
Parameters
- $ip : mixed
-
The Ip address we're checking.
- $regex : string|null = null
-
Your custom regular expression we can depend on instead.
Tags
Return values
bool|intasMac()
public
asMac(mixed $mac[, string|null $regex = null ]) : bool|int
Parameters
- $mac : mixed
-
The mac address we are checking
- $regex : string|null = null
-
Your custom regular expression we can depend on instead.
Tags
Return values
bool|intasNumber()
public
asNumber(mixed $number) : bool
Parameters
- $number : mixed
-
The number we are checking
Tags
Return values
boolasNumeric()
public
asNumeric(mixed $number) : bool|int
Parameters
- $number : mixed
Tags
Return values
bool|intasNumericInt()
public
asNumericInt(mixed $number) : bool|int
Parameters
- $number : mixed
Tags
Return values
bool|intasPassword()
Validates the rules as follows - At least one integer - At least one lowercase alpha letter - At least one Uppercase alpha letter - At least one special character
public
asPassword(mixed $password[, string|null $regex = null ]) : bool|int
Parameters
- $password : mixed
-
The password string we are testing, must be raw, not hashed
- $regex : string|null = null
-
Your custom regular expression we can depend on instead.
Tags
Return values
bool|intasSlug()
public
asSlug(mixed $slug[, string|null $regex = null ]) : bool|int
Parameters
- $slug : mixed
-
The slug string we are testing
- $regex : string|null = null
-
Your custom regular expression we can depend on instead.
Tags
Return values
bool|intasUrl()
public
asUrl(mixed $url[, string|null $regex = null ]) : bool|int
Parameters
- $url : mixed
-
The url string we are testing
- $regex : string|null = null
-
Your custom regular expression we can depend on instead.
Tags
Return values
bool|intauth()
This method holds the currently logged in user object
public
auth() : ContextUserObject|null
Return values
ContextUserObject|null —The currently logged in user object or null if no user is logged in
authExtraHas()
Checks if the auth extra data contains a key or not
public
authExtraHas(string $key) : bool
Parameters
- $key : string
Return values
bool —Returns true if the key is present in the authExtra data else returns false
can()
Like CanAny but only check for one permission at a time
public
can(string $permission[, string|null $message = 'You do not have access to this resource' ]) : bool
Parameters
- $permission : string
-
The permission to check for
- $message : string|null = 'You do not have access to this resource'
-
The message to be returned if the user does not have the required permission
Tags
Return values
bool —Returns true if the user has the required permission
canAll()
Similar to canAny only that this checks if the user has all the passed permissions
public
canAll(array<string|int, mixed> $permissions[, string|null $message = 'You do not have access to this resource' ]) : bool
Parameters
- $permissions : array<string|int, mixed>
-
The permissions to check for
- $message : string|null = 'You do not have access to this resource'
-
The message to be returned if the user does not have the required permission
Tags
Return values
bool —Returns true if the user has the required permission, else returns a BaseResponse object
canAny()
This method is used to check if the currently logged in user has the required permission to access a resource.
public
canAny(string|array<string|int, mixed> $permission[, string|null $message = 'You do not have access to this resource' ]) : bool
It checks if user is logged in first, then checks if there are any permissions set for the user.
And finally checks if the set permissions contain the given permission we are looking for.
Parameters
- $permission : string|array<string|int, mixed>
-
The permission to check for
- $message : string|null = 'You do not have access to this resource'
-
The message to be returned if the user does not have the required permission
Tags
Return values
bool —Returns true if the user has the required permission
getAuthExtraByKey()
Returns the auth extra data by key
public
getAuthExtraByKey(string $key) : mixed|null
Parameters
- $key : string
Return values
mixed|null —Returns the value of the key if it exists else returns null
mustAuthenticate()
This method ensures that only authenticated users can access a resource
public
mustAuthenticate([string|null $message = 'You must be authenticated to access this resource' ]) : bool
Parameters
- $message : string|null = 'You must be authenticated to access this resource'
-
Use this to override the default message
Tags
Return values
boolrequires()
This checks if the required fields are present in the request otherwise it throws an exception
public
requires([array<string|int, mixed>|string $required = [] ]) : void
Parameters
- $required : array<string|int, mixed>|string = []
-
The fields that are required
Tags
validate()
Use this to cover scenarios this contract does not cover
public
validate(string $regex, mixed $value[, string|null $message = 'Invalid data' ]) : bool|int
Parameters
- $regex : string
-
- The regular expression to check against
- $value : mixed
-
- The value to check
- $message : string|null = 'Invalid data'
-
- The message to throw if the value is invalid and we are in the exceptions mode
Tags
Return values
bool|int_validateFilter()
Internal validator based on PHP filter_var validations
private
_validateFilter(mixed $value, $filterType[, string $message = 'Invalid Data' ]) : int|bool
Parameters
Tags
Return values
int|boolcheck_one()
Checks if a single field is present in the request data and is not null or empty
private
static check_one( $field, $data) : void
Parameters
Tags
shouldBe()
private
shouldBe(mixed $value, mixed $expected) : bool
Parameters
- $value : mixed
- $expected : mixed