Skip to content

Instantly share code, notes, and snippets.

@Gremlyn
Gremlyn / stringGenerator.php
Created August 9, 2016 14:52
These functions create random strings, and attempts to create pseduo-words and pseudo-sentences that mimic real text, despite being nonsensical.
<?php
function randomStringGenerator($length)
{
$string = '';
$start_frequency = ['a' => 1160,'b' => 470,'c' => 351,'d' => 267,'e' => 201,'f' => 378,'g' => 195,'h' => 723,'i' => 629,'j' => 60,'k' => 59,'l' => 271,'m' => 438,'n' => 237,'o' => 626,'p' => 255.,'q' => 17,'r' => 165,'s' => 776,'t' => 1667.,'u' => 149,'v' => 65,'w' => 675,'x' => 2,'y' => 162,'z' => 3];
$letter_frequency = ['a' => 817,'b' => 149,'c' => 278,'d' => 425,'e' => 1270,'f' => 223,'g' => 202,'h' => 609,'i' => 697,'j' => 15,'k' => 77,'l' => 403,'m' => 241,'n' => 675,'o' => 751,'p' => 193,'q' => 10,'r' => 599,'s' => 633,'t' => 906,'u' => 276,'v' => 98,'w' => 236,'x' => 15,'y' => 197,'z' => 7];
$starters = array();
@Gremlyn
Gremlyn / reflection.php
Last active August 29, 2015 14:23
Need to figure out if a param of the called method is of a specific class, and if so get that entity as the value to be set.
<?php
foreach ($data as $method=>$value) {
$reflect_class = new \ReflectionClass($task);
$reflect_method = $reflect_class->getMethod($method);
$reflection_params = $reflect_method->getParameters();
// The first param should always be an object, if it is one at all
$param_class = $reflection_params[0];
if ($param_class) {
<?php
namespace AppBundle\EventSubscriber;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CampaignFormType extends AbstractType
{
<?php
namespace AppBundle\Entity\Traits;
use Doctrine\ORM\Mapping as ORM;
trait TimestampTrait
{
/**
* @var \DateTime
@Gremlyn
Gremlyn / json_fixer.php
Created January 20, 2015 20:29
Method to fix expected errors in JSON formatting.
/**
* @param $text
*
* @return mixed|string
*/
private function preprocessText($text)
{
// Remove the first two lines
$text = implode("\n", array_slice(explode("\n", $text), 2));
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule {your rule here}
</IfModule>
@Gremlyn
Gremlyn / ProfileAccessVoter.php
Created April 11, 2014 18:47
This Symfony2 Voter checks whether the given user is in either the owner, edit, or view groups for a given profile.
<?php
namespace MyVendor\AppBundle\Security\Authorization\Voter;
use MyVendor\AppBundle\Entity\Profile;
use MyVendor\AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class ProfileAccessVoter implements VoterInterface
MyBundle\AppBundle\Entity\Profile:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: website
properties:
website:
- NotBlank: ~
- Url:
message: 'This URL is already in use.'
username:
- NotBlank: ~
@Gremlyn
Gremlyn / MetadataExtension.php
Last active December 31, 2015 23:59
I needed a way to store metadata about entities that didn't follow a strict relational table structure. The data to be stored for each entity, in the example below they are 'Task' entities, could vary depending on what the given Task is. This means I needed an Entity-Attribute-Value model, which isn't readily supported by Doctrine. With the code…
<?php
namespace Acme\AppBundle\Twig;
use Acme\AppBundle\Service\MetadataInterface;
class MetadataExtension extends \Twig_Extension
{
private $task_metadata;