ynloultratech /
graphql-bundle
| 1 | <?php |
||
| 2 | /******************************************************************************* |
||
| 3 | * This file is part of the GraphQL Bundle package. |
||
| 4 | * |
||
| 5 | * (c) YnloUltratech <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | ******************************************************************************/ |
||
| 10 | |||
| 11 | namespace Ynlo\GraphQLBundle\Annotation\Plugin; |
||
| 12 | |||
| 13 | use Doctrine\Common\Inflector\Inflector; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Can use this annotation as base to se plugin options. |
||
| 17 | * Override and define all plugin settings. |
||
| 18 | */ |
||
| 19 | abstract class PluginConfigAnnotation |
||
| 20 | { |
||
| 21 | 14 | public function __construct(array $config = []) |
|
| 22 | { |
||
| 23 | 14 | $ref = new \ReflectionClass(get_class($this)); |
|
| 24 | 14 | $properties = $ref->getProperties(); |
|
| 25 | |||
| 26 | //if only one value is given, the first property is set with the given value |
||
| 27 | 14 | if ($properties && isset($config['value']) && \count($config) === 1) { |
|
|
0 ignored issues
–
show
|
|||
| 28 | 2 | $propName = $properties[0]->getName(); |
|
| 29 | 2 | $this->$propName = $config['value']; |
|
| 30 | } else { |
||
| 31 | 13 | foreach ($config as $key => $value) { |
|
| 32 | 2 | $this->$key = $value; |
|
| 33 | } |
||
| 34 | } |
||
| 35 | 14 | } |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Must return the array with plugin config |
||
| 39 | * |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | 16 | public function getConfig(): array |
|
| 43 | { |
||
| 44 | 16 | $ref = new \ReflectionClass(get_class($this)); |
|
| 45 | 16 | $properties = $ref->getProperties(); |
|
| 46 | 16 | $config = []; |
|
| 47 | |||
| 48 | //set default values |
||
| 49 | 16 | foreach ($properties as $property) { |
|
| 50 | 16 | $value = $property->getValue($this); |
|
| 51 | 16 | if (null !== $value) { |
|
| 52 | 16 | $config[Inflector::tableize($property->getName())] = $property->getValue($this); |
|
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | 16 | return $config; |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Must return the plugin name to apply this config |
||
| 61 | * |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | 16 | public function getName(): string |
|
| 65 | { |
||
| 66 | 16 | preg_match('/\w+$/', get_class($this), $matches); |
|
| 67 | |||
| 68 | 16 | return Inflector::tableize($matches[0]); |
|
| 69 | } |
||
| 70 | } |
||
| 71 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.