Skip to content

Commit 6a19b2e

Browse files
committed
Adding a convenient JmesPath\search() function
1 parent 75240d6 commit 6a19b2e

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Slice syntax is now properly validated (i.e., colons are followed by the
1616
appropriate value).
1717
* Lots of code cleanup and performance improvements.
18+
* Added a convenient `JmesPath\search()` function.
1819

1920
## 1.1.1 - 2014-10-08
2021

README.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using the ``mtdowling/jmespath.php`` package.
2222
]
2323
];
2424
25-
JmesPath\Env::search($expression, $data);
25+
JmesPath\search($expression, $data);
2626
// Returns: [1, 2, 3]
2727
2828
- `JMESPath documentation <http://jmespath.readthedocs.org/en/latest/>`_
@@ -32,22 +32,26 @@ using the ``mtdowling/jmespath.php`` package.
3232
PHP Usage
3333
=========
3434

35-
The ``JmesPath\Env::search`` function can be used in most cases when using the
35+
The ``JmesPath\search`` function can be used in most cases when using the
3636
library. This function utilizes a JMESPath runtime based on your environment.
3737
The runtime utilized can be configured using environment variables and may at
3838
some point in the future automatically utilize a C extension if available.
3939

4040
.. code-block:: php
4141
42-
$result = \JmesPath\Env::search($expression, $data);
42+
$result = JmesPath\search($expression, $data);
43+
44+
// or, if you require PSR-4 compliance.
45+
$result = JmesPath\Env::search($expression, $data);
4346
4447
Runtimes
4548
--------
4649

4750
jmespath.php utilizes *runtimes*. There are currently two runtimes:
4851
AstRuntime and CompilerRuntime.
4952

50-
AstRuntime is utilized by ``JmesPath\Env::search()`` by default.
53+
AstRuntime is utilized by ``JmesPath\search()`` and ``JmesPath\Env::search()``
54+
by default.
5155

5256
AstRuntime
5357
~~~~~~~~~~
@@ -92,7 +96,7 @@ before executing them (for example, server-side applications).
9296
Environment Variables
9397
^^^^^^^^^^^^^^^^^^^^^
9498

95-
You can utilize the CompilerRuntime in ``JmesPath\Env::search()`` by setting
99+
You can utilize the CompilerRuntime in ``JmesPath\search()`` by setting
96100
the ``JP_PHP_COMPILE`` environment variable to "on" or to a directory
97101
on disk used to store cached expressions.
98102

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"autoload": {
2424
"psr-4": {
2525
"JmesPath\\": "src/"
26-
}
26+
},
27+
"files": ["src/JmesPath.php"]
2728
},
2829

2930
"bin": ["bin/jp.php"],

src/JmesPath.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace JmesPath;
3+
4+
/**
5+
* Returns data from the input array that matches a JMESPath expression.
6+
*
7+
* @param string $expression Expression to search.
8+
* @param mixed $data Data to search.
9+
*
10+
* @return mixed|null
11+
*/
12+
function search($expression, $data)
13+
{
14+
return Env::search($expression, $data);
15+
}

tests/EnvTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ public function testSearchesInput()
99
$this->assertEquals(123, \JmesPath\Env::search('foo', $data));
1010
$this->assertEquals(123, \JmesPath\Env::search('foo', $data));
1111
}
12+
13+
public function testSearchesWithFunction()
14+
{
15+
$data = array('foo' => 123);
16+
$this->assertEquals(123, \JmesPath\search('foo', $data));
17+
}
1218
}

0 commit comments

Comments
 (0)