* Initial version

This commit is contained in:
Danyi Dávid 2021-01-10 19:19:37 +01:00
commit 27ef798792
28 changed files with 6998 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.idea
/vendor/

365
.phan/config.php Normal file
View File

@ -0,0 +1,365 @@
<?php
use Phan\Issue;
/**
* This configuration file was automatically generated by 'phan --init --init-level=3'
*
* TODOs (added by 'phan --init'):
*
* - Go through this file and verify that there are no missing/unnecessary files/directories.
* (E.g. this only includes direct composer dependencies - You may have to manually add indirect composer dependencies to 'directory_list')
* - Look at 'plugins' and add or remove plugins if appropriate (see https://github.com/phan/phan/tree/v4/.phan/plugins#plugins)
* - Add global suppressions for pre-existing issues to suppress_issue_types (https://github.com/phan/phan/wiki/Tutorial-for-Analyzing-a-Large-Sloppy-Code-Base)
* - Consider setting up a baseline if there are a large number of pre-existing issues (see `phan --extended-help`)
*
* This configuration will be read and overlaid on top of the
* default configuration. Command line arguments will be applied
* after this file is read.
*
* @see https://github.com/phan/phan/wiki/Phan-Config-Settings for all configurable options
* @see https://github.com/phan/phan/tree/v4/src/Phan/Config.php
*
* A Note About Paths
* ==================
*
* Files referenced from this file should be defined as
*
* ```
* Config::projectPath('relative_path/to/file')
* ```
*
* where the relative path is relative to the root of the
* project which is defined as either the working directory
* of the phan executable or a path passed in via the CLI
* '-d' flag.
*/
return [
// The PHP version that the codebase will be checked for compatibility against.
// For best results, the PHP binary used to run Phan should have the same PHP version.
// (Phan relies on Reflection for some types, param counts,
// and checks for undefined classes/methods/functions)
//
// Supported values: `'5.6'`, `'7.0'`, `'7.1'`, `'7.2'`, `'7.3'`, `'7.4'`, `null`.
// If this is set to `null`,
// then Phan assumes the PHP version which is closest to the minor version
// of the php executable used to execute Phan.
//
// Note that the **only** effect of choosing `'5.6'` is to infer that functions removed in php 7.0 exist.
// (See `backward_compatibility_checks` for additional options)
// Automatically inferred from composer.json requirement for "php" of "^7.4 || ^8.0"
'target_php_version' => '7.4',
// If enabled, missing properties will be created when
// they are first seen. If false, we'll report an
// error message if there is an attempt to write
// to a class property that wasn't explicitly
// defined.
'allow_missing_properties' => false,
// If enabled, null can be cast to any type and any
// type can be cast to null. Setting this to true
// will cut down on false positives.
'null_casts_as_any_type' => false,
// If enabled, allow null to be cast as any array-like type.
//
// This is an incremental step in migrating away from `null_casts_as_any_type`.
// If `null_casts_as_any_type` is true, this has no effect.
'null_casts_as_array' => true,
// If enabled, allow any array-like type to be cast to null.
// This is an incremental step in migrating away from `null_casts_as_any_type`.
// If `null_casts_as_any_type` is true, this has no effect.
'array_casts_as_null' => true,
// If enabled, scalars (int, float, bool, string, null)
// are treated as if they can cast to each other.
// This does not affect checks of array keys. See `scalar_array_key_cast`.
'scalar_implicit_cast' => false,
// If enabled, any scalar array keys (int, string)
// are treated as if they can cast to each other.
// E.g. `array<int,stdClass>` can cast to `array<string,stdClass>` and vice versa.
// Normally, a scalar type such as int could only cast to/from int and mixed.
'scalar_array_key_cast' => true,
// If this has entries, scalars (int, float, bool, string, null)
// are allowed to perform the casts listed.
//
// E.g. `['int' => ['float', 'string'], 'float' => ['int'], 'string' => ['int'], 'null' => ['string']]`
// allows casting null to a string, but not vice versa.
// (subset of `scalar_implicit_cast`)
'scalar_implicit_partial' => [],
// If enabled, Phan will warn if **any** type in a method invocation's object
// is definitely not an object,
// or if **any** type in an invoked expression is not a callable.
// Setting this to true will introduce numerous false positives
// (and reveal some bugs).
'strict_method_checking' => false,
// If enabled, Phan will warn if **any** type of the object expression for a property access
// does not contain that property.
'strict_object_checking' => false,
// If enabled, Phan will warn if **any** type in the argument's union type
// cannot be cast to a type in the parameter's expected union type.
// Setting this to true will introduce numerous false positives
// (and reveal some bugs).
'strict_param_checking' => false,
// If enabled, Phan will warn if **any** type in a property assignment's union type
// cannot be cast to a type in the property's declared union type.
// Setting this to true will introduce numerous false positives
// (and reveal some bugs).
'strict_property_checking' => false,
// If enabled, Phan will warn if **any** type in a returned value's union type
// cannot be cast to the declared return type.
// Setting this to true will introduce numerous false positives
// (and reveal some bugs).
'strict_return_checking' => false,
// If true, seemingly undeclared variables in the global
// scope will be ignored.
//
// This is useful for projects with complicated cross-file
// globals that you have no hope of fixing.
'ignore_undeclared_variables_in_global_scope' => true,
// Set this to false to emit `PhanUndeclaredFunction` issues for internal functions that Phan has signatures for,
// but aren't available in the codebase, or from Reflection.
// (may lead to false positives if an extension isn't loaded)
//
// If this is true(default), then Phan will not warn.
//
// Even when this is false, Phan will still infer return values and check parameters of internal functions
// if Phan has the signatures.
'ignore_undeclared_functions_with_known_signatures' => true,
// Backwards Compatibility Checking. This is slow
// and expensive, but you should consider running
// it before upgrading your version of PHP to a
// new version that has backward compatibility
// breaks.
//
// If you are migrating from PHP 5 to PHP 7,
// you should also look into using
// [php7cc (no longer maintained)](https://github.com/sstalle/php7cc)
// and [php7mar](https://github.com/Alexia/php7mar),
// which have different backwards compatibility checks.
//
// If you are still using versions of php older than 5.6,
// `PHP53CompatibilityPlugin` may be worth looking into if you are not running
// syntax checks for php 5.3 through another method such as
// `InvokePHPNativeSyntaxCheckPlugin` (see .phan/plugins/README.md).
'backward_compatibility_checks' => false,
// If true, check to make sure the return type declared
// in the doc-block (if any) matches the return type
// declared in the method signature.
'check_docblock_signature_return_type_match' => false,
// This setting maps case-insensitive strings to union types.
//
// This is useful if a project uses phpdoc that differs from the phpdoc2 standard.
//
// If the corresponding value is the empty string,
// then Phan will ignore that union type (E.g. can ignore 'the' in `@return the value`)
//
// If the corresponding value is not empty,
// then Phan will act as though it saw the corresponding UnionTypes(s)
// when the keys show up in a UnionType of `@param`, `@return`, `@var`, `@property`, etc.
//
// This matches the **entire string**, not parts of the string.
// (E.g. `@return the|null` will still look for a class with the name `the`, but `@return the` will be ignored with the below setting)
//
// (These are not aliases, this setting is ignored outside of doc comments).
// (Phan does not check if classes with these names exist)
//
// Example setting: `['unknown' => '', 'number' => 'int|float', 'char' => 'string', 'long' => 'int', 'the' => '']`
'phpdoc_type_mapping' => [],
// Set to true in order to attempt to detect dead
// (unreferenced) code. Keep in mind that the
// results will only be a guess given that classes,
// properties, constants and methods can be referenced
// as variables (like `$class->$property` or
// `$class->$method()`) in ways that we're unable
// to make sense of.
//
// To more aggressively detect dead code,
// you may want to set `dead_code_detection_prefer_false_negative` to `false`.
'dead_code_detection' => false,
// Set to true in order to attempt to detect unused variables.
// `dead_code_detection` will also enable unused variable detection.
//
// This has a few known false positives, e.g. for loops or branches.
'unused_variable_detection' => false,
// Set to true in order to attempt to detect redundant and impossible conditions.
//
// This has some false positives involving loops,
// variables set in branches of loops, and global variables.
'redundant_condition_detection' => false,
// If enabled, Phan will act as though it's certain of real return types of a subset of internal functions,
// even if those return types aren't available in reflection (real types were taken from php 7.3 or 8.0-dev, depending on target_php_version).
//
// Note that with php 7 and earlier, php would return null or false for many internal functions if the argument types or counts were incorrect.
// As a result, enabling this setting with target_php_version 8.0 may result in false positives for `--redundant-condition-detection` when codebases also support php 7.x.
'assume_real_types_for_internal_functions' => false,
// If true, this runs a quick version of checks that takes less
// time at the cost of not running as thorough
// of an analysis. You should consider setting this
// to true only when you wish you had more **undiagnosed** issues
// to fix in your code base.
//
// In quick-mode the scanner doesn't rescan a function
// or a method's code block every time a call is seen.
// This means that the problem here won't be detected:
//
// ```php
// <?php
// function test($arg):int {
// return $arg;
// }
// test("abc");
// ```
//
// This would normally generate:
//
// ```
// test.php:3 PhanTypeMismatchReturn Returning type string but test() is declared to return int
// ```
//
// The initial scan of the function's code block has no
// type information for `$arg`. It isn't until we see
// the call and rescan `test()`'s code block that we can
// detect that it is actually returning the passed in
// `string` instead of an `int` as declared.
'quick_mode' => false,
// Override to hardcode existence and types of (non-builtin) globals in the global scope.
// Class names should be prefixed with `\`.
//
// (E.g. `['_FOO' => '\FooClass', 'page' => '\PageClass', 'userId' => 'int']`)
'globals_type_map' => [],
// The minimum severity level to report on. This can be
// set to `Issue::SEVERITY_LOW`, `Issue::SEVERITY_NORMAL` or
// `Issue::SEVERITY_CRITICAL`. Setting it to only
// critical issues is a good place to start on a big
// sloppy mature code base.
'minimum_severity' => Issue::SEVERITY_LOW,
// Add any issue types (such as `'PhanUndeclaredMethod'`)
// to this list to inhibit them from being reported.
'suppress_issue_types' => [],
// A regular expression to match files to be excluded
// from parsing and analysis and will not be read at all.
//
// This is useful for excluding groups of test or example
// directories/files, unanalyzable files, or files that
// can't be removed for whatever reason.
// (e.g. `'@Test\.php$@'`, or `'@vendor/.*/(tests|Tests)/@'`)
'exclude_file_regex' => '@^vendor/.*/(tests?|Tests?)/@',
// A list of files that will be excluded from parsing and analysis
// and will not be read at all.
//
// This is useful for excluding hopelessly unanalyzable
// files that can't be removed for whatever reason.
'exclude_file_list' => [],
// A directory list that defines files that will be excluded
// from static analysis, but whose class and method
// information should be included.
//
// Generally, you'll want to include the directories for
// third-party code (such as "vendor/") in this list.
//
// n.b.: If you'd like to parse but not analyze 3rd
// party code, directories containing that code
// should be added to the `directory_list` as well as
// to `exclude_analysis_directory_list`.
'exclude_analysis_directory_list' => [
'vendor/',
],
// Enable this to enable checks of require/include statements referring to valid paths.
// The settings `include_paths` and `warn_about_relative_include_statement` affect the checks.
'enable_include_path_checks' => true,
// The number of processes to fork off during the analysis
// phase.
'processes' => 1,
// List of case-insensitive file extensions supported by Phan.
// (e.g. `['php', 'html', 'htm']`)
'analyzed_file_extensions' => [
'php',
],
// You can put paths to stubs of internal extensions in this config option.
// If the corresponding extension is **not** loaded, then Phan will use the stubs instead.
// Phan will continue using its detailed type annotations,
// but load the constants, classes, functions, and classes (and their Reflection types)
// from these stub files (doubling as valid php files).
// Use a different extension from php to avoid accidentally loading these.
// The `tools/make_stubs` script can be used to generate your own stubs (compatible with php 7.0+ right now)
//
// (e.g. `['xdebug' => '.phan/internal_stubs/xdebug.phan_php']`)
'autoload_internal_extension_signatures' => [],
// A list of plugin files to execute.
//
// Plugins which are bundled with Phan can be added here by providing their name (e.g. `'AlwaysReturnPlugin'`)
//
// Documentation about available bundled plugins can be found [here](https://github.com/phan/phan/tree/v4/.phan/plugins).
//
// Alternately, you can pass in the full path to a PHP file with the plugin's implementation (e.g. `'vendor/phan/phan/.phan/plugins/AlwaysReturnPlugin.php'`)
'plugins' => [
'AlwaysReturnPlugin',
'PregRegexCheckerPlugin',
'UnreachableCodePlugin',
],
// A list of directories that should be parsed for class and
// method information. After excluding the directories
// defined in `exclude_analysis_directory_list`, the remaining
// files will be statically analyzed for errors.
//
// Thus, both first-party and third-party code being used by
// your application should be included in this list.
'directory_list' => [
'src',
'vendor/container-interop/container-interop/src',
'vendor/laminas/laminas-eventmanager/src',
'vendor/laminas/laminas-filter/src',
'vendor/laminas/laminas-form/src',
'vendor/laminas/laminas-hydrator/src',
'vendor/laminas/laminas-inputfilter/src',
'vendor/laminas/laminas-servicemanager/src',
'vendor/laminas/laminas-stdlib/src',
'vendor/laminas/laminas-validator/src',
'vendor/phan/phan/src/Phan',
'vendor/psr/container/src',
'vendor/roave/psr-container-doctrine/src',
'vendor/doctrine/common/lib',
'vendor/doctrine/collections/lib',
'vendor/doctrine/inflector/lib',
'vendor/doctrine/orm/lib',
'vendor/doctrine/persistence/lib',
],
// A list of individual files to include in analysis
// with a path relative to the root directory of the
// project.
'file_list' => [],
];

