Skip to content

Instantly share code, notes, and snippets.

@SofHad
SofHad / Symfony Exceptions
Last active July 18, 2016 07:06
Symfony Exceptions
Symfony/Component/OptionsResolver/Exception/MissingOptionsException.php
Symfony/Component/OptionsResolver/Exception/OptionDefinitionException.php
Symfony/Component/OptionsResolver/Exception/InvalidOptionsException.php
Symfony/Component/Serializer/Exception/InvalidArgumentException.php
Symfony/Component/Serializer/Exception/UnsupportedException.php
Symfony/Component/Serializer/Exception/UnexpectedValueException.php
Symfony/Component/Serializer/Exception/LogicException.php
Symfony/Component/Serializer/Exception/Exception.php
Symfony/Component/Serializer/Exception/RuntimeException.php
Symfony/Component/HttpKernel/Exception/MethodNotAllowedHttpException.php
Doctrine/Common/DataFixtures/Exception/CircularReferenceException.php
Doctrine/Common/Annotations/AnnotationException.php
Doctrine/Common/CommonException.php
Doctrine/Common/Persistence/Mapping/MappingException.php
Doctrine/ORM/TransactionRequiredException.php
Doctrine/ORM/Query/QueryException.php
Doctrine/ORM/Query/AST/ASTException.php
Doctrine/ORM/Mapping/MappingException.php
Doctrine/ORM/UnexpectedResultException.php
Doctrine/ORM/PessimisticLockException.php
@SofHad
SofHad / initializeSymfony
Created August 6, 2013 20:36
initialize Symfony Controller
public function initializeAction() {
$this->doctrine = $this->getDoctrine();
$this->bundle = $this->container->getParameter("bundle");
$this->entityManager = $this->doctrine->getEntityManager();
$this->productRepository = $this->doctrine->getRepository($this->bundle . ':Product');
$this->categoryRepository = $this->doctrine->getRepository($this->bundle . ':Category');
$this->categoryModel = $categoryModel = $this->get("CategoryModel");
$this->productModel = $this->get("ProductModel");
@SofHad
SofHad / gist:6168420
Created August 6, 2013 20:40
[Symfony Tests Unitaires] - Les assertions les plus utiles
// Vérifie qu'il y a au moins une balise h2 dans la classe "subtitle"
$this->assertGreaterThan(0, $crawler->filter('h2.subtitle')->count());
// Vérifie qu'il y a exactement 4 balises h2 sur la page
$this->assertCount(4, $crawler->filter('h2'));
// Vérifie que l'entête "Content-Type" vaut "application/json"
$this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'));
// Vérifie que le contenu retourné correspond à la regex
@SofHad
SofHad / UseSymfonyAndTwig
Last active December 20, 2015 17:28
Use Symfony components and Twig in your project.
<?php
/*
# ------------------------------------------------------------------------
# SymfonyAsLib
# ------------------------------------------------------------------------
# Developer : Sofiane Haddag, sofiane.haddag@yahoo.fr
*/
use Symfony\Component\Validator\Validation;
{
"require": {
"symfony/event-dispatcher": "2.1.0-stable"
}
}
<?php
namespace RE\AssetBundle\Model;
use Knp\Bundle\GaufretteBundle\FilesystemMap;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Gaufrette\FileStream\Local;
@SofHad
SofHad / FunctionToCamelizesAString
Created August 16, 2013 11:37
Function to Camelizes a string in PHP
/**
* Camelizes a string.
*
* @param string $id A string to camelize
*
* @return string The camelized string
*/
public static function camelize($id)
{
return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ '))), array(' ' => ''));
@SofHad
SofHad / gist:6574775
Created September 15, 2013 22:14
Symfony OptionsResolver example of use case
<?php
namespace Demo\FormBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller ;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
@SofHad
SofHad / in_array_multidimensional_array.php
Created October 31, 2013 09:08
in_array() and multidimensional array
public function inMultiDimensionalArray($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (\is_array($item) && $this->inMultidimensionalArray($needle, $item, $strict))) {
return true;
}
}
return false;
}