Skip to content

Commit fdc2b5f

Browse files
committed
Add documentation for string.classes
1 parent a688755 commit fdc2b5f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog #
22

3+
## v2.4.0 (2018-07-??) ##
4+
5+
* Added `string.classes` option, which allows to define an array of classes or
6+
namespaces to encode using the `::class` format, when encountered as strings
7+
* Added `string.imports` options, which allows to define the used imports to write
8+
the `::class` format strings using shorter imported notation
9+
* Support for HHVM has been dropped, as HHVM no longer aims for PHP compatibility
10+
* Added travis builds for PHP 7.2
11+
* Change some rules in the used coding standard
12+
313
## v2.3.0 (2017-07-15) ##
414

515
* Added `string.utf8` option which causes the string encoder to escape all

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,23 @@ apply to following calls.
201201
multibyte UTF-8 characters in strings are encoded using the PHP7 unicode
202202
code point syntax. Note that this syntax does not work in PHP versions
203203
earlier than 7.0.
204+
205+
* **string.classes** : &lt;string[]&gt; ([])<br>
206+
Defines a list of classes or class namespace prefixes for classes that
207+
can be encoded using the class resolution operator `::class` when
208+
encountered in strings. e.g. providing the value `['Riimu\\']` would encode
209+
all strings that look like class names in the `Riimu` namespace like
210+
`Riimu\Kit\PHPEncoder::class`.
211+
212+
* **string.imports** : &lt;string[]&gt; ([])<br>
213+
List of imports used in the resulting code file, which allows class names
214+
to be written using shorter format. The list should be an associative array
215+
with the namespace or class as the key and the used name as the value. Use
216+
empty string to indicate the current namespace.
217+
218+
For example, if the resulting file would have `namespace Riimu\Kit\PHPEncoder;`
219+
and `use PHPUnit\Framework\TestCase;`, you could set up the array as
220+
`['Riimu\\Kit\\PHPEncoder\\' => '', 'PHPUnit\\Framework\\TestCase' => 'TestCase']`
204221

205222
* **array.short** : &lt;boolean&gt; (true)<br>
206223
When set to `true`, arrays are enclosed using square brackets `[]` instead

examples/class_resolution.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/* Shows an example of how you can take advantage of class resolution in the
4+
generated code. */
5+
6+
require_once __DIR__ . '/../vendor/autoload.php';
7+
8+
$encoder = new \Riimu\Kit\PHPEncoder\PHPEncoder([
9+
'string.classes' => [
10+
'Riimu\\',
11+
'PHPUnit\\Framework\\TestCase',
12+
'DateTime',
13+
],
14+
'string.imports' => [
15+
'Riimu\\Kit\\PHPEncoder\\' => '',
16+
'PHPUnit\\Framework\\TestCase' => 'TestCase',
17+
],
18+
]);
19+
20+
21+
echo "<?php
22+
23+
namespace Riimu\Kit\PHPEncoder;
24+
25+
use PHPUnit\Framework\TestCase;
26+
27+
var_dump(";
28+
29+
echo $encoder->encode([
30+
\PHPUnit\Framework\TestCase::class,
31+
\Riimu\Kit\PHPEncoder\PHPEncoder::class,
32+
\Riimu\Kit\PHPEncoder\Encoder\Encoder::class,
33+
\DateTime::class,
34+
\DateTimeInterface::class, // Will be encoded as plain string, since it's not allowed by string.classes
35+
]);
36+
37+
echo ");\n";

src/Encoder/StringEncoder.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public function encode($value, $depth, array $options, callable $encode)
4444
return $this->getSingleQuotedString($value);
4545
}
4646

47+
/**
48+
* Tests if the given value is a string that could be encoded as a class name constant.
49+
* @param string $value The string to test
50+
* @param array $options The string encoding options
51+
* @return bool True if string can be encoded as class constant, false if not
52+
*/
4753
private function isClassName($value, array $options)
4854
{
4955
if (preg_match('/^([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\\\\(?1))*$/', $value) !== 1) {
@@ -53,6 +59,12 @@ private function isClassName($value, array $options)
5359
return array_intersect(iterator_to_array($this->iterateNamespaces($value)), $options['string.classes']) !== [];
5460
}
5561

62+
/**
63+
* Encodes the given string as a class name constant based on used imports.
64+
* @param string $value The string to encode
65+
* @param array $options The string encoding options
66+
* @return string The class constant PHP code representation
67+
*/
5668
private function getClassName($value, array $options)
5769
{
5870
foreach ($this->iterateNamespaces($value) as $partial) {
@@ -65,6 +77,11 @@ private function getClassName($value, array $options)
6577
return sprintf('\\%s::class', $value);
6678
}
6779

80+
/**
81+
* Iterates over the variations of the namespace for the given class name.
82+
* @param string $value The class name to iterate over
83+
* @return \Generator|string[] The namespace parts of the string
84+
*/
6885
private function iterateNamespaces($value)
6986
{
7087
yield $value;
@@ -77,6 +94,12 @@ private function iterateNamespaces($value)
7794
}
7895
}
7996

97+
/**
98+
* Returns the PHP code representation for the string that is not just simple ascii characters.
99+
* @param string $value The string to encode
100+
* @param array $options The string encoding options
101+
* @return string The PHP code representation for the complex string
102+
*/
80103
private function getComplexString($value, array $options)
81104
{
82105
if ($this->isBinaryString($value, $options)) {

0 commit comments

Comments
 (0)