27
README.md Normal file
View File

@ -0,0 +1,27 @@
# DoctrineMezzioModule
Simple integration of doctrine into mezzio applications.
## Getting Started
You'll have to add the VCS to your global composer config.json:
```bash
$ cat ~/.composer/config.json
```
```json
{
"config": {},
"repositories": [
{
"type": "vcs",
"url": "https://gogs.ragnarok.yvan.hu/yvan/mezzio-api-skeleton.git"
}
]
}
```
Install the module in your existing mezzio application with composer:
```bash
$ composer require yvan/mezzio-api-skeleton --stability=dev
```

40
composer.json Normal file
View File

@ -0,0 +1,40 @@
{
"name": "yvan/doctrine-mezzio-module",
"description": "Doctrine module for mezzio middleware applications",
"type": "library",
"config": {
"sort-packages": true
},
"require": {
"php": "^7.4 || ^8.0",
"laminas/laminas-eventmanager": "^3.3",
"laminas/laminas-servicemanager": "^3.5",
"laminas/laminas-validator": "^2.13",
"laminas/laminas-form": "^2.15",
"roave/psr-container-doctrine": "^2.2"
},
"autoload": {
"psr-4": {
"DoctrineMezzioModule\\": "src/"
}
},
"authors": [
{
"name": "Danyi Dávid",
"email": "danyi.david@gmail.com"
}
],
"require-dev": {
"phan/phan": "^4.0"
},
"scripts": {
"phan": "phan",
"phan-init": "phan --init"
},
"extra": {
"laminas": {
"component": "DoctrineMezzioModule",
"config-provider": "DoctrineMezzioModule\\ConfigProvider"
}
}
}

3818
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

