项目作者: korchasa

项目描述 :
Simple pattern matching for PHP
高级语言: PHP
项目地址: git://github.com/korchasa/matcho.git
创建时间: 2017-08-05T18:43:41Z
项目社区:https://github.com/korchasa/matcho

开源协议:MIT License

下载


Simple pattern matching for PHP

Stable Version
Unstable Version
Build Status

Install:

  1. composer require korchasa/matcho

Usage in tests:

Use *** for “any value”. Connect asserts by use AssertMatchedTrait

  1. <?php
  2. use korchasa\Vhs\AssertMatchedTrait;
  3. use PHPUnit\Framework\TestCase;
  4. class IntegrationTest extends TestCase
  5. {
  6. use AssertMatchedTrait;
  7. public function testResponseJson()
  8. {
  9. $this->assertJsonMatched(
  10. '{
  11. "baz": {
  12. "value": 1
  13. },
  14. "items": [
  15. {
  16. "a": "b***",
  17. "c": 2
  18. },
  19. "***"
  20. ]
  21. }',
  22. $this->server()->call()->responseJson()
  23. );
  24. /**
  25. Given value of `items.0.c` not match pattern `2`
  26. --- Pattern
  27. +++ Actual
  28. @@ @@
  29. -2
  30. +22
  31. */
  32. }
  33. public function testArray()
  34. {
  35. $this->assertArrayMatched(
  36. [
  37. "foo" => "somestring***", // check string pattern
  38. "bar" => "***", // check only presence
  39. "baz" => 42 // check presence and value
  40. ],
  41. $complexArray
  42. );
  43. /**
  44. Given value has no key `baz`
  45. --- Pattern
  46. +++ Actual
  47. @@ @@
  48. array (
  49. - 'foo' => 'something***',
  50. - 'baz' => "***",
  51. + 'foo' => 'something2',
  52. */
  53. }
  54. public function testString()
  55. {
  56. $this->assertStringMatched('cu***mber', $somestring);
  57. /**
  58. Given value not match pattern
  59. --- Pattern
  60. +++ Actual
  61. @@ @@
  62. -cu***mber
  63. +kucumber
  64. */
  65. }
  66. }

Usage in business logic:

Use *** for “any value”.

Functions or class:

  1. match_array([ 'foo' => [ 'any' => '***' ] ], $someArray);
  2. Match::array([ 'foo' => [ 'any' => '***' ] ], $someArray);

Arrays:

  1. $someArray = [ 'foo' => [ 'any' => 11 ] ];
  2. Match::array([ 'foo' => [ 'any' => '***' ]], $someArray); //true
  3. Match::array([ 'foo' => [], $someArray); //true
  4. Match::array([ 'foo' => [ 'not_any' => 13 ]], $someArray); //false (missed key foo.not_any)
  5. Match::array([ 'foo' => [ 'any' => 12 ]], $someArray); //false (not equals values foo.any)

JSON:

  1. $someJson = '{
  2. "foo": "bar",
  3. "baz": { "value": 1 },
  4. "items": [
  5. { "a": "b", "c": 2 },
  6. { "z": "x", "c": 3 }
  7. ]
  8. }';
  9. Match::json('{
  10. "foo": "bar",
  11. "baz": "***",
  12. "items": [
  13. "***",
  14. { "z": "x", "c": 3 }
  15. ]
  16. }',
  17. $someJson
  18. ); //true

String:

  1. Match::string('12345***0ab***f', '1234567890abcdef'); //true

Custom “any symbol”:

  1. Match::string('12345%%%0ab%%%f', '1234567890abcdef', '%%%'); //true

Default values vs *:

  1. Match::string('123450ab**>cde<**f', '1234567890abcdef'); //true
  2. Match::defaultString('123450ab**>cde<**f'); //123450abcdef
  3. Match::defaultString('**>foo<**'); //foo
  4. Match::defaultString('***1'); //1

Case (not implemented yet):

  1. $processingResult = Match::stringCase('user@company2.com')
  2. ->case('***@company1.com', function($val) { return $this->processCompany1Email($val); })
  3. ->case('***@company2.com', function($val) { return $this->processCompany2Email($val); })
  4. ->default(function ($val) { return $this->processUsualEmails($val); });