-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Field slugifier id generator #585
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?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 Doctrine\ODM\PHPCR\Id; | ||
|
||
use Doctrine\ODM\PHPCR\DocumentManager; | ||
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; | ||
use PHPCR\RepositoryException; | ||
use PHPCR\Util\NodeHelper; | ||
|
||
/** | ||
* Generate the id using the auto naming strategy | ||
*/ | ||
class FieldSlugifierIdGenerator extends ParentIdGenerator | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $slugifier; | ||
|
||
/** | ||
* @param callable Slugifier callable | ||
*/ | ||
public function __construct($slugifier) | ||
{ | ||
$this->slugifier = $slugifier; | ||
} | ||
|
||
/** | ||
* Use the parent field together with an auto generated name to generate the id | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public function generate($document, ClassMetadata $class, DocumentManager $dm, $parent = null) | ||
{ | ||
if (null === $parent) { | ||
$parent = $class->parentMapping ? $class->getFieldValue($document, $class->parentMapping) : null; | ||
} | ||
|
||
if (null === $parent) { | ||
throw IdException::noIdNoParent($document, $class->parentMapping); | ||
} | ||
|
||
if (!isset($class->idGeneratorOptions['field'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i guess we can't detect this misconfiguration earlier? |
||
throw new \InvalidArgumentException( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doctrine exceptions are supposed to have static factory methods to have all exceptions in one central place. |
||
'The field slugifier ID generator requires that you specify the '. | ||
'"field" option specifying the field to be slugified.' | ||
); | ||
} | ||
|
||
$fieldName = $class->idGeneratorOptions['field']; | ||
|
||
if (!$class->hasField($fieldName)) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'"%s" has been specified as the field to be slugified by the ' . | ||
'field slugifier ID generator. But it is not mapped', | ||
$fieldName | ||
)); | ||
} | ||
|
||
$value = $class->getFieldValue($document, $fieldName); | ||
|
||
if (!$value) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Cannot slugify node name from empty field value for field "%s"', | ||
$fieldName | ||
)); | ||
} | ||
|
||
$slugified = $this->slugify($value); | ||
|
||
$parentId = $dm->getUnitOfWork()->getDocumentId($parent); | ||
|
||
return $parentId . '/' . $slugified; | ||
} | ||
|
||
/** | ||
* Try and call the slugifier | ||
*/ | ||
private function slugify($string) | ||
{ | ||
static $resolvedSlugifier = null; | ||
|
||
if ($resolvedSlugifier) { | ||
return $resolvedSlugifier($string); | ||
} | ||
|
||
$slugifier = $this->slugifier; | ||
|
||
if ($slugifier instanceof \Closure) { | ||
return $resolvedSlugifier = function ($string) use ($slugifier) { | ||
$slugifier($string); | ||
}; | ||
} | ||
|
||
if (is_array($string)) { | ||
return $resolvedSlugifier = function ($string) use ($slugifier) { | ||
call_user_func_array($slugifier[0], $slugifier[1]); | ||
}; | ||
} | ||
|
||
if (is_string($string)) { | ||
return $resolvedSlugifier = function ($string) use ($slugifier) { | ||
call_user_func($string); | ||
}; | ||
} | ||
|
||
throw new \InvalidArgumentException(sprintf( | ||
'Could not call given slugifier callable of type "%s"', | ||
gettype($string) | ||
)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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 Doctrine\ODM\PHPCR\Mapping\Annotations; | ||
|
||
use Doctrine\Common\Annotations\Annotation; | ||
|
||
/** | ||
* @Annotation | ||
* @Target("PROPERTY") | ||
*/ | ||
final class AssocArray extends TranslatableProperty | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $type = 'assoc-array'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems unrelated to the slugifier id strategy |
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,9 @@ final class Id | |
* @var string | ||
*/ | ||
public $strategy; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $options; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\CMS; | ||
|
||
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; | ||
|
||
/** | ||
* @PHPCRODM\Document | ||
*/ | ||
class CmsBlock | ||
{ | ||
/** @PHPCRODM\Id */ | ||
public $id; | ||
|
||
/** @PHPCRODM\PhpArray */ | ||
public $config; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ODM\PHPCR\Functional; | ||
|
||
use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface; | ||
use Doctrine\ODM\PHPCR\DocumentRepository; | ||
use Doctrine\ODM\PHPCR\Exception\InvalidArgumentException; | ||
use PHPCR\PropertyType; | ||
use PHPCR\Util\UUIDHelper; | ||
|
||
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; | ||
use Doctrine\Tests\Models\CMS\CmsBlock; | ||
|
||
/** | ||
* @group functional | ||
*/ | ||
class PhpArrayTest extends \Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bah .. not part of this PR |
||
{ | ||
/** | ||
* @var \Doctrine\ODM\PHPCR\DocumentManager | ||
*/ | ||
private $dm; | ||
|
||
public function setUp() | ||
{ | ||
$this->dm = $this->createDocumentManager(array(__DIR__)); | ||
$this->node = $this->resetFunctionalNode($this->dm); | ||
} | ||
|
||
public function providePersist() | ||
{ | ||
return array( | ||
array( | ||
array( | ||
'rows' => 3, | ||
'title' => 'Foobar', | ||
), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider providePersist | ||
*/ | ||
public function testPersist($config) | ||
{ | ||
$block = new CmsBlock(); | ||
$block->id = '/functional/test'; | ||
$block->config = $config; | ||
$this->dm->persist($block); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
$block = $this->dm->find(null, '/functional/test'); | ||
$this->assertSame( | ||
$config, | ||
$block->config | ||
); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/peroforming/performing