Last active
December 10, 2015 03:03
-
-
Save cristopher-rodrigues/528b707861c7babb2e15 to your computer and use it in GitHub Desktop.
Make Constraints Class (SF&Doctrine)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use DMS; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Doctrine\Common\Annotations\AnnotationReader; | |
use Symfony\Component\Validator; | |
class makeConstraints { | |
private $em; | |
public function __construct(EntityManagerInterface $em){ | |
$this->em = $em; | |
} | |
public function makeConstraintSearch(array $dataOptions, $entity){ | |
if (!class_exists($entity)) | |
throw new /*Internal*/\Exception("Not found Entity."); | |
$res["where"] = array(); | |
$res['limit'] = !empty($dataOptions['limit']) ? $dataOptions['limit'] : null; | |
$res['offset'] = !empty($dataOptions['offset']) ? $dataOptions['offset'] : null; | |
$res['orderBy'] = !empty($dataOptions['orderBy']) ? $dataOptions['orderBy'] : array(); | |
unset($dataOptions["limit"], $dataOptions["offset"], $dataOptions["orderBy"]); | |
if (!empty($res['limit']) && !ctype_digit($res["limit"])) | |
throw new InvalidValueFieldException(array("limit" => "Invalid value field limit.")); | |
if (!empty($res['offset']) && !ctype_digit($res["offset"])) | |
throw new InvalidValueFieldException(array("offset" => "Invalid value field offset.")); | |
$rc = $this->em->getClassMetadata($entity); | |
if (!empty($res["orderBy"])) { | |
if (!is_array($res["orderBy"])) | |
throw new InvalidValueFieldException(array("orderBy" => "Invalid value field to orderBy.")); | |
foreach ($res["orderBy"] as $field => $value) { | |
if (!in_array($field, array_keys($rc->getReflectionProperties()))) | |
throw new InvalidFieldException(array("orderBy" => array($field => "Field does not belong to ". $entity ." entity."))); | |
if (!in_array(strtoupper($value), array("ASC", "DESC"))) | |
throw new InvalidValueFieldException(array("orderBy" => array($field => "Invalid value to field $field by orderBy (ASC, DESC)."))); | |
} | |
} | |
$object = new $entity; | |
foreach ($dataOptions as $property => $value){ | |
if (!in_array($property, array_keys($rc->getReflectionProperties()))) | |
throw new InvalidFieldException(array($property => "Field does not belong to ". $entity ." entity.")); | |
unset($dataOptions[$property]); | |
$res["where"][$property] = $value; | |
$rc->setFieldValue($object, $property, $value); | |
} | |
self::makeConstraintSave($object, array("find")); | |
return $res; | |
} | |
public static function makeFilters($object){ | |
//Object is entity? | |
$filter = new DMS\Filter\Filter( | |
new DMS\Filter\Mapping\ClassMetadataFactory( | |
new DMS\Filter\Mapping\Loader\AnnotationLoader( | |
new AnnotationReader() | |
) | |
) | |
); | |
$filter->filterEntity($object); | |
} | |
public static function makeConstraintSave($object, array $groups = array("save")){ | |
//Object is entity? | |
self::makeFilters($object); | |
$groups[] = "Default"; | |
$violations = Validator\Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator()->validate($object, $groups); | |
if (!$violations->count()) | |
return true; | |
$constraints = null; | |
while($violations->getIterator()->valid()){ | |
$e = explode(":", explode(".", $it->current()->__toString())[1]); | |
$constraints[$e[0]] = trim(stripslashes($e[1])); | |
$it->next(); | |
} | |
throw new InvalidValueFieldException((string) $constraints); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment