phramework/jsonapi doc
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Phramework
    • JSONAPI
      • Controller
        • POST
      • Model
      • Viewers

Classes

  • Controller
  • Fields
  • Filter
  • FilterAttribute
  • FilterJSONAttribute
  • Model
  • Page
  • Relationship
  • RelationshipResource
  • Resource
  • Sort
  • Util
  • ValidationModel

Class Filter

Filter helper methods

Namespace: Phramework\JSONAPI
License: Apache-2.0
Author: Xenofon Spafaridis nohponex@gmail.com
Since: 1.0.0
Located at Filter.php

Methods summary

public
# __construct( array $primary = [], object|null $relationships = null, array $filterAttributes = [] )

Filter constructor.

Filter constructor.

Parameters

$primary
$relationships
$filterAttributes

Throws

Exception

Example

$filter = new Filter(
    [1, 2],
    (object) [
        'author'  => [1],
        'comment' => [1, 2, 3],
        'tag'     => ['blog']
    ],
    [
        new FilterAttribute('title', Operator::OPERATOR_LIKE, 'blog')
    ]
);

$filter = new Filter(
    [1, 2],
    null,
    [
        new FilterAttribute('title', Operator::OPERATOR_LIKE, 'blog'),
        new FilterJSONAttribute('meta', 'keyword', Operator::OPERATOR_EQUAL, 'blog')
    ]
);

public
# validate( string $modelClass )

Validate against a resource model class

Validate against a resource model class

Parameters

$modelClass
Resource model class

Throws

Phramework\Exceptions\RequestException
Exception
Phramework\Exceptions\IncorrectParametersException
When filter for primary id attribute is incorrect
Phramework\Exceptions\IncorrectParametersException
When filter for relationship is incorrect

Example

$filter = new Filter([1, 2, 3]);

$filter->validate(Article::class);

Todo

add relationship validator
public static Phramework\JSONAPI\Filter|null
# parseFromParameters( object $parameters, string $modelClass )

Parameters

$parameters
Request parameters
$modelClass

Returns

Phramework\JSONAPI\Filter|null

Throws

Phramework\Exceptions\RequestException
Exception
Phramework\Exceptions\IncorrectParametersException

Example

$filter = Filter::parseFromParameters(
    (object) [
        'filter' => [
            'article'   => '1, 2', //primary filter (parsed from URL's ?filter[article]=1, 2)
            'tag'       => '4, 5, 7', //relationship filter (parsed from URL's ?filter[tag]=4, 5, 7)
            'creator'   => '1', //relationship  filter(parsed from URL's ?filter[creator]=1)
            'status'    => ['ENABLED', 'INACTIVE'], //multiple filters
            'title'     => [
                Operator::OPERATOR_LIKE . 'blog',
                Operator::OPERATOR_NOT_LIKE . 'welcome'
            ], //multiple filters on title (parsed from URL's ?filter[title][]=~~blog&filter[title][]=!~~welcome)
            'updated'   => Operator::OPERATOR_NOT_ISNULL,
            'meta.keywords' => 'blog' //JSON attribute filter
        ]
    ], //Request parameters object
    Article::class
);

Todo

allow strings and integers as id
Todo use filterValidation model for relationships
allowed operator for JSON properties
add support for operators of class in, parsing input using explode , (as array)
public mixed
# __get( string $name )

Parameters

$name

Returns

mixed

Throws

Exception

Properties summary

protected string[] $primary

Example

[1, 2]
#
protected object $relationships

Example

(object) [
    'author'  => [1],
    'comment' => [1, 2, 3],
    'tag'     => ['blog']
]

#
protected Phramework\JSONAPI\FilterAttribute[]|Phramework\JSONAPI\FilterJSONAttribute[] $attributes
#

Magic properties

public read-only object $relationships
phramework/jsonapi doc API documentation generated by ApiGen