Skip to content

Instantly share code, notes, and snippets.

@naosim
naosim / ArraySchema.php
Created September 26, 2017 22:25
ArraySchema
<?php
class ArraySchema {
private $value;
function __construct(array $value) {
$this->value = $value;
}
function getValue() {
return $this->value;
}
}
@naosim
naosim / File.php
Last active September 25, 2017 21:23
File
<?php
function rstrpos ($haystack, $needle) {
$size = strlen ($haystack);
$pos = strpos(strrev($haystack), $needle);
if ($pos === false) {
return false;
}
return $size - $pos;
}
@naosim
naosim / ApiUtils.php
Created September 25, 2017 12:57
ApiUtils
<?php
function api_forminput_jsonoutput($get_action, $post_action) {
header('Content-Type: application/json; charset=utf-8');
try {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo json_encode($post_action());
} else {
echo json_encode($get_action());
}
} catch(RuntimeException $e) {
@naosim
naosim / ValueObject.php
Last active September 25, 2017 12:56
ValueObject
<?php
declare(strict_types=1);
class StringVO {
protected $value; // string
function __construct(string $value) {
$this->value = $value;
}
public function getValue(): string {
return $this->value;
@naosim
naosim / VariousColumnTable.java
Created August 17, 2017 22:24
VariousColumnTable
package com.naosim.table;
import java.util.HashMap;
import java.util.Map;
public class VariousColumnTable<ROW> {
private final Map<Class, Integer> columnIndex;
private final Map<ROW, Object[]> rows;
public VariousColumnTable(Class ...columns) {
@naosim
naosim / Table.java
Created August 17, 2017 22:06
Table
package com.naosim.table;
import java.util.HashMap;
import java.util.Map;
public class Table<ROW, CLM, CELL> {
private final Map<CLM, Integer> columnIndex;
private final Map<ROW, CELL[]> rows;
public Table(CLM ...columns) {
@naosim
naosim / EasyInclude.php
Last active July 5, 2017 22:04
EasyInclude
<?php
class EasyInclude {
private $root;
private $excludePhp;
public function __construct($root, $excludePhp = null) {
$this->root = $root;
$this->excludePhp = $excludePhp;
}
private function loadFromWeb($url) {
@naosim
naosim / includeFromWeb.php
Last active July 5, 2017 22:01
[PHP]include php a file from web
<?php
function includeFromWeb($url, $root = '.') {
$file = $root . '/vendor/' . explode('//', $url)[1];
$dir = substr($file, 0, strrpos($file, '/'));
if(!file_exists($file)) {
if(!file_exists($dir)) {
mkdir($dir, 0777, true);
}
$text = trim(file_get_contents($url));
@naosim
naosim / DateTimeFactory.php
Last active July 5, 2017 22:32
いろいろPHP
<?php
interface DateTimeFactory {
public function createDateTime(): DateTime;
public function createUnixDateTime(): int;
}
class DateTimeFactoryImpl implements DateTimeFactory {
public function createDateTime(): DateTime {
return new DateTime();
}
@naosim
naosim / pipe.py
Last active June 21, 2017 22:27
pythonでパイプを処理する
import sys
i = 0
for line in sys.stdin:
print "{0}:{1}".format(i, line.strip())
i = i + 1