68
src/ConfigProvider.php Normal file
View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace DoctrineMezzioModule;
use Doctrine\ORM\EntityManager;
use Laminas\EventManager\EventManager;
use Roave\PsrContainerDoctrine\EntityManagerFactory;
/**
* The configuration provider for the DoctrineExpressiveModule module
*
* @see https://docs.zendframework.com/zend-component-installer/
*/
class ConfigProvider
{
/**
* Returns the configuration array
*
* To add a bit of a structure, each section is defined in a separate
* method which returns an array with its configuration.
*
*/
public function __invoke() : array
{
return [
'dependencies' => $this->getDependencies(),
'form_elements' => $this->getFormElements(),
];
}
/**
* Returns the container dependencies
*/
public function getDependencies() : array
{
return [
'aliases' => [
'EventManager' => EventManager::class,
'doctrine.hydrator' => Hydrator\DoctrineObject::class,
'doctrine.entity_manager.orm_default' => EntityManager::class,
],
'invokables' => [
EventManager::class => EventManager::class,
],
'factories' => [
Hydrator\DoctrineObject::class => Hydrator\DoctrineObjectFactory::class,
EntityManager::class => EntityManagerFactory::class,
],
];
}
/**
* Returns the form dependencies
*/
public function getFormElements() : array
{
return [
'aliases' => [
'doctrine.object_select' => Form\Element\ObjectSelect::class,
],
'factories' => [
Form\Element\ObjectSelect::class => Form\Element\ElementFactory::class,
],
];
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace DoctrineMezzioModule\Form\Element;
use Interop\Container\ContainerInterface;
class ElementFactory
{
public function __invoke(ContainerInterface $container, string $elementClass, array $options = [])
{
$em = $container->get('doctrine.entity_manager.orm_default');
/** @var ObjectSelect|ObjectRadio|ObjectMultiCheckbox $element */
$element = new $elementClass();
$element->setOption('object_manager', $em);
return $element;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace DoctrineMezzioModule\Form\Element\Exception;
use InvalidArgumentException;
class InvalidRepositoryResultException extends InvalidArgumentException
{
}

View File

@ -0,0 +1,81 @@
<?php
namespace DoctrineMezzioModule\Form\Element;
use Laminas\Form\Element\MultiCheckbox;
use Laminas\Stdlib\ArrayUtils;
use Traversable;
class ObjectMultiCheckbox extends MultiCheckbox
{
/**
* @var Proxy
*/
protected $proxy;
/**
* @return Proxy
*/
public function getProxy(): Proxy
{
if (null === $this->proxy) {
$this->proxy = new Proxy();
}
return $this->proxy;
}
/**
* @param array|Traversable $options
* @return self
*/
public function setOptions($options): ObjectMultiCheckbox
{
$this->getProxy()->setOptions($options);
return parent::setOptions($options);
}
/**
* @param string $key
* @param mixed $value
* @return self
*/
public function setOption($key, $value): ObjectMultiCheckbox
{
$this->getProxy()->setOptions([$key => $value]);
return parent::setOption($key, $value);
}
/**
* {@inheritDoc}
*/
public function setValue($value)
{
if ($value instanceof Traversable) {
$value = ArrayUtils::iteratorToArray($value);
} elseif ($value == null) {
return parent::setValue([]);
} elseif (! is_array($value)) {
$value = (array)$value;
}
return parent::setValue(array_map([$this->getProxy(), 'getValue'], $value));
}
/**
* {@inheritDoc}
*/
public function getValueOptions(): array
{
if (! empty($this->valueOptions)) {
return $this->valueOptions;
}
$proxyValueOptions = $this->getProxy()->getValueOptions();
if (! empty($proxyValueOptions)) {
$this->setValueOptions($proxyValueOptions);
}
return $this->valueOptions;
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace DoctrineMezzioModule\Form\Element;
use Laminas\Form\Element\Radio as RadioElement;
use Traversable;
class ObjectRadio extends RadioElement
{
/**
* @var Proxy
*/
protected $proxy;
/**
* @return Proxy
*/
public function getProxy(): Proxy
{
if (null === $this->proxy) {
$this->proxy = new Proxy();
}
return $this->proxy;
}
/**
* @param array|Traversable $options
* @return self
*/
public function setOptions($options): ObjectRadio
{
$this->getProxy()->setOptions($options);
return parent::setOptions($options);
}
/**
* @param string $key
* @param mixed $value
* @return self
*/
public function setOption($key, $value): ObjectRadio
{
$this->getProxy()->setOptions([$key => $value]);
return parent::setOption($key, $value);
}
/**
* {@inheritDoc}
*/
public function setValue($value)
{
return parent::setValue($this->getProxy()->getValue($value));
}
/**
* {@inheritDoc}
*/
public function getValueOptions(): array
{
if (! empty($this->valueOptions)) {
return $this->valueOptions;
}
$proxyValueOptions = $this->getProxy()->getValueOptions();
if (! empty($proxyValueOptions)) {
$this->setValueOptions($proxyValueOptions);
}
return $this->valueOptions;
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace DoctrineMezzioModule\Form\Element;
use Laminas\Form\Element\Select as SelectElement;
use Laminas\Stdlib\ArrayUtils;
use Traversable;
class ObjectSelect extends SelectElement
{
/**
* @var Proxy
*/
protected $proxy;
/**
* @return Proxy
*/
public function getProxy(): Proxy
{
if (null === $this->proxy) {
$this->proxy = new Proxy();
}
return $this->proxy;
}
/**
* @param array|Traversable $options
* @return self
*/
public function setOptions($options): ObjectSelect
{
$this->getProxy()->setOptions($options);
return parent::setOptions($options);
}
/**
* @param string $key
* @param mixed $value
* @return self
*/
public function setOption($key, $value): ObjectSelect
{
$this->getProxy()->setOptions([$key => $value]);
return parent::setOption($key, $value);
}
/**
* {@inheritDoc}
*/
public function setValue($value)
{
$multiple = $this->getAttribute('multiple');
if (true === $multiple || 'multiple' === $multiple) {
if ($value instanceof Traversable) {
$value = ArrayUtils::iteratorToArray($value);
} elseif ($value == null) {
return parent::setValue([]);
} elseif (! is_array($value)) {
$value = (array) $value;
}
return parent::setValue(array_map([$this->getProxy(), 'getValue'], $value));
}
return parent::setValue($this->getProxy()->getValue($value));
}
/**
* {@inheritDoc}
*/
public function getValueOptions(): array
{
if (! empty($this->valueOptions)) {
return $this->valueOptions;
}
$proxyValueOptions = $this->getProxy()->getValueOptions();
if (! empty($proxyValueOptions)) {
$this->setValueOptions($proxyValueOptions);
}
return $this->valueOptions;
}
}

657
src/Form/Element/Proxy.php Normal file
View File

@ -0,0 +1,657 @@
<?php
namespace DoctrineMezzioModule\Form\Element;
use Doctrine\Inflector\InflectorFactory;
use ReflectionException;
use Traversable;
use ReflectionMethod;
use RuntimeException;
use InvalidArgumentException;
use Laminas\Stdlib\Guard\ArrayOrTraversableGuardTrait;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Persistence\ObjectManager;
class Proxy
{
use ArrayOrTraversableGuardTrait;
/**
* @var array|Traversable
*/
protected $objects;
/**
* @var string
*/
protected $targetClass;
/**
* @var array
*/
protected $valueOptions = [];
/**
* @var array
*/
protected $findMethod = [];
/**
* @var string
*/
protected $property;
/**
* @var array
*/
protected $option_attributes = [];
/**
* @var callable $labelGenerator A callable used to create a label based on an item in the collection an Entity
*/
protected $labelGenerator;
/**
* @var bool|null
*/
protected $isMethod;
/**
* @var ObjectManager
*/
protected $objectManager;
/**
* @var bool
*/
protected $displayEmptyItem = false;
/**
* @var string
*/
protected $emptyItemLabel = '';
/**
* @var string|null
*/
protected $optgroupIdentifier;
/**
* @var string|null
*/
protected $optgroupDefault;
public function setOptions($options)
{
if (isset($options['object_manager'])) {
$this->setObjectManager($options['object_manager']);
}
if (isset($options['target_class'])) {
$this->setTargetClass($options['target_class']);
}
if (isset($options['property'])) {
$this->setProperty($options['property']);
}
if (isset($options['label_generator'])) {
$this->setLabelGenerator($options['label_generator']);
}
if (isset($options['find_method'])) {
$this->setFindMethod($options['find_method']);
}
if (isset($options['is_method'])) {
$this->setIsMethod($options['is_method']);
}
if (isset($options['display_empty_item'])) {
$this->setDisplayEmptyItem($options['display_empty_item']);
}
if (isset($options['empty_item_label'])) {
$this->setEmptyItemLabel($options['empty_item_label']);
}
if (isset($options['option_attributes'])) {
$this->setOptionAttributes($options['option_attributes']);
}
if (isset($options['optgroup_identifier'])) {
$this->setOptgroupIdentifier($options['optgroup_identifier']);
}
if (isset($options['optgroup_default'])) {
$this->setOptgroupDefault($options['optgroup_default']);
}
}
public function getValueOptions()
{
if (empty($this->valueOptions)) {
$this->loadValueOptions();
}
return $this->valueOptions;
}
/**
* @return array|Traversable
*/
public function getObjects()
{
$this->loadObjects();
return $this->objects;
}
/**
* Set the label for the empty option
*
* @param string $emptyItemLabel
*
* @return Proxy
*/
public function setEmptyItemLabel($emptyItemLabel): Proxy
{
$this->emptyItemLabel = $emptyItemLabel;
return $this;
}
/**
* @return string
*/
public function getEmptyItemLabel(): string
{
return $this->emptyItemLabel;
}
/**
* @return array
*/
public function getOptionAttributes(): array
{
return $this->option_attributes;
}
/**
* @param array $option_attributes
*/
public function setOptionAttributes(array $option_attributes)
{
$this->option_attributes = $option_attributes;
}
/**
* Set a flag, whether to include the empty option at the beginning or not
*
* @param boolean $displayEmptyItem
*
* @return Proxy
*/
public function setDisplayEmptyItem(bool $displayEmptyItem): Proxy
{
$this->displayEmptyItem = $displayEmptyItem;
return $this;
}
/**
* @return boolean
*/
public function getDisplayEmptyItem(): bool
{
return $this->displayEmptyItem;
}
/**
* Set the object manager
*
* @param ObjectManager $objectManager
*
* @return Proxy
*/
public function setObjectManager(ObjectManager $objectManager): Proxy
{
$this->objectManager = $objectManager;
return $this;
}
/**
* Get the object manager
*
* @return ObjectManager
*/
public function getObjectManager(): ObjectManager
{
return $this->objectManager;
}
/**
* Set the FQCN of the target object
*
* @param string $targetClass
*
* @return Proxy
*/
public function setTargetClass(string $targetClass): Proxy
{
$this->targetClass = $targetClass;
return $this;
}
/**
* Get the target class
*
* @return string
*/
public function getTargetClass(): string
{
return $this->targetClass;
}
/**
* Set the property to use as the label in the options
*
* @param string $property
*
* @return Proxy
*/
public function setProperty(string $property): Proxy
{
$this->property = $property;
return $this;
}
/**
* @return string
*/
public function getProperty(): string
{
return $this->property;
}
/**
* Set the label generator callable that is responsible for generating labels for the items in the collection
*
* @param callable $callable A callable used to create a label based off of an Entity
*
* @return void
* @throws InvalidArgumentException
*/
public function setLabelGenerator(callable $callable)
{
if (! is_callable($callable)) {
throw new InvalidArgumentException(
'Property "label_generator" needs to be a callable function or a \Closure'
);
}
$this->labelGenerator = $callable;
}
/**
* @return callable|null
*/
public function getLabelGenerator(): ?callable
{
return $this->labelGenerator;
}
/**
* @return string|null
*/
public function getOptgroupIdentifier(): ?string
{
return $this->optgroupIdentifier;
}
/**
* @param string $optgroupIdentifier
*/
public function setOptgroupIdentifier(string $optgroupIdentifier)
{
$this->optgroupIdentifier = (string) $optgroupIdentifier;
}
/**
* @return string|null
*/
public function getOptgroupDefault(): ?string
{
return $this->optgroupDefault;
}
/**
* @param string $optgroupDefault
*/
public function setOptgroupDefault(string $optgroupDefault)
{
$this->optgroupDefault = (string) $optgroupDefault;
}
/**
* Set if the property is a method to use as the label in the options
*
* @param boolean $method
*
* @return Proxy
*/
public function setIsMethod(bool $method): Proxy
{
$this->isMethod = (bool) $method;
return $this;
}
/**
* @return mixed
*/
public function getIsMethod(): ?bool
{
return $this->isMethod;
}
/** Set the findMethod property to specify the method to use on repository
*
* @param array $findMethod
*
* @return Proxy
*/
public function setFindMethod(array $findMethod): Proxy
{
$this->findMethod = $findMethod;
return $this;
}
/**
* Get findMethod definition
*
* @return array
*/
public function getFindMethod(): array
{
return $this->findMethod;
}
/**
* @param $targetEntity
*
* @return string|null
*/
protected function generateLabel($targetEntity): ?string
{
if (null === ($labelGenerator = $this->getLabelGenerator())) {
return null;
}
return call_user_func($labelGenerator, $targetEntity);
}
/**
* @param $value
*
* @return array|mixed|object
* @throws RuntimeException
*/
public function getValue($value)
{
if (! ($om = $this->getObjectManager())) {
throw new RuntimeException('No object manager was set');
}
if (! ($targetClass = $this->getTargetClass())) {
throw new RuntimeException('No target class was set');
}
$metadata = $om->getClassMetadata($targetClass);
if (is_object($value)) {
if ($value instanceof Collection) {
$data = [];
foreach ($value as $object) {
$values = $metadata->getIdentifierValues($object);
$data[] = array_shift($values);
}
$value = $data;
} else {
$metadata = $om->getClassMetadata(get_class($value));
$identifier = $metadata->getIdentifierFieldNames();
// TODO: handle composite (multiple) identifiers
if (null !== $identifier && count($identifier) > 1) {
//$value = $key;
} else {
$value = current($metadata->getIdentifierValues($value));
}
}
}
return $value;
}
/**
* Load objects
*
* @return void
* @throws Exception\InvalidRepositoryResultException
* @throws ReflectionException
* @throws RuntimeException
* @throws \Exception
*/
protected function loadObjects()
{
if (! empty($this->objects)) {
return;
}
$findMethod = (array) $this->getFindMethod();
if (! $findMethod) {
$findMethodName = 'findAll';
$repository = $this->objectManager->getRepository($this->targetClass);
$objects = $repository->findAll();
} else {
if (! isset($findMethod['name'])) {
throw new RuntimeException('No method name was set');
}
$findMethodName = $findMethod['name'];
$findMethodParams = isset($findMethod['params']) ? array_change_key_case($findMethod['params']) : [];
$repository = $this->objectManager->getRepository($this->targetClass);
if (! method_exists($repository, $findMethodName)) {
throw new RuntimeException(
sprintf(
'Method "%s" could not be found in repository "%s"',
$findMethodName,
get_class($repository)
)
);
}
$r = new ReflectionMethod($repository, $findMethodName);
$args = [];
foreach ($r->getParameters() as $param) {
if (array_key_exists(strtolower($param->getName()), $findMethodParams)) {
$args[] = $findMethodParams[strtolower($param->getName())];
} elseif ($param->isDefaultValueAvailable()) {
$args[] = $param->getDefaultValue();
} elseif (! $param->isOptional()) {
throw new RuntimeException(
sprintf(
'Required parameter "%s" with no default value for method "%s" in repository "%s"'
. ' was not provided',
$param->getName(),
$findMethodName,
get_class($repository)
)
);
}
}
$objects = $r->invokeArgs($repository, $args);
}
$this->guardForArrayOrTraversable(
$objects,
sprintf('%s::%s() return value', get_class($repository), $findMethodName),
'DoctrineModule\Form\Element\Exception\InvalidRepositoryResultException'
);
$this->objects = $objects;
}
/**
* Load value options
*
* @throws RuntimeException
* @return void
*/
protected function loadValueOptions()
{
if (! ($om = $this->objectManager)) {
throw new RuntimeException('No object manager was set');
}
if (! ($targetClass = $this->targetClass)) {
throw new RuntimeException('No target class was set');
}
$metadata = $om->getClassMetadata($targetClass);
$identifier = $metadata->getIdentifierFieldNames();
$objects = $this->getObjects();
$options = [];
$optionAttributes = [];
if ($this->displayEmptyItem) {
$options[''] = $this->getEmptyItemLabel();
}
foreach ($objects as $key => $object) {
if (null !== ($generatedLabel = $this->generateLabel($object))) {
$label = $generatedLabel;
} elseif ($property = $this->property) {
if ($this->isMethod == false && ! $metadata->hasField($property)) {
throw new RuntimeException(
sprintf(
'Property "%s" could not be found in object "%s"',
$property,
$targetClass
)
);
}
$inflector = InflectorFactory::create()->build();
$getter = 'get' . $inflector->classify($property);
if (! is_callable([$object, $getter])) {
throw new RuntimeException(
sprintf('Method "%s::%s" is not callable', $this->targetClass, $getter)
);
}
$label = $object->{$getter}();
} else {
if (! is_callable([$object, '__toString'])) {
throw new RuntimeException(
sprintf(
'%s must have a "__toString()" method defined if you have not set a property'
. ' or method to use.',
$targetClass
)
);
}
$label = (string) $object;
}
if (null !== $identifier && count($identifier) > 1) {
$value = $key;
} else {
$value = current($metadata->getIdentifierValues($object));
}
foreach ($this->getOptionAttributes() as $optionKey => $optionValue) {
if (is_string($optionValue)) {
$optionAttributes[$optionKey] = $optionValue;
continue;
}
if (is_callable($optionValue)) {
$callableValue = call_user_func($optionValue, $object);
$optionAttributes[$optionKey] = (string) $callableValue;
continue;
}
throw new RuntimeException(
sprintf(
'Parameter "option_attributes" expects an array of key => value where value is of type'
. '"string" or "callable". Value of type "%s" found.',
gettype($optionValue)
)
);
}
// If no optgroup_identifier has been configured, apply default handling and continue
if (is_null($this->getOptgroupIdentifier())) {
$options[] = ['label' => $label, 'value' => $value, 'attributes' => $optionAttributes];
continue;
}
// optgroup_identifier found, handle grouping
$inflector = InflectorFactory::create()->build();
$optgroupGetter = 'get' . $inflector->classify($this->getOptgroupIdentifier());
if (! is_callable([$object, $optgroupGetter])) {
throw new RuntimeException(
sprintf('Method "%s::%s" is not callable', $this->targetClass, $optgroupGetter)
);
}
$optgroup = $object->{$optgroupGetter}();
// optgroup_identifier contains a valid group-name. Handle default grouping.
if (false === is_null($optgroup) && trim($optgroup) !== '') {
$options[$optgroup]['label'] = $optgroup;
$options[$optgroup]['options'][] = [
'label' => $label,
'value' => $value,
'attributes' => $optionAttributes,
];
continue;
}
$optgroupDefault = $this->getOptgroupDefault();
// No optgroup_default has been provided. Line up without a group
if (is_null($optgroupDefault)) {
$options[] = ['label' => $label, 'value' => $value, 'attributes' => $optionAttributes];
continue;
}
// Line up entry with optgroup_default
$options[$optgroupDefault]['label'] = $optgroupDefault;
$options[$optgroupDefault]['options'][] = [
'label' => $label,
'value' => $value,
'attributes' => $optionAttributes,
];
}
$this->valueOptions = $options;
}
}

621
src/Hydrator/DoctrineObject.php Executable file
View File

@ -0,0 +1,621 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMezzioModule\Hydrator;
use DateTime;
use DateTimeImmutable;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Inflector\InflectorFactory;
use Exception;
use InvalidArgumentException;
use ReflectionException;
use RuntimeException;
use Traversable;
use Laminas\Stdlib\ArrayUtils;
use Laminas\Hydrator\AbstractHydrator;
use Laminas\Hydrator\Filter\FilterProviderInterface;
/**
* This hydrator has been completely refactored for DoctrineModule 0.7.0. It provides an easy and powerful way
* of extracting/hydrator objects in Doctrine, by handling most associations types.
*
* Starting from DoctrineModule 0.8.0, the hydrator can be used multiple times with different objects
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class DoctrineObject extends AbstractHydrator
{
/**
* @var ObjectManager
*/
protected ObjectManager $objectManager;
/**
* @var ClassMetadata
*/
protected ClassMetadata $metadata;
/**
* @var bool
*/
protected bool $byValue = true;
/**
* Constructor
*
* @param ObjectManager $objectManager The ObjectManager to use
* @param bool $byValue If set to true, hydrator will always use entity's public API
*/
public function __construct(ObjectManager $objectManager, $byValue = true)
{
$this->objectManager = $objectManager;
$this->byValue = (bool) $byValue;
}
/**
* Extract values from an object
*
* @param object $object
* @return array
* @throws ReflectionException
*/
public function extract(object $object): array
{
$this->prepare($object);
if ($this->byValue) {
return $this->extractByValue($object);
}
return $this->extractByReference($object);
}
/**
* Hydrate $object with the provided $data.
*
* @param array $data
* @param object $object
* @return object
* @throws ReflectionException
* @throws Exception
*/
public function hydrate(array $data, object $object): object
{
$this->prepare($object);
if ($this->byValue) {
return $this->hydrateByValue($data, $object);
}
return $this->hydrateByReference($data, $object);
}
/**
* Prepare the hydrator by adding strategies to every collection valued associations
*
* @param object $object
* @return void
*/
protected function prepare(object $object)
{
$this->metadata = $this->objectManager->getClassMetadata(get_class($object));
$this->prepareStrategies();
}
/**
* Prepare strategies before the hydrator is used
*
* @throws InvalidArgumentException
* @return void
*/
protected function prepareStrategies()
{
$associations = $this->metadata->getAssociationNames();
foreach ($associations as $association) {
if ($this->metadata->isCollectionValuedAssociation($association)) {
// Add a strategy if the association has none set by user
if (!$this->hasStrategy($association)) {
if ($this->byValue) {
$this->addStrategy($association, new Strategy\AllowRemoveByValue());
} else {
$this->addStrategy($association, new Strategy\AllowRemoveByReference());
}
}
$strategy = $this->getStrategy($association);
if (!$strategy instanceof Strategy\AbstractCollectionStrategy) {
throw new InvalidArgumentException(
sprintf(
'Strategies used for collections valued associations must inherit from '
. 'Strategy\AbstractCollectionStrategy, %s given',
get_class($strategy)
)
);
}
$strategy->setCollectionName($association)
->setClassMetadata($this->metadata);
}
}
}
/**
* Extract values from an object using a by-value logic (this means that it uses the entity
* API, in this case, getters)
*
* @param object $object
* @return array
* @throws RuntimeException
*/
protected function extractByValue(object $object): array
{
$fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
$methods = get_class_methods($object);
$filter = $object instanceof FilterProviderInterface
? $object->getFilter()
: $this->filterComposite;
$data = [];
foreach ($fieldNames as $fieldName) {
if ($filter && !$filter->filter($fieldName)) {
continue;
}
$inflector = InflectorFactory::create()->build();
$getter = 'get' . $inflector->classify($fieldName);
$isser = 'is' . $inflector->classify($fieldName);
$dataFieldName = $this->computeExtractFieldName($fieldName);
if (in_array($getter, $methods)) {
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$getter(), $object);
} elseif (in_array($isser, $methods)) {
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$isser(), $object);
} elseif (substr($fieldName, 0, 2) === 'is'
&& ctype_upper(substr($fieldName, 2, 1))
&& in_array($fieldName, $methods)) {
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$fieldName(), $object);
}
// Unknown fields are ignored
}
return $data;
}
/**
* Extract values from an object using a by-reference logic (this means that values are
* directly fetched without using the public API of the entity, in this case, getters)
*
* @param object $object
* @return array
* @throws ReflectionException
*/
protected function extractByReference(object $object): array
{
$fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
$refl = $this->metadata->getReflectionClass();
$filter = $object instanceof FilterProviderInterface
? $object->getFilter()
: $this->filterComposite;
$data = [];
foreach ($fieldNames as $fieldName) {
if ($filter && !$filter->filter($fieldName)) {
continue;
}
$reflProperty = $refl->getProperty($fieldName);
$reflProperty->setAccessible(true);
$dataFieldName = $this->computeExtractFieldName($fieldName);
$data[$dataFieldName] = $this->extractValue($fieldName, $reflProperty->getValue($object), $object);
}
return $data;
}
/**
* Hydrate the object using a by-value logic (this means that it uses the entity API, in this
* case, setters)
*
* @param array $data
* @param object $object
* @return object
* @throws RuntimeException|ReflectionException
* @throws Exception
*/
protected function hydrateByValue(array $data, object $object): object
{
$tryObject = $this->tryConvertArrayToObject($data, $object);
$metadata = $this->metadata;
if (is_object($tryObject)) {
$object = $tryObject;
}
foreach ($data as $field => $value) {
$field = $this->computeHydrateFieldName($field);
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
$inflector = InflectorFactory::create()->build();
$setter = 'set' . $inflector->classify($field);
if ($metadata->hasAssociation($field)) {
$target = $metadata->getAssociationTargetClass($field);
if ($metadata->isSingleValuedAssociation($field)) {
if (! method_exists($object, $setter)) {
continue;
}
$value = $this->toOne($target, $this->hydrateValue($field, $value, $data));
if (null === $value
&& !current($metadata->getReflectionClass()->getMethod($setter)->getParameters())->allowsNull()
) {
continue;
}
$object->$setter($value);
} elseif ($metadata->isCollectionValuedAssociation($field)) {
$this->toMany($object, $field, $target, $value);
}
} else {
if (! method_exists($object, $setter)) {
continue;
}
$object->$setter($this->hydrateValue($field, $value, $data));
}
}
return $object;
}
/**
* Hydrate the object using a by-reference logic (this means that values are modified directly without
* using the public API, in this case setters, and hence override any logic that could be done in those
* setters)
*
* @param array $data
* @param object $object
* @return object
* @throws Exception
*/
protected function hydrateByReference(array $data, object $object): object
{
$tryObject = $this->tryConvertArrayToObject($data, $object);
$metadata = $this->metadata;
$refl = $metadata->getReflectionClass();
if (is_object($tryObject)) {
$object = $tryObject;
}
foreach ($data as $field => $value) {
$field = $this->computeHydrateFieldName($field);
// Ignore unknown fields
if (!$refl->hasProperty($field)) {
continue;
}
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
$reflProperty = $refl->getProperty($field);
$reflProperty->setAccessible(true);
if ($metadata->hasAssociation($field)) {
$target = $metadata->getAssociationTargetClass($field);
if ($metadata->isSingleValuedAssociation($field)) {
$value = $this->toOne($target, $this->hydrateValue($field, $value, $data));
$reflProperty->setValue($object, $value);
} elseif ($metadata->isCollectionValuedAssociation($field)) {
$this->toMany($object, $field, $target, $value);
}
} else {
$reflProperty->setValue($object, $this->hydrateValue($field, $value, $data));
}
}
return $object;
}
/**
* This function tries, given an array of data, to convert it to an object if the given array contains
* an identifier for the object. This is useful in a context of updating existing entities, without ugly
* tricks like setting manually the existing id directly into the entity
*
* @param array $data The data that may contain identifiers keys
* @param object $object
* @return object
*/
protected function tryConvertArrayToObject(array $data, object $object): ?object
{
$metadata = $this->metadata;
$identifierNames = $metadata->getIdentifierFieldNames();
$identifierValues = [];
if (empty($identifierNames)) {
return $object;
}
foreach ($identifierNames as $identifierName) {
if (!isset($data[$identifierName])) {
return $object;
}
$identifierValues[$identifierName] = $data[$identifierName];
}
return $this->find($identifierValues, $metadata->getName());
}
/**
* Handle ToOne associations
*
* When $value is an array but is not the $target's identifiers, $value is
* most likely an array of fieldset data. The identifiers will be determined
* and a target instance will be initialized and then hydrated. The hydrated
* target will be returned.
*
* @param string $target
* @param mixed $value
* @return object
* @throws ReflectionException
*/
protected function toOne(string $target, $value): ?object
{
$metadata = $this->objectManager->getClassMetadata($target);
if (is_array($value) && array_keys($value) != $metadata->getIdentifier()) {
// $value is most likely an array of fieldset data
$identifiers = array_intersect_key(
$value,
array_flip($metadata->getIdentifier())
);
$object = $this->find($identifiers, $target) ?: new $target;
return $this->hydrate($value, $object);
}
return $this->find($value, $target);
}
/**
* Handle ToMany associations. In proper Doctrine design, Collections should not be swapped, so
* collections are always handled by reference. Internally, every collection is handled using specials
* strategies that inherit from AbstractCollectionStrategy class, and that add or remove elements but without
* changing the collection of the object
*
* @param object $object
* @param mixed $collectionName
* @param string $target
* @param mixed $values
*
* @return void
* @throws InvalidArgumentException|ReflectionException
*/
protected function toMany(object $object, $collectionName, string $target, $values)
{
$metadata = $this->objectManager->getClassMetadata(ltrim($target, '\\'));
$identifier = $metadata->getIdentifier();
if (!is_array($values) && !$values instanceof Traversable) {
$values = (array)$values;
}
$collection = [];
// If the collection contains identifiers, fetch the objects from database
foreach ($values as $value) {
if ($value instanceof $target) {
// assumes modifications have already taken place in object
$collection[] = $value;
continue;
} elseif (empty($value)) {
// assumes no id and retrieves new $target
$collection[] = $this->find($value, $target);
continue;
}
$find = [];
if (is_array($identifier)) {
foreach ($identifier as $field) {
switch (gettype($value)) {
case 'object':
$getter = 'get' . ucfirst($field);
if (method_exists($value, $getter)) {
$find[$field] = $value->$getter();
} elseif (property_exists($value, $field)) {
$find[$field] = $value->$field;
}
break;
case 'array':
if (array_key_exists($field, $value) && $value[$field] != null) {
$find[$field] = $value[$field];
unset($value[$field]); // removed identifier from persistable data
}
break;
default:
$find[$field] = $value;
break;
}
}
}
if (!empty($find) && $found = $this->find($find, $target)) {
$collection[] = (is_array($value)) ? $this->hydrate($value, $found) : $found;
} else {
$collection[] = (is_array($value)) ? $this->hydrate($value, new $target) : new $target;
}
}
$collection = array_filter(
$collection,
function ($item) {
return null !== $item;
}
);
// Set the object so that the strategy can extract the Collection from it
/** @var Strategy\AbstractCollectionStrategy $collectionStrategy */
$collectionStrategy = $this->getStrategy($collectionName);
$collectionStrategy->setObject($object); // @phan-suppress-current-line PhanUndeclaredMethod
// We could directly call hydrate method from the strategy, but if people want to override
// hydrateValue function, they can do it and do their own stuff
$this->hydrateValue($collectionName, $collection, $values);
}
/**
* Handle various type conversions that should be supported natively by Doctrine (like DateTime)
*
* @param mixed $value
* @param string $typeOfField
* @return DateTime|DateTimeImmutable|null
* @throws Exception
*/
protected function handleTypeConversions($value, string $typeOfField)
{
switch ($typeOfField) {
case 'datetimetz':
case 'datetime':
case 'time':
case 'date':
if ('' === $value) {
return null;
}
if (is_int($value)) {
$dateTime = new DateTime();
$dateTime->setTimestamp($value);
$value = $dateTime;
} elseif (is_string($value)) {
$value = new DateTime($value);
}
break;
case 'datetimetz_immutable':
case 'datetime_immutable':
case 'time_immutable':
case 'date_immutable':
if ('' === $value) {
return null;
}
if (is_int($value)) {
$dateTime = new DateTimeImmutable();
$dateTime->setTimestamp($value);
$value = $dateTime;
} elseif (is_string($value)) {
$value = new DateTimeImmutable($value);
}
break;
default:
}
return $value;
}
/**
* Find an object by a given target class and identifier
*
* @param mixed $identifiers
* @param string $targetClass
*
* @return object|null
*/
protected function find($identifiers, string $targetClass): ?object
{
if ($identifiers instanceof $targetClass) {
return $identifiers;
}
if ($this->isNullIdentifier($identifiers)) {
return null;
}
return $this->objectManager->find($targetClass, $identifiers);
}
/**
* Verifies if a provided identifier is to be considered null
*
* @param mixed $identifier
*
* @return bool
*/
private function isNullIdentifier($identifier): bool
{
if (null === $identifier) {
return true;
}
if ($identifier instanceof Traversable || is_array($identifier)) {
$nonNullIdentifiers = array_filter(
ArrayUtils::iteratorToArray($identifier),
function ($value) {
return null !== $value;
}
);
return empty($nonNullIdentifiers);
}
return false;
}
/**
* Applies the naming strategy if there is one set
*
* @param string $field
*
* @return string
*/
protected function computeHydrateFieldName(string $field): string
{
if ($this->hasNamingStrategy()) {
$field = $this->getNamingStrategy()->hydrate($field);
}
return $field;
}
/**
* Applies the naming strategy if there is one set
*
* @param string $field
*
* @return string
*/
protected function computeExtractFieldName(string $field): string
{
if ($this->hasNamingStrategy()) {
$field = $this->getNamingStrategy()->extract($field);
}
return $field;
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace DoctrineMezzioModule\Hydrator;
use Interop\Container\ContainerInterface;
class DoctrineObjectFactory
{
public function __invoke(ContainerInterface $container): DoctrineObject
{
$em = $container->get('doctrine.entity_manager.orm_default');
return new DoctrineObject($em);
}
}

View File

@ -0,0 +1,66 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMezzioModule\Hydrator\Filter;
use Laminas\Hydrator\Filter\FilterInterface;
/**
* Provides a filter to restrict returned fields by whitelisting or
* blacklisting property names.
*
* @license MIT
* @link http://www.doctrine-project.org/
* @author Liam O'Boyle <liam@ontheroad.net.nz>
*/
class PropertyName implements FilterInterface
{
/**
* The propteries to exclude.
*
* @var array
*/
protected $properties = [];
/**
* Either an exclude or an include.
*
* @var bool
*/
protected $exclude = null;
/**
* @param string|array $properties The properties to exclude or include.
* @param bool $exclude If the method should be excluded
*/
public function __construct($properties, $exclude = true)
{
$this->exclude = $exclude;
$this->properties = is_array($properties)
? $properties
: [$properties];
}
public function filter(string $property): bool
{
return in_array($property, $this->properties)
? !$this->exclude
: $this->exclude;
}
}

View File

@ -0,0 +1,190 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMezzioModule\Hydrator\Strategy;
use InvalidArgumentException;
use Doctrine\Common\Collections\Collection;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Laminas\Hydrator\Strategy\StrategyInterface;
/**
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
abstract class AbstractCollectionStrategy implements StrategyInterface
{
/**
* @var string
*/
protected $collectionName;
/**
* @var ClassMetadata
*/
protected $metadata;
/**
* @var object
*/
protected $object;
/**
* Set the name of the collection
*
* @param string $collectionName
* @return AbstractCollectionStrategy
*/
public function setCollectionName(string $collectionName): AbstractCollectionStrategy
{
$this->collectionName = (string) $collectionName;
return $this;
}
/**
* Get the name of the collection
*
* @return string
*/
public function getCollectionName(): string
{
return $this->collectionName;
}
/**
* Set the class metadata
*
* @param ClassMetadata $classMetadata
* @return AbstractCollectionStrategy
*/
public function setClassMetadata(ClassMetadata $classMetadata): AbstractCollectionStrategy
{
$this->metadata = $classMetadata;
return $this;
}
/**
* Get the class metadata
*
* @return ClassMetadata
*/
public function getClassMetadata(): ClassMetadata
{
return $this->metadata;
}
/**
* Set the object
*
* @param object $object
*
* @return AbstractCollectionStrategy
* @throws InvalidArgumentException
*/
public function setObject(object $object): AbstractCollectionStrategy
{
if (!is_object($object)) {
throw new InvalidArgumentException(
sprintf('The parameter given to setObject method of %s class is not an object', get_called_class())
);
}
$this->object = $object;
return $this;
}
/**
* Get the object
*
* @return object
*/
public function getObject(): object
{
return $this->object;
}
/**
* {@inheritDoc}
*/
public function extract($value, $object = null)
{
return $value;
}
/**
* Return the collection by value (using the public API)
*
* @throws \InvalidArgumentException
*
* @return Collection
*/
protected function getCollectionFromObjectByValue(): Collection
{
$object = $this->getObject();
$getter = 'get' . ucfirst($this->getCollectionName());
if (!method_exists($object, $getter)) {
throw new InvalidArgumentException(
sprintf(
'The getter %s to access collection %s in object %s does not exist',
$getter,
$this->getCollectionName(),
get_class($object)
)
);
}
return $object->$getter();
}
/**
* Return the collection by reference (not using the public API)
*
* @return Collection
* @throws \ReflectionException
*/
protected function getCollectionFromObjectByReference(): Collection
{
$object = $this->getObject();
$refl = $this->getClassMetadata()->getReflectionClass();
$reflProperty = $refl->getProperty($this->getCollectionName());
$reflProperty->setAccessible(true);
return $reflProperty->getValue($object);
}
/**
* This method is used internally by array_udiff to check if two objects are equal, according to their
* SPL hash. This is needed because the native array_diff only compare strings
*
* @param object $a
* @param object $b
*
* @return int
*/
protected function compareObjects(object $a, object $b): int
{
return strcmp(spl_object_hash($a), spl_object_hash($b));
}
}

View File

@ -0,0 +1,61 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMezzioModule\Hydrator\Strategy;
use Doctrine\Common\Collections\Collection;
/**
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
* the original collection, then this strategy remove elements from the original collection. For instance, if the
* collection initially contains elements A and B, and that the new collection contains elements B and C, then the
* final collection will contain elements B and C (while element A will be asked to be removed).
*
* This strategy is by reference, this means it won't use public API to add/remove elements to the collection
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class AllowRemoveByReference extends AbstractCollectionStrategy
{
/**
* {@inheritDoc}
* @throws \ReflectionException
*/
public function hydrate($value, $data = null): Collection
{
$collection = $this->getCollectionFromObjectByReference();
$collectionArray = $collection->toArray();
$toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']);
$toRemove = array_udiff($collectionArray, $value, [$this, 'compareObjects']);
foreach ($toAdd as $element) {
$collection->add($element);
}
foreach ($toRemove as $element) {
$collection->removeElement($element);
}
return $collection;
}
}

View File

@ -0,0 +1,77 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMezzioModule\Hydrator\Strategy;
use Doctrine\Common\Collections\Collection;
use LogicException;
use Doctrine\Common\Collections\ArrayCollection;
/**
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
* the original collection, then this strategy remove elements from the original collection. For instance, if the
* collection initially contains elements A and B, and that the new collection contains elements B and C, then the
* final collection will contain elements B and C (while element A will be asked to be removed).
*
* This strategy is by value, this means it will use the public API (in this case, adder and remover)
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class AllowRemoveByValue extends AbstractCollectionStrategy
{
/**
* {@inheritDoc}
*/
public function hydrate($value, $data = null)
{
// AllowRemove strategy need "adder" and "remover"
$adder = 'add' . ucfirst($this->collectionName);
$remover = 'remove' . ucfirst($this->collectionName);
if (!method_exists($this->object, $adder) || !method_exists($this->object, $remover)) {
throw new LogicException(
sprintf(
'AllowRemove strategy for DoctrineModule hydrator requires both %s and %s to be defined in %s
entity domain code, but one or both seem to be missing',
$adder,
$remover,
get_class($this->object)
)
);
}
$collection = $this->getCollectionFromObjectByValue();
if ($collection instanceof Collection) {
$collection = $collection->toArray();
}
/** @var array $collection */
$toAdd = new ArrayCollection(array_udiff($value, $collection, [$this, 'compareObjects']));
$toRemove = new ArrayCollection(array_udiff($collection, $value, [$this, 'compareObjects']));
$this->object->$adder($toAdd);
$this->object->$remover($toRemove);
return $collection;
}
}

View File

@ -0,0 +1,54 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMezzioModule\Hydrator\Strategy;
/**
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
* the original collection, then this strategy will not remove those elements. At most, it will add new elements. For
* instance, if the collection initially contains elements A and B, and that the new collection contains elements B
* and C, then the final collection will contain elements A, B and C.
*
* This strategy is by reference, this means it won't use the public API to remove elements
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class DisallowRemoveByReference extends AbstractCollectionStrategy
{
/**
* {@inheritDoc}
* @throws \ReflectionException
*/
public function hydrate($value, $data = null)
{
$collection = $this->getCollectionFromObjectByReference();
$collectionArray = $collection->toArray();
$toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']);
foreach ($toAdd as $element) {
$collection->add($element);
}
return $collection;
}
}

View File

@ -0,0 +1,72 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMezzioModule\Hydrator\Strategy;
use Doctrine\Common\Collections\Collection;
use LogicException;
use Doctrine\Common\Collections\ArrayCollection;
/**
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
* the original collection, then this strategy will not remove those elements. At most, it will add new elements. For
* instance, if the collection initially contains elements A and B, and that the new collection contains elements B
* and C, then the final collection will contain elements A, B and C.
*
* This strategy is by value, this means it will use the public API (in this case, remover)
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class DisallowRemoveByValue extends AbstractCollectionStrategy
{
/**
* {@inheritDoc}
*/
public function hydrate($value, $data = null)
{
// AllowRemove strategy need "adder"
$adder = 'add' . ucfirst($this->collectionName);
if (!method_exists($this->object, $adder)) {
throw new LogicException(
sprintf(
'DisallowRemove strategy for DoctrineModule hydrator requires %s to
be defined in %s entity domain code, but it seems to be missing',
$adder,
get_class($this->object)
)
);
}
$collection = $this->getCollectionFromObjectByValue();
if ($collection instanceof Collection) {
$collection = $collection->toArray();
}
$toAdd = new ArrayCollection(array_udiff($value, $collection, [$this, 'compareObjects']));
$this->object->$adder($toAdd);
return $collection;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace DoctrineMezzioModule\Validator;
/**
* Class that validates if objects does not exist in a given repository with a given list of matched fields
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.4.0
* @author Marco Pivetta <ocramius@gmail.com>
*/
class NoObjectExists extends ObjectExists
{
/**
* Error constants
*/
const ERROR_OBJECT_FOUND = 'objectFound';
/**
* @var array Message templates
*/
protected $messageTemplates = [
self::ERROR_OBJECT_FOUND => "An object matching '%value%' was found",
];
/**
* {@inheritDoc}
*/
public function isValid($value): bool
{
$cleanedValue = $this->cleanSearchValue($value);
$match = $this->objectRepository->findOneBy($cleanedValue);
if (is_object($match)) {
$this->error(self::ERROR_OBJECT_FOUND, $value);
return false;
}
return true;
}
}

View File

@ -0,0 +1,177 @@
<?php
declare(strict_types=1);
namespace DoctrineMezzioModule\Validator;
use Laminas\Validator\AbstractValidator;
use Laminas\Validator\Exception;
use Doctrine\Common\Persistence\ObjectRepository;
use Laminas\Stdlib\ArrayUtils;
/**
* Class that validates if objects exist in a given repository with a given list of matched fields
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.4.0
* @author Marco Pivetta <ocramius@gmail.com>
*/
class ObjectExists extends AbstractValidator
{
/**
* Error constants
*/
const ERROR_NO_OBJECT_FOUND = 'noObjectFound';
/**
* @var array Message templates
*/
protected $messageTemplates = [
self::ERROR_NO_OBJECT_FOUND => "No object matching '%value%' was found",
];
/**
* ObjectRepository from which to search for entities
*
* @var ObjectRepository
*/
protected $objectRepository;
/**
* Fields to be checked
*
* @var array
*/
protected $fields;
/**
* Constructor
*
* @param array $options required keys are `object_repository`, which must be an instance of
* Doctrine\Common\Persistence\ObjectRepository, and `fields`, with either
* a string or an array of strings representing the fields to be matched by the validator.
* @throws Exception\InvalidArgumentException
*/
public function __construct(array $options)
{
if (! isset($options['object_repository']) || ! $options['object_repository'] instanceof ObjectRepository) {
if (! array_key_exists('object_repository', $options)) {
$provided = 'nothing';
} else {
if (is_object($options['object_repository'])) {
$provided = get_class($options['object_repository']);
} else {
$provided = getType($options['object_repository']);
}
}
throw new Exception\InvalidArgumentException(
sprintf(
'Option "object_repository" is required and must be an instance of'
. ' Doctrine\Common\Persistence\ObjectRepository, %s given',
$provided
)
);
}
$this->objectRepository = $options['object_repository'];
if (! isset($options['fields'])) {
throw new Exception\InvalidArgumentException(
'Key `fields` must be provided and be a field or a list of fields to be used when searching for'
. ' existing instances'
);
}
$this->fields = $options['fields'];
$this->validateFields();
parent::__construct($options);
}
/**
* Filters and validates the fields passed to the constructor
*
* @throws Exception\InvalidArgumentException
*/
private function validateFields()
{
$fields = (array) $this->fields;
if (empty($fields)) {
throw new Exception\InvalidArgumentException('Provided fields list was empty!');
}
foreach ($fields as $key => $field) {
if (! is_string($field)) {
throw new Exception\InvalidArgumentException(
sprintf('Provided fields must be strings, %s provided for key %s', gettype($field), $key)
);
}
}
$this->fields = array_values($fields);
}
/**
* @param string|array $value a field value or an array of field values if more fields have been configured to be
* matched
* @return array
* @throws Exception\RuntimeException
*/
protected function cleanSearchValue($value): array
{
$value = is_object($value) ? [$value] : (array) $value;
if (ArrayUtils::isHashTable($value)) {
$matchedFieldsValues = [];
foreach ($this->fields as $field) {
if (! array_key_exists($field, $value)) {
throw new Exception\RuntimeException(
sprintf(
'Field "%s" was not provided, but was expected since the configured field lists needs'
. ' it for validation',
$field
)
);
}
$matchedFieldsValues[$field] = $value[$field];
}
} else {
$matchedFieldsValues = @array_combine($this->fields, $value);
/** @phan-suppress-next-line PhanTypeComparisonToArray */
if (false === $matchedFieldsValues) { //
throw new Exception\RuntimeException(
sprintf(
'Provided values count is %s, while expected number of fields to be matched is %s',
count($value),
count($this->fields)
)
);
}
}
return $matchedFieldsValues;
}
/**
* {@inheritDoc}
*/
public function isValid($value): bool
{
$cleanedValue = $this->cleanSearchValue($value);
$match = $this->objectRepository->findOneBy($cleanedValue);
if (is_object($match)) {
return true;
}
$this->error(self::ERROR_NO_OBJECT_FOUND, $value);
return false;
}
}

View File

@ -0,0 +1,112 @@
<?php
namespace DoctrineMezzioModule\Validator\Service;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Interop\Container\Exception\ContainerException;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use DoctrineMezzioModule\Validator\Service\Exception\ServiceCreationException;
use Laminas\Stdlib\ArrayUtils;
/**
* Factory for creating NoObjectExists instances
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 1.3.0
* @author Fabian Grutschus <f.grutschus@lubyte.de>
* @phan-file-suppress PhanTypeInvalidThrowsIsInterface
*/
abstract class AbstractValidatorFactory implements FactoryInterface
{
const DEFAULT_OBJECTMANAGER_KEY = 'doctrine.entity_manager.orm_default';
protected $creationOptions = [];
protected $validatorClass;
/**
* @param ContainerInterface $container
* @param array $options
* @return ObjectRepository
* @throws ServiceCreationException
*/
protected function getRepository(ContainerInterface $container, array $options): ObjectRepository
{
if (empty($options['target_class'])) {
throw new ServiceCreationException(sprintf(
"Option 'target_class' is missing when creating validator %s",
__CLASS__
));
}
$objectManager = $this->getObjectManager($container, $options);
$targetClassName = $options['target_class'];
/** @var ObjectRepository $objectRepository */
$objectRepository = $objectManager->getRepository($targetClassName);
return $objectRepository;
}
/**
* @param ContainerInterface $container
* @param array $options
* @return ObjectManager
*/
protected function getObjectManager(ContainerInterface $container, array $options)
{
$objectManager = ($options['object_manager']) ?? self::DEFAULT_OBJECTMANAGER_KEY;
if (is_string($objectManager)) {
$objectManager = $container->get($objectManager);
}
return $objectManager;
}
/**
* @param array $options
* @return array
*/
protected function getFields(array $options): array
{
if (isset($options['fields'])) {
return (array) $options['fields'];
}
return [];
}
/**
* Helper to merge options array passed to `__invoke`
* together with the options array created based on the above
* helper methods.
*
* @param array $previousOptions
* @param array $newOptions
* @return array
*/
protected function merge(array $previousOptions, array $newOptions): array
{
return ArrayUtils::merge($previousOptions, $newOptions, true);
}
/**
* @param ServiceLocatorInterface $serviceLocator
* @return object
* @throws ContainerException
*/
public function createService(ServiceLocatorInterface $serviceLocator): object
{
return $this($serviceLocator, $this->validatorClass, $this->creationOptions);
}
public function setCreationOptions(array $options)
{
$this->creationOptions = $options;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace DoctrineMezzioModule\Validator\Service\Exception;
use RuntimeException as BaseRuntimeException;
/**
* @license MIT
* @link http://www.doctrine-project.org/
* @since 1.3.0
* @author Fabian Grutschus <f.grutschus@lubyte.de>
*/
class ServiceCreationException extends BaseRuntimeException
{
}

View File

@ -0,0 +1,30 @@
<?php
namespace DoctrineMezzioModule\Validator\Service;
use Interop\Container\ContainerInterface;
use DoctrineMezzioModule\Validator\NoObjectExists;
/**
* Factory for creating NoObjectExists instances
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 1.3.0
* @author Fabian Grutschus <f.grutschus@lubyte.de>
*/
class NoObjectExistsFactory extends AbstractValidatorFactory
{
protected $validatorClass = NoObjectExists::class;
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): NoObjectExists
{
$repository = $this->getRepository($container, $options);
return new NoObjectExists($this->merge($options, [
'object_repository' => $repository,
'fields' => $this->getFields($options),
]));
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace DoctrineMezzioModule\Validator\Service;
use Interop\Container\ContainerInterface;
use DoctrineMezzioModule\Validator\ObjectExists;
/**
* Factory for creating ObjectExists instances
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 1.3.0
* @author Fabian Grutschus <f.grutschus@lubyte.de>
*/
class ObjectExistsFactory extends AbstractValidatorFactory
{
protected $validatorClass = ObjectExists::class;
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): ObjectExists
{
$repository = $this->getRepository($container, $options);
return new ObjectExists($this->merge($options, [
'object_repository' => $repository,
'fields' => $this->getFields($options),
]));
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace DoctrineMezzioModule\Validator\Service;
use Interop\Container\ContainerInterface;
use DoctrineMezzioModule\Validator\UniqueObject;
class UniqueObjectFactory extends AbstractValidatorFactory
{
protected $validatorClass = UniqueObject::class;
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): UniqueObject
{
$useContext = isset($options['use_context']) ? (boolean) $options['use_context'] : false;
return new UniqueObject($this->merge($options, [
'object_manager' => $this->getObjectManager($container, $options),
'use_context' => $useContext,
'object_repository' => $this->getRepository($container, $options),
'fields' => $this->getFields($options),
]));
}
}

View File

@ -0,0 +1,166 @@
<?php
namespace DoctrineMezzioModule\Validator;
use Doctrine\Common\Persistence\ObjectManager;
use Laminas\Validator\Exception;
/**
* Class that validates if objects exist in a given repository with a given list of matched fields only once.
*
* @license MIT
* @link http://www.doctrine-project.org/
* @author Oskar Bley <oskar@programming-php.net>
*/
class UniqueObject extends ObjectExists
{
/**
* Error constants
*/
const ERROR_OBJECT_NOT_UNIQUE = 'objectNotUnique';
/**
* @var array Message templates
*/
protected $messageTemplates = [
self::ERROR_OBJECT_NOT_UNIQUE => "There is already another object matching '%value%'",
];
/**
* @var ObjectManager
*/
protected $objectManager;
/**
* @var boolean
*/
protected $useContext;
/***
* Constructor
*
* @param array $options required keys are `object_repository`, which must be an instance of
* Doctrine\Common\Persistence\ObjectRepository, `object_manager`, which
* must be an instance of Doctrine\Common\Persistence\ObjectManager,
* and `fields`, with either a string or an array of strings representing
* the fields to be matched by the validator.
* @throws Exception\InvalidArgumentException
*/
public function __construct(array $options)
{
parent::__construct($options);
if (! isset($options['object_manager']) || ! $options['object_manager'] instanceof ObjectManager) {
if (! array_key_exists('object_manager', $options)) {
$provided = 'nothing';
} else {
if (is_object($options['object_manager'])) {
$provided = get_class($options['object_manager']);
} else {
$provided = getType($options['object_manager']);
}
}
throw new Exception\InvalidArgumentException(
sprintf(
'Option "object_manager" is required and must be an instance of'
. ' Doctrine\Common\Persistence\ObjectManager, %s given',
$provided
)
);
}
$this->objectManager = $options['object_manager'];
$this->useContext = isset($options['use_context']) ? (boolean) $options['use_context'] : false;
}
/**
* Returns false if there is another object with the same field values but other identifiers.
*
* @param mixed $value
* @param array $context
* @return boolean
*/
public function isValid($value, $context = null): bool
{
if (! $this->useContext) {
$context = (array) $value;
}
$cleanedValue = $this->cleanSearchValue($value);
$match = $this->objectRepository->findOneBy($cleanedValue);
if (! is_object($match)) {
return true;
}
$expectedIdentifiers = $this->getExpectedIdentifiers($context);
$foundIdentifiers = $this->getFoundIdentifiers($match);
if (count(array_diff_assoc($expectedIdentifiers, $foundIdentifiers)) == 0) {
return true;
}
$this->error(self::ERROR_OBJECT_NOT_UNIQUE, $value);
return false;
}
/**
* Gets the identifiers from the matched object.
*
* @param object $match
* @return array
* @throws Exception\RuntimeException
*/
protected function getFoundIdentifiers($match)
{
return $this->objectManager
->getClassMetadata($this->objectRepository->getClassName())
->getIdentifierValues($match);
}
/**
* Gets the identifiers from the context.
*
* @param array|object $context
* @return array
* @throws Exception\RuntimeException
*/
protected function getExpectedIdentifiers($context = null)
{
if ($context === null) {
throw new Exception\RuntimeException(
'Expected context to be an array but is null'
);
}
$className = $this->objectRepository->getClassName();
if ($context instanceof $className) {
return $this->objectManager
->getClassMetadata($this->objectRepository->getClassName())
->getIdentifierValues($context);
}
$result = [];
foreach ($this->getIdentifiers() as $identifierField) {
if (! array_key_exists($identifierField, $context)) {
throw new Exception\RuntimeException(\sprintf('Expected context to contain %s', $identifierField));
}
$result[$identifierField] = $context[$identifierField];
}
return $result;
}
/**
* @return array the names of the identifiers
*/
protected function getIdentifiers()
{
return $this->objectManager
->getClassMetadata($this->objectRepository->getClassName())
->getIdentifierFieldNames();
}
}