Skip to content

Instantly share code, notes, and snippets.

View alxfv's full-sized avatar

Alexander Fedorov alxfv

View GitHub Profile
<?php
namespace Application\Bundle\DefaultBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
<?php
protected function getSignature(array $params)
{
ksort($params, SORT_STRING);
$str = '';
foreach ($params as $key => $value) {
$str .= sprintf('%s=%s', $key, $value);
}
@alxfv
alxfv / gist:3234644
Created August 2, 2012 07:00
Copy file using OutputStream
File srcFile = new File("/var/www/afishapoisk/public/uploads/images/events/big/new.jpg");
File dstfile = new File("/var/www/afishapoisk/public/uploads/images/events/big/super.jpg");
try {
FileOutputStream dstFos = new FileOutputStream(dstfile);
FileInputStream srcFos = new FileInputStream(srcFile);
byte[] buffer = new byte[256];
int bytesRead;
do {
PreparedStatement pstmt = conn.prepareStatement("update blob_table set blob = ? where id = ?");
File blob = new File("/path/to/picture.png");
FileInputStream in = new FileInputStream(blob);
// the cast to int is necessary because with JDBC 4 there is
// also a version of this method with a (int, long)
// but that is not implemented by Oracle
pstmt.setBinaryStream(1, in, (int)blob.length());
pstmt.setInt(2, 42); // set the PK value
@alxfv
alxfv / gist:3792660
Created September 27, 2012 07:25
Access to attribute in SimpleXML
<?php
header('Content-Type: text/html; charset=utf-8');
$s = file_get_contents('http://bananadream-monkey:8983/solr/select/?q=%D0%BF%D1%83%D1%88%D0%BA%D0%B8%D0%BD%0D%0A&version=2.2&start=0&rows=10&indent=on');
$xml = simplexml_load_string($s);
$doc = $xml->result->doc[0];
echo $doc->str[0]->attributes()->name;
<?php
if (!empty($_GET['test'])) {
if (!empty($_SESSION)) {
foreach ($_SESSION as $name => $value) {
echo $name . '<br />';
}
}
foreach ($_COOKIE as $name => $value) {
echo $name . '<br />';
@alxfv
alxfv / gist:3858075
Created October 9, 2012 11:24
Lazy quantifier
<?php
$subject = <<<EOF
<style type="text/css">
p {
font-size: 100500px;
}
</style>
some text
@alxfv
alxfv / rAF.js
Created December 12, 2012 10:49 — forked from paulirish/rAF.js
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
@alxfv
alxfv / gist:4604329
Last active December 11, 2015 12:58
hw1-1
def palindrome?(str)
str.downcase.gsub(/\W/, '') == str.downcase.gsub(/\W/, '').reverse
end
def count_words(str)
words = {}
str.downcase.split(/\W+/).each do |word|
words[word] = words.include?(word) ? words[word] + 1 : words[word] = 1
end
words
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
2.times do |i|
raise NoSuchStrategyError unless ['P', 'S', 'R'].include? game[i][1].upcase
end
first = game[0][1].upcase
second = game[1][1].upcase
if first == 'P'