Menu

## ServiceContract
in package
Uses AuthTrait, RequestActionTrait, ValidationTrait

AbstractYes
ServiceContract.php : 25

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

author

  Jet - ezrajet9@gmail.com

### 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

ServiceContract.php : 44

An associative array of actions and their required permissions

`public array<string|int, mixed> $actionPermissions = []`

#### $actionsRequiringAuth

ServiceContract.php : 0
`public array<string|int, mixed> $actionsRequiringAuth`

An array of actions that require authentication

#### $actionsRequiringAuth

ServiceContract.php : 50

This array contains the actions that require authentication

`public array<string|int, mixed> $actionsRequiringAuth = []`

#### $authMessage

ServiceContract.php : 62

This message will be displayed when the entire service requires authentication

`public string|null $authMessage = null`

#### $deactivatedActions

ServiceContract.php : 0
`public array<string|int, mixed> $deactivatedActions`

An array of actions that are deactivated for the current service

#### $deactivatedActions

ServiceContract.php : 38

An array of actions that are deactivated for the current service

`public array<string|int, mixed> $deactivatedActions = []`

#### $ip_pattern

ValidationTrait.php : 12
`public string $ip_pattern = "/^(\\d{1,3}\\.){3}\\d{1,3}\$/"`

#### $password_pattern

ValidationTrait.php : 11
`public string $password_pattern = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*(_|[^\\w])).+\$/"`

#### $phone_pattern

ValidationTrait.php : 10
`public string $phone_pattern = "/^[+]{1}(?:[0-9\\-\\(\\)\\/.]\\s?){6,15}[0-9]{1}\$/"`

#### $request

ServiceContract.php : 0
`public Request $request`

The request object

#### $request

ServiceContract.php : 32
`public Request $request`

The request object

#### $serviceRequiresAuth

ServiceContract.php : 0
`public bool $serviceRequiresAuth`

If true, the entire service requires authentication

#### $serviceRequiresAuth

ServiceContract.php : 56

If true, the entire service requires authentication

`public bool $serviceRequiresAuth = false`

#### $slug_pattern

ValidationTrait.php : 13
`public string $slug_pattern = "/^[a-z0-9-]+\$/"`

#### $throwsExceptions

ValidationTrait.php : 14
`private bool $throwsExceptions = true`

### Methods

#### __construct()

ValidationTrait.php : 16
`public __construct([mixed $throws = true ]) : mixed`

##### Parameters

$throws : mixed = true

#### allShouldBe()

ValidationTrait.php : 220
`public allShouldBe(iterable<string|int, mixed> $value, string $expected) : mixed`

##### Parameters

$value : iterable<string|int, mixed>
$expected : string

##### Tags

throws
  InvalidDataException

#### asDomain()

ValidationTrait.php : 165
`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

throws
  InvalidDataException

##### Return values

bool|int

#### asEmail()

ValidationTrait.php : 43

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

throws
  InvalidDataException

##### Return values

bool|int

#### asInternationalPhone()

ValidationTrait.php : 57

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

throws
  InvalidDataException

##### Return values

bool|int

#### asIp()

ValidationTrait.php : 136
`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

throws
  InvalidDataException

##### Return values

bool|int

#### asMac()

ValidationTrait.php : 150
`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

throws
  InvalidDataException

##### Return values

bool|int

#### asNumber()

ValidationTrait.php : 92
`public asNumber(mixed $number) : bool`

##### Parameters

$number : mixed

  The number we are checking

##### Tags

throws
  InvalidDataException

##### Return values

bool

#### asNumeric()

ValidationTrait.php : 100
`public asNumeric(mixed $number) : bool|int`

##### Parameters

$number : mixed

##### Tags

throws
  InvalidDataException

##### Return values

bool|int

#### asNumericInt()

ValidationTrait.php : 108
`public asNumericInt(mixed $number) : bool|int`

##### Parameters

$number : mixed

##### Tags

throws
  InvalidDataException

##### Return values

bool|int

#### asPassword()

ValidationTrait.php : 82

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

throws
  InvalidDataException

##### Return values

bool|int

#### asSlug()

ValidationTrait.php : 186
`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

example

      $slug = 'fsjkfjshfsjk-skdhfkjdfsj-skdjfhjskdf'; // valid slug
      $slug2 = 'sfksdfsdskljfhsdhjkfhsdsfsdfsfsd'; // valid slug
      $slug3 = 'dkfl ksjfhsdk/skjdfsk%'; // invalid slug

throws
  InvalidDataException

##### Return values

bool|int

#### asUrl()

ValidationTrait.php : 122
`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

throws
  InvalidDataException

##### Return values

bool|int

#### auth()

AuthTrait.php : 126

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()

AuthTrait.php : 152

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()

AuthTrait.php : 73

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

throws
  UserUnauthenticatedException
throws
  UserUnauthorizedException

##### Return values

bool —

Returns true if the user has the required permission

#### canAll()

AuthTrait.php : 100

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

throws
  UserUnauthenticatedException

  If the user is not authenticated

throws
  UserUnauthorizedException

  If the user does not have the required permission

##### Return values

bool —

Returns true if the user has the required permission, else returns a BaseResponse object

#### canAny()

AuthTrait.php : 33

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

throws
  UserUnauthenticatedException
throws
  UserUnauthorizedException

##### Return values

bool —

Returns true if the user has the required permission

#### getAuthExtraByKey()

AuthTrait.php : 167

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()

AuthTrait.php : 138

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

throws
  UserUnauthenticatedException

##### Return values

bool

#### requires()

RequestActionTrait.php : 34

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

throws
  FailedRequiredException

  if a required field is not present in the request

#### validate()

ValidationTrait.php : 29

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

throws
  InvalidDataException

##### Return values

bool|int

#### _validateFilter()

ValidationTrait.php : 200

Internal validator based on PHP filter_var validations

`private _validateFilter(mixed $value, $filterType[, string $message = 'Invalid Data' ]) : int|bool`

##### Parameters

$value : mixed
$filterType :
$message : string = 'Invalid Data'

##### Tags

throws
  InvalidDataException

##### Return values

int|bool

#### check_one()

RequestActionTrait.php : 22

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

$field :
$data :

##### Tags

throws
  FailedRequiredException

#### shouldBe()

ValidationTrait.php : 212
`private shouldBe(mixed $value, mixed $expected) : bool`

##### Parameters

$value : mixed
$expected : mixed

##### Tags

throws
  InvalidDataException

##### Return values

bool

## Search results