承接 nebkam/odm-search-param 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nebkam/odm-search-param

最新稳定版本:6.0.9

Composer 安装命令:

composer require nebkam/odm-search-param

包简介

README 文档

README

Latest Stable Version Tests

ODM Search Param

Rationale

  • you use Doctrine MongoDB ODM
  • you need some search functionality, that can be expressed through the QueryBuilder or the MatchPhase of the Aggregation Builder
  • you have a class that holds the search parameters (i.e. a domain class in Symfony)
  • you don't want to manually map the search parameters to builder statements

Usage

  1. Search filter class can be any class with public properties
  2. Properties should be marked with #[SearchParam] attribute
  3. Use the SearchParamParser::parse with a QueryBuilder|MatchStage instance to build the query on

Examples

Verbatim (String and Bool types)

class SearchFilter
{
    #[SearchParam(type: SearchParamType::String)]
    public string $name;
}

Builds the query to:

$builder->field('name')->equals($propertyValue);

Using different property names

class SearchFilter
{
    #[SearchParam(type: SearchParamType::Exists, field: 'images')]
    public bool $hasImages;
}

Builds the query to:

$builder->field('images')->exists(true);

Type casting (Int type)

class SearchFilter
{
    #[SearchParam(type: SearchParamType::Int)]
    public $age;
}

Builds the query to:

$builder->field('age')->equals((int) $propertyValue);

Type casting with arrays (IntArray and StringArray types)

class SearchFilter
{
    /**
    * @var int[] 
    */
    #[SearchParam(type: SearchParamType::IntArray)]
    public array $grades;
}

Builds the query to:

$builder->field('grades')->in($propertyValuesAllCastedToInt);

Using backing values from enums (StringEnum and IntEnum types)

class SearchFilter
{
    #[SearchParam(type: SearchParamType::StringEnum)]
    public SideOfTheWorldEnum $sideOfTheWorld;
}

Builds the query to:

$builder->field('sideOfTheWorld')->equals($propertyValue->value);

Using backing values from enum arrays (StringEnumArray and IntEnumArray types)

class SearchFilter
{
    /**
    * @var SideOfTheWorldEnum[]
    */
    #[SearchParam(type: SearchParamType::StringEnumArray)]
    public array $sidesOfTheWorld;
}

Builds the query to:

$builder->field('sideOfTheWorld')->in($backingValuesOfPropertyValue);

Querying by range (RangeInt, RangeInt and RangeFloat types)

class SearchFilter
{
    #[SearchParam(type: SearchParamType::RangeInt, direction: SearchParamDirection::From)]
    public int $price;
}

Builds the query to:

$builder->field('price')->gte((int) $propertyValue);

Custom query building by specifying the callable

class SearchFilter
{
    #[SearchParam(type: SearchParamType::Callable, callable: [SomeClass::class, 'setStatus'])]
    public string $status;
}

class SomeClass
{
    public static function setStatus(Builder|MatchStage $builder, $value, $filter)
    {
    // Call $builder methods to build the query
    }
}

Testing

  • run docker.sh

统计信息

  • 总下载量: 1.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-04